使用 immaximas 找到图像中最常见的色块?

Use immaximas to find the most common colour block in image?

我想在图像上应用 64x64 滑动 window 并输出 window 中最常见的颜色。 immaximas 会是我要找的吗?还是这个函数会告诉我色块的位置?

我的启发式示例:

pkg load image;
pkg load signal;

i = imread('foo.jpg');

[r,c] = immaximas(i, 65);  % radius=65 ie a 65x65 kernel correct?
% I should now know the most common colour in 65x6 regions across the image right?

来自 immaximas 文档。

Find local spatial maximas.

...

A local spatial maxima is defined as an image point with a value that is larger than all neighbouring values in a square region of width 2*radius+1

所以immaximas与找到最常见的值无关。

数字列表中最常见的元素称为众数。如果您想简单地输出图像中每个点最常见的颜色,您可以使用图像处理工具箱中的 nlfilter

imode = nlfilter(i, [64 64], 'sliding', @mode);

如果图像是类型为 uint8 的 3 通道,您可以将像素编码为单个值,然后找到模式。此外,最好忽略边缘上的隐式零填充,因此我们将编码值加 1 并找到仅非零值的模式。

i = uint32(imread('peppers.png'));

% encode 3 channel image into single channel
ienc = 1+bitor(bitor(i(:,:,1), bitshift(i(:,:,2),8)), bitshift(i(:,:,3),16));

% find the mode
imode = nlfilter(ienc, [64 64], @(x) mode(x(x~=0)));

% decode single into 3 channel image
idec = zeros(size(i));
idec(:,:,1) = bitand(uint32(255), imode(:,:,1)-1);
idec(:,:,2) = bitshift(bitand(uint32(65280), imode(:,:,1)-1),-8);
idec(:,:,3) = bitshift(bitand(uint32(16711680), imode(:,:,1)-1),-16);
idec = uint8(idec);

imshow(uint8(idec));

结果