2

I need to collect some data from an image. Looping should be done with a mask.

For example, I have a simple cross mask:

 1 1 1 1 1 

And I need to know the sum of gray value of every point of Image.

I can use simple loops, like this:

// looping except first and last int nr = image.rows-1; int nc = image.cols-1; for (int j=1; j<nr; j++) { // for all rows const uchar* previous = image.ptr<const uchar>(j-1); // previous row const uchar* current = image.ptr<const uchar>(j); // current row const uchar* next = image.ptr<const uchar>(j+1); // next row for (int i=1; i<nc; i++) { sum = previos[i] + current[i] + current[i-1] + current[i+1] + next[i]; } } 

But I think I do this wrong. May be I should use something like cv::Mat kernel()?

I need mask to be a parameter, so I can use any kind of mask.

Is there a ready function for looping an image with a mask? ( There is filter2D function, but I don't need to make changes with an images, only some calculations with pixels ).

2
  • what do you want to do with the sum for each pixel? Commented Apr 5, 2012 at 20:33
  • here is simple example. Really, I need to calc some formula with every sum, for example to apply SUSAN detection algorithm. Commented Apr 5, 2012 at 20:37

1 Answer 1

2

If you want the sum for each pixel, isn't that exactly what filter2d() does? You compute the sums for each pixel, and then use these sums to go on with SUSAN: (untested code)

cv::Mat img; // TODO: load img cv::Mat kernel = cv::Mat::ones(3,3,CV_8U); // TODO: set some elements to zero you don't like cv::Mat sums = img.clone(); cv::filter2d(img, sums, -1, kernel); // TODO: use sums for further computation 

What happens at the edges of your image depends on the border extrapolation type you specify for filter2d. From the docs:

Another common feature of the functions and classes described in this section is that, unlike simple arithmetic functions, they need to extrapolate values of some non-existing pixels. For example, if you want to smooth an image using a Gaussian 3 \times 3 filter, then, when processing the left-most pixels in each row, you need pixels to the left of them, that is, outside of the image. You can let these pixels be the same as the left-most image pixels (“replicated border” extrapolation method), or assume that all the non-existing pixels are zeros (“constant border” extrapolation method), and so on. OpenCV enables you to specify the extrapolation method. For details, see the function borderInterpolate() and discussion of the borderType parameter in the section and various functions below.

/* Various border types, image boundaries are denoted with '|' * BORDER_REPLICATE: aaaaaa|abcdefgh|hhhhhhh * BORDER_REFLECT: fedcba|abcdefgh|hgfedcb * BORDER_REFLECT_101: gfedcb|abcdefgh|gfedcba * BORDER_WRAP: cdefgh|abcdefgh|abcdefg * BORDER_CONSTANT: iiiiii|abcdefgh|iiiiiii with some specified 'i' */ 
Sign up to request clarification or add additional context in comments.

4 Comments

So, if the img is 10x10=100 pixels, and we loop throug it without first and last rows and cols ( 8x8 = 64 pixels as nucleuses of kernel) : would I have sums matrix of 64 elements (sum of every kernel)?
@Innuendo : that would depend on the border extrapolation method you specify for filter2d(). See my edit.
can you suggest e any link that defines about the working of borderInterpolate function in opencv? I need this urgently.Please help me. Thnaks in advance.
@jhamb that's an OpenSource library. You could just look up the source code yourself. If you have a specific problem implementing this yourself, why don't you just post a question on this site?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.