我正试图找到R,G&的中位数值。每个像素的B通道为每组10个图像中的一个100,以找到背景图像。我的价值观似乎都是正确的,但当我尝试在代码末尾显示背景时,它总是白色的,请帮助
%// 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)
感谢
答案 0 :(得分:3)
第一步是对代码进行矢量化。而不是以下代码块:
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或单个,值介于0和1之间,或者类uint8或uint16,其值分别为0,255或0,65535。如果您的值介于0和255之间但类(子图像)返回double或single,请在使用imshow()之前执行以下操作:
subimages = uint8(subimages);
答案 1 :(得分:0)
尝试
imshow(background,[])
使用imshow时,MATLAB需要设置显示范围。对于单灰度或双灰度图像,默认显示范围为[0 1]。这意味着任何大于1的值都将表示为白色。您可以通过手动设置自己的显示范围来解决此问题,例如
imshow(background,[0 100],
或者您可以让MATLAB通过
计算新的显示范围imshow(background,[])
与
相同imshow(background,[min(background(:)) max(background(:))])
答案 2 :(得分:0)
您可以将代码重写为:
%# 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)