15,964,471 members
2.00/5 (1 vote)
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
Updated 19-Apr-12 21:45pm
v2
Sergey Alexandrovich Kryukov 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

## 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?

## 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.

## 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
 Papy@007 35 Daniel Davis 2024 10 Pete O'Hanlon 10 merano99 10 RAMASWAMY EKAMBARAM 10
 OriginalGriff 308 Pete O'Hanlon 170 Dave Kreskowiak 145 merano99 90 Andre Oosthuizen 55

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900