13,047,131 members (60,319 online)
Rate this:
See more:
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
Posted 19-Apr-12 17:28pm
zarunz335
Updated 19-Apr-12 21:45pm
CPallini404.4K
v2
SAKryukov 20-Apr-12 0:14am

Why OpenCV? What's the problem? You don't know how to address pixels? Work with pointers, arrays? calculate mean values? What?
--SA

Rate this:

## Solution 1

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[^]
zarunz 20-Apr-12 2:30am

what should be written in TakeImage function??
jk chan 20-Apr-12 4:05am

you can't take a 2x2 matrix from an array ?
zarunz 23-Apr-12 23:43pm

yes, but what is the 3rd and 4th argument in the function for???
jk chan 24-Apr-12 0:41am

i was just showing we are taking2x2 matrix. you can see it as the order of matrix.
zarunz 24-Apr-12 4:22am

I wrote the Takeimage function so that it returns a 2X2 matrix. But where should be i and j used?
zarunz 24-Apr-12 4:24am

This is what i wrote in the TakeImage function

CvMat TakeImage(IplImage *input2)
{
CvMat *mat = cvCreateMat(input2->height,input2->width,CV_32FC3 );//CV_32FC1
cvConvert( input2, mat );
return mat;
}
Rate this:

## Solution 2

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

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.
Rate this:

## Solution 3

You may use standard `GDI` functions for that, see Using Bitmaps (Windows)[^] for code samples.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Graeme_Grant 142 Kornfeld Eliyahu Peter 110 Richard MacCutchan 104 Satya Prakash Swain 79 OriginalGriff 65
 OriginalGriff 4,868 RickZeeland 1,864 ppolymorphe 1,748 F-ES Sitecore 1,553 Dave Kreskowiak 1,379

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 20 Apr 2012