Ask Your Question

Segmentation fault when writing to subranges of Mat

asked 2014-06-09 10:09:18 -0500

benniz gravatar image

Hi there,

I'm new to OpenCV a have some trouble regarding writing to a subrange of a Mat-Object.

The code below iterates a given Image. For each pixel, it takes pixel within a range of 5x5, finds the brightest pixel, and put all other pixel to 0.

I call the function multiple times. After a random number of calls the function gives me a segmentation fault or "malloc memory corruption". Sometimes I can call the function 10 times with no problems sometimes only twice, then the program stops.

I tracked down the problem to the line, where I write to the original image using the subimage.<uchar>(rowSubimage,colSubimage) = 0;

There is the function that drives me crazy:

void findMaxAndBlackout(Mat& image, int size){

Point centralPoint;
Size rangeSize = Size(size,size);
Mat subimage;
Rect range;

// iterate the image
for(int row = 0; row <= image.rows-size; row++){
    for(int col = 0; col <= image.cols-size; col++){
        centralPoint = Point(col,row);
        range = Rect(centralPoint, rangeSize);

            // slice submatrix and find max
        subimage = image(range);
        double max;
        minMaxLoc( subimage, NULL, &max, NULL, NULL );

            // iterate the surrounding 
        for(int rowSubimage  = 0; rowSubimage <= subimage.rows; rowSubimage++){
            for(int colSubimage = 0; colSubimage <= subimage.cols; colSubimage++){
                if(<uchar>(rowSubimage,colSubimage) < max){
                    //this line cause the trouble
          <uchar>(rowSubimage,colSubimage) = 0;

The Mat-Object is generated using:

Mat houghImage = imread("small_schachbrett1_cam.png", CV_LOAD_IMAGE_GRAYSCALE);

Please help me understand the problem.

If you know a better or more efficient way to achieve the same result please let me know. I am open for any improvements

Regards benniz

edit retag flag offensive close merge delete



I think the first two loops should be:

or(int row = size; row <= image.rows-size; row++){ for(int col = size; col <= image.cols-size; col++){

GilLevi gravatar imageGilLevi ( 2014-06-09 16:28:01 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-06-09 23:28:54 -0500

Haris gravatar image

So basically your code will loop over the image using specified block, find out the local maxima and set all pixel to 0 other than the maxima.

First thing to take care that, you are modifying the source itself on each loop (making the surrounding zero) and you lost the original pixel data for next iteration.

So create a blank image same as source,

Mat dst(src.size(),CV_8UC1,Scalar(0));

Now inside loop find minmaxLoc, note that here you will get the max pixel value as well as the co-ordinates, you have to use that.

    subimage = image(range);
    subimageDest = dst(range); //Set ROI on destination image
    double max;
    Point maxLoc;
    minMaxLoc( subimage, NULL, &max, NULL, &maxLoc );

Now Set the maxima on dst image,<uchar>(maxLoc.y,maxLoc.x) =max;

Note: the code is not tested.!!

edit flag offensive delete link more

Question Tools


Asked: 2014-06-09 10:09:18 -0500

Seen: 898 times

Last updated: Jun 09 '14