Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To be clear, you want to calculate the difference between the center pixel, and the 8 surrounding pixels, pick the minimum difference, and store that?

The easiest way is to initialize a Mat the size of your image with MAX_VALUE, let's call it result.

Create different views of your image using the example cv::Mat view = image(cv::Rect(x,y,width,height));

Subtract (or absdiff, if appropriate) the two views, storing it in a temporary variable, then do result = cv::min(result, temp);

So as an example, this would be the first of 8, doing (x-1, y-1) against (x, y).

cv::Mat result(image.rows, image.cols, CV_?);
result.setTo(MAX_VALUE_OF_CV_?);//Whatever your data type is.
cv::Mat temp, center, offset;

//This is the views for this offset. Note that the size is one smaller in both x and y, because you //can't calculate the difference between (0,0) and (-1,-1).
cv::Rect centerRect = cv::Rect(1,1,image.cols-1,image.rows-1);
cv::Rect offsetRect = cv::Rect(0,0,image.cols-1,image.rows-1);

center = image(centerRect);
offset = image(offsetRect);
cv::absdiff(center, offset, temp);  //Do the actual work.

//Note then that the results are only valid for centerRect too.  
//Once you've done this for all 8, result contains what you want.
result(centerRect) = cv::min(result(centerRect), temp);