Ask Your Question
0

Segmentation fault(core dumped) in log transformation

asked 2017-03-26 09:47:17 -0600

Tom James gravatar image

I am a beginner of digital image processing. And I want to conduct log transformation for each pixel of the input picture. But, I encountered a segmentation fault(core dumped error). I don't know where does the error exists. Can you help me? For some pictures, like lena.jpg, the error didn't happen, while it happens when I input a picture of a hand-writing on a paper. Here is my code:

Mat Log_Trans(Mat);

int main(int argc,char**argv)
{
      Mat m1=imread(argv[1]);
      if ( m1.empty() )
           {
                  cout<< "Could not open file" <<endl;
                  return ( 1 );
           }
     imshow("Input",m1) ;
     m1=Log_Trans(m1);
     imshow("Log",m1);
    waitKey(0);
    return 1;
}

 Mat Log_Trans(Mat input)
{
       Mat output(input.cols+1,input.rows+1,CV_8UC3,Scalar(0,0,0));
       int i,j;
       for(i=0;i<=input.rows;i++)
             {
                      for(j=0;j<=input.cols;j++)
                          {
                                output.at<Vec3b>(j,i)[0]=(int)log(1+input.at<Vec3b>(j,i)[0]);
                                output.at<Vec3b>(j,i)[1]=(int)log(1+input.at<Vec3b>(j,i)[1]);
                                output.at<Vec3b>(j,i)[2]=(int)log(1+input.at<Vec3b>(j,i)[2]);
                          }
             }
         normalize(output,output,0,255,NORM_MINMAX);
         return output;



}
edit retag flag offensive close merge delete

Comments

1

It is not Mat output(input.cols+1,input.rows+1,CV_8UC3,Scalar(0,0,0)); but Mat output(input.rows+1,input.cols+1,CV_8UC3,Scalar(0,0,0));

LBerger gravatar imageLBerger ( 2017-03-26 10:04:09 -0600 )edit

I modified my code as you said, and it works. But there is something I still can not understand: Isn't the rows denoting the number of pixels in a column or the number of rows the Matrix has? And is the First parameter of a Mat class when initializing it represents the numbers of columns?

Tom James gravatar imageTom James ( 2017-03-27 09:36:16 -0600 )edit

Mat(numRows, numCols, type) -- is that, what you meant ? rows counts "vertically", or in y direction, cols counts "horizontal", or in x direction

berak gravatar imageberak ( 2017-03-27 09:44:01 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-03-27 01:06:33 -0600

berak gravatar image

updated 2017-03-27 02:53:24 -0600

please DO NOT WRITE FOR LOOPS like that, ever , with opencv.

  • you can only swap i,j safely IF your image is quadratic
  • j<=input.cols; is out of bounds, same for rows
  • applying log() only makes sense for float data

it should be rewritten as:

Mat input = ...
Mat tmp, output;
input.convertTo(tmp, CV_32F, 1.0, 1.0); // to float, also add 1
cv::log(tmp.t(), output);   // log on transposed Mat
normalize(output,output,0,255,NORM_MINMAX, CV_8U); // back to uchar
edit flag offensive delete link more

Comments

Thank you for pointing it out. But what is a quadratic image?

Tom James gravatar imageTom James ( 2017-03-27 09:54:07 -0600 )edit

rows == cols, like 256x256 (like the lena image)

berak gravatar imageberak ( 2017-03-27 09:58:00 -0600 )edit
1

oh! I understood, I confused the rows and the cols. It works when the picture when it is quadratic like lena.jpg. However, it doesn't work when the length isn't as large as width, like the picture of a handwriting. Thank you for your answer~

Tom James gravatar imageTom James ( 2017-03-28 08:57:51 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-03-26 09:47:17 -0600

Seen: 842 times

Last updated: Mar 27 '17