我刚刚开始学习图像处理和Matlab,我试图使用平均4像素缩小图像。这意味着对于每4个原始像素,我计算平均值并产生1个输出像素。 到目前为止,我有以下代码:
img = imread('bird.jpg');
row_size = size(img, 1);
col_size = size(img, 2);
res = zeros(floor(row_size/2), floor(col_size/2));
figure, imshow(img);
for i = 1:2:row_size
for j = 1:2:col_size
num = mean([img(i, j), img(i, j+1), img(i+1, j), img(i+1, j+1)]);
res(round(i/2), round(j/2)) = num;
end
end
figure, imshow(uint8(res));
此代码设法缩小图像,但会将其转换为灰度。 我知道我可能必须计算输出像素的 RGB 组件的平均值,但我不知道如何访问它们,计算平均值并将它们插入结果矩阵。
答案 0 :(得分:3)
在Matlab中,RGB图像被视为3D阵列。您可以查看:
depth_size = size(img, 3)
depth_size =
3
正如您所做的那样,循环解决方案在Sardar_Usama's answer中进行了解释。但是,在Matlab中,建议您在想要获得速度时避免循环。
这是一种矢量化解决方案,可以将RGB图像按比例缩小n
:
img = imread('bird.jpg');
n = 2; % n can only be integer
[row_size, col_size] = size(img(:, :, 1));
% getting rid of extra rows and columns that won't be counted in averaging:
I = img(1:n*floor(row_size / n), 1:n*floor(col_size / n), :);
[r, ~] = size(I(:, :, 1));
% separating and re-ordering the three colors of image in a way ...
% that averaging could be done with a single 'mean' command:
R = reshape(permute(reshape(I(:, :, 1), r, n, []), [2, 1, 3]), n*n, [], 1);
G = reshape(permute(reshape(I(:, :, 2), r, n, []), [2, 1, 3]), n*n, [], 1);
B = reshape(permute(reshape(I(:, :, 3), r, n, []), [2, 1, 3]), n*n, [], 1);
% averaging and reshaping the colors back to the image form:
R_avg = reshape(mean(R), r / n, []);
G_avg = reshape(mean(G), r / n, []);
B_avg = reshape(mean(B), r / n, []);
% concatenating the three colors together:
scaled_img = cat(3, R_avg, G_avg, B_avg);
% casting the result to the class of original image
scaled_img = cast(scaled_img, 'like', img);
如果您想知道为什么矢量化解决方案更受欢迎,请查看使用这两种方法处理RGB 768 x 1024图像所需的时间:
------------------- With vectorized solution:
Elapsed time is 0.024690 seconds.
------------------- With nested loop solution:
Elapsed time is 6.127933 seconds.
因此两种解决方案之间的速度差异超过2个数量级。
答案 1 :(得分:0)
您可以使用以下修改后的代码来处理:
img = imread('bird.jpg');
row_size = size(img, 1);
col_size = size(img, 2);
figure, imshow(img);
res = zeros(floor(row_size/2), floor(col_size/2), 3); %Pre-allocation
for p = 1:2:row_size
for q = 1:2:col_size
num = mean([img(p, q,:), img(p, q+1,:), img(p+1, q,:), img(p+1, q+1,:)]);
res(round(p/2), round(q/2),:) = num;
end
end
figure, imshow(uint8(res));
我拍了一张1200x1600x3 uint8
的示例图片,上面的代码转换为600x800x3 uint8
这是正确的,因为(1200*1600)/4 = 480000
和600*800 = 480000
PS:我将变量名称i
和j
分别更改为p
和q
,因为i
和{{ 1}}保留给imaginary numbers
。
答案 2 :(得分:0)
另一种可能的解决方案是使用this link中提到的函数blockproc
。这也可以避免循环。