Ask Your Question
0

Make 32x32 sections on an image in C++ OpenCV?

asked 2018-03-14 18:26:17 -0600

Im trying to divide a passed in, gray scaled image and divide it into 32x32 "blocks" or "sections". Sort of like an imaginary, overlay grid.

Once this is done, I need to loop through each pixel in each individual section to perform analysis on the single channel, gray scale value that is returned by each pixel.

For example:

// For region (x,y)

if pixelVal >= 120 && pixelVal <= 130{

bitStream += "1"; }

else if pixelVal >= 135 && pixelVal <= 140{

bitstream += "0"; }

else{

bitStream += "X"; }

Does this make sense? I'm very new to OpenCV and C++ and basically I know how to show, loop through, and return the pixel value at a given point, just not within a certain, predetermined area. Im thinking about creating some sort of window that loops through? I just have no idea how to code it.

edit retag flag offensive close merge delete

Comments

no, it does not quite make sense to me.

if you need an analysis per pixel, why the blocks then ?

if you need an analysis per block, why the pixels then ?

what are you trying to achieve here ? what's the context ?

and no, you should never iterate over pixels for anything, in opencv. there are always higher-level functions, that should be used instead.

berak gravatar imageberak ( 2018-03-15 04:00:31 -0600 )edit
1

1."why the blocks then?": Im going to take an average pixel count in the respective blocks. So say block (4,16) is averaging 210. 2. "why the pixels then": Analysis per block is necessary for error correcting methods later on. 3. "what are you trying to achieve here": It's a cryptographic key generator that Im basing off of a passed in image. If a block fits certain conditions, like the ones I laid out above, it's counted as a 1, 0, or X.

TopRightCheese gravatar imageTopRightCheese ( 2018-03-15 13:15:03 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-03-15 13:41:06 -0600

berak gravatar image

updated 2018-03-15 13:46:43 -0600

from comments above: " Im going to take an average pixel count in the respective blocks"

ah, easy answer, then, - you want to iterate over blocks, not pixels, then, and take the average of the whole block at a time:

Mat image = ... // for simplicity, let's assume a single channel , grayscale one
for (int y=0; y<image.cols-32; y+=32) {
    for (int x=0; x<image.rows-32; x+=32) {
        // get the average for the whole 32x32 block:
        Rect roi(x,y,32,32);
        Scalar mean,dev;
        meanStdDev(image(roi), mean, dev);
        // mean[0] is the mean of the first channel. do whatever you want with it
    }
}

have a look at the docs, too, please

edit flag offensive delete link more

Comments

This answer is fantastic! My mistake on the last comment. I didn't use mean[0].....which brings me to my next question. Is there any ability to just pass in the single channel value without having the 3 channels?

TopRightCheese gravatar imageTopRightCheese ( 2018-03-15 15:20:17 -0600 )edit

what ? ifthere's more than one channel, use mean[1], etc,

(i did not understand your comment)

berak gravatar imageberak ( 2018-03-15 16:06:18 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-03-14 18:26:17 -0600

Seen: 1,391 times

Last updated: Mar 15 '18