Ask Your Question
0

corrupted double-linked list

asked 2013-05-21 23:56:42 -0500

lyw8120 gravatar image

updated 2013-05-23 03:02:55 -0500

I have a problem which probably is the memory problem, but I do not where is error and how to solve it. the code is followed.

when removed the two lines code

mv_background_average(image); mv_background_rotation(image);

it can work, I still can not find the bug.

please help me to check it and tell me what happened. Thanks a lot.

int main (int argc, char * argv[])
{
   string filename = string(argv[1]);
   Mat image = imread(filename.c_str(),0);
   mv_background_average(image);
   mv_background_rotation(image);
   int r = image.rows;
   int c = image.cols;
   int k=0; 
   Mat_<uchar> dst(r,c);
   for (int i=3; i<r-3; ++i)
     for (int j=3; j<c-3; ++j)
      {
        int m = i-2;
        int n = j-2;
        double maxval;
        Rect roi(m,n,5,5);

       Mat temp(image,roi);
       minMaxLoc(temp,0,&maxval,0,0);
          if ((maxval >= 100) && (image.at<uchar>(i,j) == maxval))
            {
               dst(i,j) == 255;
               cout << temp << " ";
               k++;
            }
       }
        cout << k <<endl;
  return 0;
  }

void mv_background_average(Mat & img)
{
    int r = img.rows;
    int c = img.cols;
    int length =  r/4;
    int r2 =  3*r/8;
    int c2 =  3*c/8;

    Scalar mean, stddev;
    Mat RoiA(img,Rect(r2,c2,length,length));
    meanStdDev(RoiA,mean,stddev);
    float m = mean.val[0];
    float std = stddev.val[0];
    int threshold = ceil(m + 2 * std);

    for (int i = 0; i<r ; ++i)
     { 
      ushort *  data = img.ptr<ushort>(i);
      for (int j = 0; j<c; ++j)
         {
             if(data[j] <= threshold) { data[j] = 0;}
         }
     }
}

void mv_background_rotation(Mat & img)
{
    int r = img.rows;
    int c = img.cols;
    int max_radius = 0;
    int radius = 0;

    r > c ? max_radius = c/2 : max_radius = r/2;

    Mat rotation_sum(max_radius,3,CV_32FC1,Scalar::all(0));

   for (int i = 0; i< r; ++i)
   {
       ushort * data = img.ptr<ushort>(i);
       for (int j = 0; j<c; ++j)
          {
             radius = round(sqrt((i-r/2)*(i-c/2) + (j-r/2)*(j-c/2)));
             if (radius <= max_radius)
              { 
                rotation_sum.at<float>(radius,0) += data[j];
                rotation_sum.at<float>(radius,1) += 1;
              }

          }
   }



  for (int i=0; i<=max_radius; ++i)
  {
     rotation_sum.at<float>(i,2) = rotation_sum.at<float>(i,0) / rotation_sum.at<float>(i,1);
  }    

  for (int i = 0; i< r; ++i)
   {
       ushort * data = img.ptr<ushort>(i);
       for (int j = 0; j<c; ++j)
          {
             radius = round(sqrt((i-r/2)*(i-c/2) + (j-r/2)*(j-c/2)));
             int mean = ceil(rotation_sum.at<float>(radius,2));
             if (radius <= max_radius && data[j] <= 4*mean)
              { 
                data[j] = 0;
              }

          }
   }

}

image description

edit retag flag offensive close merge delete

Comments

unfortunately, you don't tell us, what mv_background_average or mv_background_rotation do ;(

berak gravatar imageberak ( 2013-05-22 01:17:08 -0500 )edit

they are try to remove the background of image. their definitions show as follow: void mv_background_aveage(Mat & img) void mv_background_rotation(Mat & img)

lyw8120 gravatar imagelyw8120 ( 2013-05-22 07:24:20 -0500 )edit

again, since something seems to go wrong there, noone can help you without looking at the code.

where's the "double linked list", your title refers to ?

( also, you can use the "01101" button to format code properly )

berak gravatar imageberak ( 2013-05-22 07:31:06 -0500 )edit

I have uploaded my code, and hope this can help you find the bug. Thanks

lyw8120 gravatar imagelyw8120 ( 2013-05-22 12:39:51 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-05-22 12:57:25 -0500

berak gravatar image

updated 2013-05-22 13:05:10 -0500

thanks for editing, definitely helpful.

i found 2 buffer overflows there, both the same kind:

you load your img as grayscale, 8 bit, then you pass it to mv_background_average(), and there access it as ushort:

ushort *  data = img.ptr<ushort>(i); 
// since a ushort is twice as big as a uchar, you'll get wrong values *and* overflow

make it :

uchar *  data = img.ptr<uchar>(i);

instead.

same for the similar call in mv_background_rotation()

( and where's the "double-linked list" ? )

edit flag offensive delete link more

Comments

there is only the memory problem( type problem as you said) and thank you very much. This error let me understood type deeply.

By the way, you know why the result image is rotated to compare the original image when I processed in the main function (the main function above, but the problem is here, I think the problem about the variables "i", "j", "m", "n")?

lyw8120 gravatar imagelyw8120 ( 2013-05-23 03:04:03 -0500 )edit

hmm, puzzled , myself here ;)

your Rect's / ROI's look strange..

Rect roi(m,n,5,5); // m is "vertical", n is "horizontal". is that intended, or an error ?

Mat RoiA(img,Rect(r2,c2,length,length)); // same here

it's mostly row,col in opencv, but it's Rect(x,y,w,h)

berak gravatar imageberak ( 2013-05-23 03:53:48 -0500 )edit

m represent horizontal and n for vertical (m = i -2; n = j-2), while i will go through image rows and j for image cols. That also puzzled me for a day. anyway, thank you.

lyw8120 gravatar imagelyw8120 ( 2013-05-23 05:18:50 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-05-21 23:56:42 -0500

Seen: 1,338 times

Last updated: May 23 '13