Ask Your Question
0

Segmentation fault in ~ImageCodecInitializer() while exiting main()

asked 2017-02-28 01:42:44 -0600

Shrey gravatar image

updated 2017-02-28 06:20:19 -0600

I am trying to implement a median filter(for practice). The program showed the output image correctly but it ended with a segmentation fault. The following error occurred just before exiting main(){}

Program received signal SIGSEGV, Segmentation fault. 0x00007ffff78bf789 in cv::ImageCodecInitializer::~ImageCodecInitializer() () from /usr/local/lib/libopencv_imgcodecs.so.3.2

//mainFile.cpp
 #include <opencv2/highgui/highgui.h
 #include <opencv2/core/core.hpp>
 #include "lowp.h"
 //#include <stdlib.h>
 using namespace cv;

 void medFilter(Mat &, Mat &, int);
 int main(){
 Mat img = imread("home.jpg",1);
 Mat out;
 medFilter(img,out,7);
 namedWindow("Example");
 imshow("Example", out);
 waitKey(0);
 destroyWindow("Example");
  }



    //lowp.h
 void medFilter(Mat &img, Mat &out, int size){
      Mat low(img.rows, img.cols, img.type());
      int r = img.rows;
      int c = img.cols;
      const int ch = img.channels();
      int hist[256];   //changed from int hist[255] -- as correctly suggested by LBerger
      int w = 0;
      int *win;
      win = new int(size*size);
      for(int h = 0; h<255; h++){
                  hist[h] = 0;
      }
      for(int i = 0; i<r; i++){
                   for(int j = 0; j<c; j++){
                        for(int chi = 0; chi < ch; chi++){
                                  if(i>=size/2 && i<r-size/2 && j>=size/2 && j<c-size/2){
                                           for(int u = -size/2 ; u<=size/2; u++){
                                                 for(int v = -size/2; v<=size/2; v++){
                                                          win[w] = *(img.ptr<uchar>(i + u) + (j + v)*ch + chi);
                                                          w++;
                                                   }
                                           }
                                          w = 0;
                                         *(low.ptr<uchar>(i) + j*ch + chi) = getMedian(win, hist, (size*size));
                                  }else{
                                         *(low.ptr<uchar>(i) + j*ch + chi) = *(img.ptr<uchar>(i) + j*ch + chi);
                                  }
                         }
                 }
          }
          out = low;
  }

   uchar  getMedian(int* w, int* hist, int size){
        uchar h;
        for(int i = 0; i<size; i++){
              hist[w[i]] ++;
         }
          int sum = 0;
          for(h = 0; h<255; h++){
                  sum += hist[h];
                  if(sum>size/2) break;
          }
         for(int i = 0; i<size; i++){
                  hist[w[i]] = 0;
         }
         return h;
  }
edit retag flag offensive close merge delete

Comments

there is a buffer overrun somewhere in your code. now learn to use a debugger, please.

berak gravatar imageberak ( 2017-02-28 01:56:58 -0600 )edit

berak.. I used gdb: The program executed well till line no. 16; after which just before exiting the main function gave a dump.

Shrey gravatar imageShrey ( 2017-02-28 02:05:17 -0600 )edit

good !

admittedly, this will be a hard nut.

berak gravatar imageberak ( 2017-02-28 02:15:04 -0600 )edit

Can you delete line number?

LBerger gravatar imageLBerger ( 2017-02-28 03:33:08 -0600 )edit

LBerger: Line numbers are removed.

Shrey gravatar imageShrey ( 2017-02-28 05:44:38 -0600 )edit

berak- The error is happening in the destructor: cv::ImageCodecInitializer::~ImageCodecInitializer(). Which is OpenCV Library code. Cant seem to find whats going on.

Shrey gravatar imageShrey ( 2017-02-28 05:56:41 -0600 )edit
1

no, the error is caused by a buffer overrun somewhere in your code.

berak gravatar imageberak ( 2017-02-28 06:06:12 -0600 )edit
1

hist[255] is wrong it should be hist[256] ... Now your program is not really nice Ok it is an exercise. First in your histogram you can use that is a moving window...

LBerger gravatar imageLBerger ( 2017-02-28 06:06:48 -0600 )edit

LBerger: hist[255] has been changed to hist[256] as you have correctly suggested. Problem still persists though.

Shrey gravatar imageShrey ( 2017-02-28 06:26:06 -0600 )edit

berak: I have debugged the code. My custom function medFilter works correctly. imshow displays the output which is correct. Error is thrown at the closing curly bracket of the main() function.

Shrey gravatar imageShrey ( 2017-02-28 06:33:41 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2017-02-28 10:06:58 -0600

LBerger gravatar image

What does “new int(100)” do? basic C++ error :

edit flag offensive delete link more

Comments

Thanks LBerger: This solved it.

Shrey gravatar imageShrey ( 2017-03-01 22:27:17 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-02-28 01:39:29 -0600

Seen: 218 times

Last updated: Feb 28 '17