如何分割图像 [英] how to partition an image

查看:68
本文介绍了如何分割图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将图像划分为2 x 2像素块,然后计算所有块均值.图像大小为320 X240.如何在opencv中执行此操作.请提供答案

I have to partition an image into 2 by 2 pixel blocks and compute all the block means. The image size is 320 X 240. How can i do this in opencv. Please provide an answer

推荐答案

,您不需要openCV.您可以将图像视为二维矩阵.那么从中轻松取2X2块对吧?计算的意思也很容易吧?

for(int i = 0; i< imageheight; i + = 2)//忽略图像边界.
{
for(int j = 0; j< imagewidth; j + = 2)
{
PixBlock = TakeImage(ImageData,i,j,2,2),说TakeImage函数返回2x2矩阵
平均值= ComputeMean(PixBlock,2,2)

}
}

jkchan
http://cgmath.blogspot.com [ ^ ]
you don''t need openCV for that. you can consider image a 2-D matrix. Then it easy to take 2X2 blocks from it right ? computing mean also will be easy right ?

for( int i =0; i < imageheight; i+=2) // ignoring the image boundaries.
{
for( int j = 0; j < imagewidth; j+=2 )
{
PixBlock = TakeImage(ImageData,i,j,2,2) , say TakeImage function returns a 2x2 matrix
mean = ComputeMean(PixBlock,2,2)

}
}

jkchan
http://cgmath.blogspot.com[^]


您实际上可以在没有OpenCV的情况下进行操作.计算4个像素以上的平均值并不困难.但是,使用OpenCV函数可能比代码更方便,并且性能可能比代码更好(取决于您对自己的代码的优化程度).在许多情况下,OpenCV都进行了很好的优化.

使用相应的OpenCV功能很容易. OpenCV文档包含以下示例:

You can in fact do it without OpenCV. Calculating the average over 4 pixels is not that hard of a task. But, using an OpenCV function might be more convenient and the performance might be better than that of your code (depending on how well you optimized your own code). OpenCV is pretty well optimized in many cases.

Using the corresponding OpenCV function is easy. The OpenCV documentation contains the following example:

resize (src, dst, Size(), 0.5, 0.5, interpolation);



请注意,由于要根据源图像和两个比例因子来计算目标图像的大小,因此忽略了Size()参数.

您希望选择INTER_AREA作为插值参数的值,然后进行平均.但是请注意,如果要解决比例因子在某个范围内变化的更一般情况,则其他插值模式(例如INTER_LINEAR或INTER_CUBIC)可能会提供更好的结果.

如果这是家庭作业,我建议您也编写该功能的自己的版本.对于图像处理的基础知识来说,这确实是一个很好的练习.希望这里的一些小提示可以帮助您正确地开始.



Note that the Size() argument is being ignored, as the size of the destination image is being calculated from the source image and the two scale factors.

As value of the interpolation argument you want to choose INTER_AREA, which does the averaging. Note, however that if you want to solve a more general case in which the scale factor varies over a range, other interpolation modes like INTER_LINEAR or INTER_CUBIC might give better results.

If this is homework, I would suggest that you also write your own version of that function. It is a really good exercise for the basics in image processing. Hope this little hint here gets you started on the right path.


您可以为此使用标准的GDI函数,请参见
You may use standard GDI functions for that, see Using Bitmaps (Windows)[^] for code samples.


这篇关于如何分割图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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