如何取2x2像素块的平均值? [英] how to take an average of a 2x2 pixel block?

查看:237
本文介绍了如何取2x2像素块的平均值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像,我必须采用2x2像素的平均颜色。

I have an image and I have to take the average of the color of 2x2 pixel.

取2x2像素块的平均颜色,然后计算图像中颜色与我们使用的颜色之间的距离。

我不知道采用2x2像素块的平均值意味着什么。怎么解决这个问题?

I have no idea what taking the average of 2x2 pixel block means. How to solve this?

谢谢。

推荐答案

我建议的另一种方法是使用 colfilt '滑动' flag和 表示 作为操作的功能。 'distinct'标志是您实际需要使用的标志,但是如果您看到我和@eigenchris之间的对话,我们就无法使其工作。尽管如此,eigenchris已经证明这比 blockproc 快300倍。

One other method I can suggest is to use combination of colfilt with the 'sliding' flag and mean as the function to operate on. The 'distinct' flag is what you actually need to use, but if you see the conversation between myself and @eigenchris, we couldn't get it to work. Still, eigenchris has shown that this is 300x faster than blockproc.

因此,假设您的图像存储在 im 中,您只需执行以下操作:

Therefore, assuming your image is stored in im, you can simply do:

out = uint8(colfilt(im, [2 2], 'sliding', @mean));
out2 = out(1:2:end,1:2:end);

你需要对结果进行二次抽样的原因是因为当我们申请滑动选项,您有一次重叠块处理图像。因为您需要不同的块,所以您只需要1/4的图像,因为您已将图像分解为2 x 2块。在一个2 x 2的块中,如果你使用了滑动选项,你将得到三个不需要的其他结果,因此上面的子采样两倍会消除那些产生结果的其他三个块。

The reason why you would need to subsample the results is because when we apply a sliding option, you have overlapping blocks processing the image at a time. Because you want distinct blocks, you only need 1/4 of the image because you have decomposed the image into 2 x 2 blocks. Within a 2 x 2 block, if you did the sliding option, you would have three other results that are not required, and so doing the subsampling above by a factor of two eliminates those three other blocks that give results.

请注意,您需要转换结果,因为输出的类型为 double

Note that you'll need to cast the result as the output will be of type double.

在我和eigenchris之间进行讨论时,您可以使用规范 colfilt =http://www.mathworks.com/help/images/ref/imfilter.html\"rel =nofollow> imfilter 复制第一个上面代码的行。在将两者结合在一起时,我设法在我的机器上获得了8倍的加速。因此:

Going with the discussion between myself and eigenchris, you can replace colfilt with the canonical imfilter to replicate the first line of the above code. I managed to get an 8x speedup on my machine when comparing the two together. Therefore:

out = imfilter(im, [0.25 0.25; 0.25 0.25], 'replicate');
out2 = out(1:2:end,1:2:end);

就加速而言,我将每个调用包装在一个匿名函数中,然后使用 timeit 为这些功能计时: / p>

In terms of speedup, I wrapped each call in an anonymous function, then used timeit to time the functions:

>> f = @() uint8(colfilt(im, [2 2], 'sliding', @mean));
>> g = @() imfilter(im, [0.25 0.25; 0.25 0.25], 'replicate');
>> T = timeit(f);
>> T2 = timeit(g);
>> T/T2

ans = 

7.5421

正如你所看到的,大约有8倍的加速比 colfilt ...很可能是因为它正在调用 im2col 并且引擎盖下 col2im

As you can see, there is roughly a 8x speedup over colfilt... most likely because it's calling im2col and col2im under the hood.

这篇关于如何取2x2像素块的平均值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆