【问题标题】:Matlab - displaying a background imageMatlab - 显示背景图像
【发布时间】:2011-10-31 13:41:08
【问题描述】:

我正在尝试为一组 100 个图像中的每个第 10 个图像找到每个像素的 R、G 和 B 通道的中值,以找到背景图像。我的值似乎都是正确的,但是当我尝试在代码末尾显示背景时,它总是白色的,请帮助

%// list all the files in some folder
folder = '~/V&R/1/';
filelist = dir(folder);
images = zeros(480,640,3,100);
% images = [];
%// the first two in filelist are . and ..

count = 1;
for i=3:size(filelist,1)

    if filelist(i).isdir ~= true
        fname = filelist(i).name;
        %// if file extension is jpg
        if strcmp( fname(size(fname,2)-3:size(fname,2)) ,'.jpg'  ) == 1
            tmp = imread([folder fname]);
            images(:,:,:,count) = tmp;
            count = count +1;

        end
    end
end


background = zeros(480,640,3);
 for j=1:480
    for i=1:640
        tmpR = zeros(1,10);
        tmpG = zeros(1,10);
        tmpB = zeros(1,10);
        for k=1:10
            tmpR(k) = images(j,i,1,k*10);
            tmpG(k) = images(j,i,2,k*10);
            tmpB(k) = images(j,i,3,k*10);

        end
        background(j,i,1) = floor(median(tmpR));
        background(j,i,2) = floor(median(tmpG));
        background(j,i,3) = floor(median(tmpB));
    end
 end
imshow(background)

谢谢

【问题讨论】:

  • 即兴发挥,我想说试试imagesc(background)

标签: image matlab background


【解决方案1】:

第一步是矢量化您的代码。而不是下面的代码块:

background = zeros(480,640,3);
for j=1:480
  for i=1:640
    tmpR = zeros(1,10);
    tmpG = zeros(1,10);
    tmpB = zeros(1,10);
    for k=1:10
        tmpR(k) = images(j,i,1,k*10);
        tmpG(k) = images(j,i,2,k*10);
        tmpB(k) = images(j,i,3,k*10);

    end
    background(j,i,1) = floor(median(tmpR));
    background(j,i,2) = floor(median(tmpG));
    background(j,i,3) = floor(median(tmpB));
  end
end

写:

subimages = images(:, :, :, 1:10:end);
background = median(subimages, 4);

现在如前所述,使用带有 [] 选项的 imshow 来显示您的图像:

imshow(background, []);

如果您仍然看到白色图像,那么您可能正在处理一个不介于 [0, 1] 之间的双精度值矩阵。 Matlab 中的图像通常是 double 类或 single 类,其值介于 0 和 1 之间,或者是 uint8 或 uint16 类,其值分别介于 0、255 或 0、65535 之间。如果您的值介于 0 和 255 之间,但 class(subimages) 返回双精度或单精度,请在使用 imshow() 之前执行以下操作:

subimages = uint8(subimages);

【讨论】:

  • 这相当于下面这行代码; imshow(background, [min(background(:), max(background(:)]); 如果你的 min(background(:)) 和 max(background(:)) 都为零,那么你的原始图像 4D 矩阵有问题。
  • 我得到了 3 和 210 的最小值和最大值,但仍然得到白色图像
  • 那么您可能有一个图像只有几个低值像素,而图像的其余部分都是(210、210、210)RGB 值。使用 imshow(..., []),所有这些值都转换为白色,您可能无法检测到较暗的分散像素。使用 imtool(subimages, []) 并使用阈值工具,看看是否可以找到类似的散点。
  • 使用您编辑的代码,我仍然收到我为以下答案发布的错误消息:S
  • 试试我刚刚添加到原始帖子中的最后一部分。
【解决方案2】:

试试

imshow(background,[])

使用imshow时,MATLAB需要设置一个显示范围。对于单灰度或双灰度图像,默认显示范围为 [0 1]。这意味着任何大于 1 的值都将表示为白色。您可以通过手动设置自己的显示范围来解决此问题,比如

imshow(background,[0 100],

或者你可以让 MATLAB 计算一个新的显示范围

imshow(background,[])

与

相同
imshow(background,[min(background(:)) max(background(:))])

【讨论】:

  • 嗨,我尝试使用第一个选项并收到此错误消息:??? Error using ==> checkDisplayRange at 22 HIGH must be greater than LOW. Error in ==> imageDisplayValidateParams at 57 common_args.DisplayRange = checkDisplayRange(common_args.DisplayRange,mfilename); Error in ==> imageDisplayParseInputs at 79 common_args = imageDisplayValidateParams(common_args); Error in ==> imshow at 199 [common_args,specific_args] = ... Error in ==> a1 at 44 imshow(background,[]) 然后尝试其他两个,但直到得到相同的白色图像结果
  • 背景的最小值和最大值是多少?
【解决方案3】:

您可以将代码重写为:

%# get filenames of all JPG images in some folder
folder = '~/V&R/1/';
filelist = dir( fullfile(folder,'*.jpg') );
filelist = strcat(folder, filesep, {filelist.name});

%# read files, and store as 'double' images in a 4D matrix
images = zeros(480,640,3, numel(filelist), 'double');
for i=1:numel(filelist)
    images(:,:,:,i) = im2double( imread(filelist{i}) );
end

%# estimate background using median
subimages = images(:,:,:,1:10:end);
background = median(subimages, 4);
imshow(background)

【讨论】:

    猜你喜欢
    • 2013-04-30
    • 2010-12-19
    • 2012-10-16
    • 2021-02-26
    • 2012-12-11
    • 2016-08-19
    • 2016-06-11
    • 2015-12-10
    • 2019-02-27
    相关资源
    最近更新 更多