Segmentation fault (core dumped) image processing c++/opencv

asked 2016-04-03 07:26:02 -0500

choukri el gravatar image

I want to apply a smooth to an image,for that purpose I create the function "sum" Here's my code

double sum(cv::Mat img, int startedX, int startedY, int w) {
double res = 0;
std::cout << "startedX= " << startedX<< "/ startedY= " << startedY<<  std::endl; 
for (int i = startedX - ((w - 1) / 2); i < startedX + ((w + 1) / 2); i++) {
  std::cout << "i= " << i <<  std::endl;    
    for (int j = startedY - ((w - 1) / 2) ; j < startedY + ((w + 1) / 2); j++) {std::cout << " j= " <<j<<  std::endl;
     if ((i >= 0 && i < img.size().width) && (j >= 0  && j < img.size().height)) {      
            res += img.at<float>(i,j); }}}
              return res;}

This is a portion of my main function where the program crashes :

for (int k = 0; k < imgSource.size().width; k++) {
 for (int l = 0; l < imgSource.size().height; l++) {
        if( (k >= 0 && k < imgSource.size().width) && (l >= 0  && l < imgSource.size().height) ) {
      std::cout << "k= "<<k <<"  l= "<<l<< " wmax= "<< wmax<<std::endl; 
      newImgMax.at<float>(k,l) =(float)sum(imgMax,k,l, wmax) / (wmax * wmax);
       }}}

for k=0 and l=0 the program works fine and returns a value but for k=0 and l=1 the call of the function(sum(imgMax,0,1,1)) doesn't pass,it crashes and shows a segmentation fault core dumped.This is my output

k= 0  l= 0 wmax= 1
startedX= 0/ startedY= 0
i= 0
j= 0
k= 0  l= 1 wmax= 1
Segmentation fault (core dumped)
edit retag flag offensive close merge delete

Comments

2
LBerger gravatar imageLBerger ( 2016-04-03 13:49:28 -0500 )edit
1

don't reinvent the wheel: if you want to apply a custom smooth kernel, do it with the already optimized OpenCV functions. And always remember that per pixel loops are a terrible idea

LorenaGdL gravatar imageLorenaGdL ( 2016-04-03 16:46:28 -0500 )edit

After several tests I found that the call to the function "sum" is done only once independently of indexes(rows,cols) for example when I try :

std::cout << " (1,0)= " <<sum(imgMax,1, 0, wmax) <<std::endl;

it returns a value and when I try :

std::cout << " (2,3)= " <<sum(imgMax,2, 3, wmax) <<std::endl;
std::cout << " (1,0)= " <<sum(imgMax,1, 0, wmax) <<std::endl;

it returns the result of the first and crashes after any suggestions please ?

choukri el gravatar imagechoukri el ( 2016-04-03 18:13:29 -0500 )edit
1

As @LorenaGdL said you should use opencv function to make a convolution. Except if your problem is an exercice about loop.

LBerger gravatar imageLBerger ( 2016-04-04 02:18:27 -0500 )edit

@choukri el please follow the guidance given ... you are reinventing the sole purpose of libraries like OpenCV, which is supplying general computer vision functionality. Smoothing is need in almost all cases, so it already exists and it would be stupid to create your own pixel wise operations...

StevenPuttemans gravatar imageStevenPuttemans ( 2016-04-04 03:28:52 -0500 )edit
1

okay I will see these libraries, thank you for your advices and your replies @LorenaGdL@LBerger@StevenPuttemans

choukri el gravatar imagechoukri el ( 2016-04-04 03:55:44 -0500 )edit