Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ 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
Posted 19-Apr-12 18:28pm
zarunz335
Edited 19-Apr-12 22:45pm
CPallini328.8K
v2
Comments
SAKryukov at 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: bad
good
Please Sign up or sign in to vote.

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[^]
  Permalink  
Comments
zarunz at 20-Apr-12 2:30am
   
what should be written in TakeImage function??
jk chan at 20-Apr-12 4:05am
   
you can't take a 2x2 matrix from an array ?
zarunz at 23-Apr-12 23:43pm
   
yes, but what is the 3rd and 4th argument in the function for???
jk chan at 24-Apr-12 0:41am
   
i was just showing we are taking2x2 matrix. you can see it as the order of matrix.
zarunz at 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 at 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: bad
good
Please Sign up or sign in to vote.

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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

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

  Print Answers RSS
0 OriginalGriff 245
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 Afzaal Ahmad Zeeshan 120
4 DamithSL 115
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 20 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100