简体   繁体   English

Matlab-平均使用四个像素按比例缩小图像

[英]Matlab - Scale down an image using an average of four pixels

I have just started learning image-processing and Matlab and I'm trying to scale down an image using an average of 4 pixels. 我刚刚开始学习图像处理和Matlab,并且尝试使用平均4个像素按比例缩小图像。 That means that for every 4 original pixels I calculate the average and produce 1 output pixel. 这意味着我每4个原始像素计算一次平均值,并产生1个输出像素。 So far I have the following code: 到目前为止,我有以下代码:

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));

This code manages to scale down the image but it converts it to grayscale. 此代码设法按比例缩小图像,但将其转换为灰度。 I understand that I probably have to calculate the average of the RGB components for the output pixel but I don't know how to access them, calculate the average and insert them to the result matrix. 我知道我可能必须计算输出像素的RGB分量的平均值,但是我不知道如何访问它们,计算平均值并将它们插入到结果矩阵中。

In Matlab, an RGB image is treated as a 3D array. 在Matlab中,RGB图像被视为3D阵列。 You can check it with: 您可以使用以下方法进行检查:

depth_size = size(img, 3)

depth_size =

     3

The loop solution, as you have done, is explained in Sardar_Usama's answer . 正如您所做的那样,循环解决方案在Sardar_Usama的答案中进行了解释。 However, in Matlab it is recommended to avoid loops whenever you want to gain speed. 但是,在Matlab中,建议每当要提高速度时就避免循环。

This is a vectorized solution to scale down an RGB image by a factor of n : 这是将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); 

Benchmarking: 标杆:

If you want to know why vectorized solutions are more popular, take a look at how long it takes to process an RGB 768 x 1024 image with the two methods: 如果您想知道为什么矢量化解决方案更受欢迎,请看一下使用两种方法处理RGB 768 x 1024图像需要多长时间:

------------------- With vectorized solution:
Elapsed time is 0.024690 seconds.

------------------- With nested loop solution:
Elapsed time is 6.127933 seconds.

So there is more than 2 orders of magnitude difference of speed between the two solutions. 因此,两个解决方案之间的速度差异超过2个数量级。

You can take care of that using the modified code below: 您可以使用下面的修改后的代码来解决:

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));

I took a sample image of 1200x1600x3 uint8 which is converted to 600x800x3 uint8 by the above code which is correct because (1200*1600)/4 = 480000 and 600*800 = 480000 我把样品图像1200x1600x3 uint8其换算为600x800x3 uint8由上述码是正确的,因为(1200*1600)/4 = 480000600*800 = 480000

PS : I changed the variable names i and j to p and q respectively since i and j are reserved for imaginary numbers . PS:我将变量名ij分别更改为pq因为ij保留用于imaginary numbers

Another possible solution can be using the function blockproc as mentioned at this link . 另一个可能的解决方案是使用此链接上提到的功能blockproc This will also avoid for loops. 这也将避免for循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM