Applying filter keeps crashing? [closed]

asked 2015-11-04 17:10:24 -0600

215 gravatar image

updated 2015-11-06 12:33:56 -0600

I am bit unsure why this keep crashing.. Qt isn't providing me with an proper error message, but just states that it crashed.. I seems to me that the program crashes when I try to add the pixel values together.

here is the filter.

void filter(Mat src, Mat dst, int kernel, double P)
{
  cout << "got it" << endl;
    Mat temp = src.clone();
    Mat dst111 = src.clone();
    copyMakeBorder(temp,temp,kernel-1,kernel-1,kernel-1,kernel-1,BORDER_CONSTANT,Scalar(0,0,0));

    for(int row = kernel/2; row < temp.rows - kernel/2-1; row++)
    {
        for(int col = kernel/2; col < temp.cols - kernel/2-1; col++)
        {

        double den=0,num=0;
          for(int i = -(kernel/2); i <= (kernel/2) ; i++)
          {
            for(int j = -(kernel/2) ; j <= (kernel/2); j++)
            {
                den += pow(temp.at<double>(row+i,col+j),P);
                num += pow(temp.at<double>(row+i,col+j),P+1);
                //cout <<"Row: "<<row+i << " " << "col: "<< col + j << endl;
            }
          }

        //cout << num/den << endl;
        double value = num/den;
        dst.at<double>(row,col) = value;
      }
    }
    cout << "done " << endl;
    dst.convertTo(dst,CV_8U);
    imshow("hey",dst111);
    waitKey();
}

--UPDATE-- I changed some few things, based on the comments. It seems now that nothings happens to the image, when the filter is applied to it. The output image is the same image, with some lines on it..

ex. image description

---Update--- Using the code snippet posted below things the output image becomes this

image description

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-10-07 06:48:53.401162

Comments

Well.. i padded the borders.. So, each has 2 rows and coloum of zeroes extra. So i should start my outer for loop at 2,2 (kernel-1, kernel -1) It still crashes at 3,3 => when it has to input the value at output.

215 gravatar image215 ( 2015-11-04 17:41:06 -0600 )edit

couple more things wrong here:

  • for(int row = kernel/2; row < temp.rows-kernel/2; row++) , same for cols
  • temp.at<int> is sure the wrong type
  • output.at<uchar> you will need a float Mat, else either pow() will overflow, and you get rounding errors
  • floor(kernel/2) since kernel is int, floor has no effect
berak gravatar imageberak ( 2015-11-05 02:46:07 -0600 )edit

it seems to work.. but the output image is just blank.. nothing gets shows.

215 gravatar image215 ( 2015-11-05 06:57:32 -0600 )edit

that happens often with float images, you probably need to scale it a bit, like: imshow("lala", im * 100); (if it's all black, or * 0.01 if it's all white.

also try a cerr << img << endl , to see, what's in it.

berak gravatar imageberak ( 2015-11-05 07:32:15 -0600 )edit
1

Please look at the updates.

215 gravatar image215 ( 2015-11-05 08:03:42 -0600 )edit

i'm not familiar with java but (temp.at<uchar>(row+i,col+j),P); Is it x^p and if x is uchar type result is a float ?

LBerger gravatar imageLBerger ( 2015-11-05 14:13:00 -0600 )edit

Sorry for the confusion, this is the result of using float. I retrieve the value as a float, and insert it as a float. this is c++. and yes. Pow(a,c) = A^c

215 gravatar image215 ( 2015-11-05 14:57:47 -0600 )edit

(temp.rows - kernel)/2 should be temp.rows - kernel/2 and same for (temp.cols - kernel)/2 should be temp.cols - kernel/2

LBerger gravatar imageLBerger ( 2015-11-05 15:02:05 -0600 )edit

It ended in a segmentation fault (core dumped)

215 gravatar image215 ( 2015-11-05 16:14:07 -0600 )edit

(temp.rows - kernel)/2 should be temp.rows - kernel/2-1 and same for (temp.cols - kernel)/2 should be temp.cols - kernel/2-1

I think like this it's more an opencv answer

Mat orgFrame = cv::imread("f:/lib/opencv/samples/data/lena.jpg");
Mat f,fPowerP,fPowerPPlus1,meanfPowerP,meanfPowerPPlus1,meanfpplus1,r,result;
orgFrame.convertTo(f,CV_32F);
pow(f,4,fPowerP);
multiply(f,fPowerP,fPowerPPlus1);
blur(fPowerP,meanfPowerP,Size(3,3));
blur(fPowerPPlus1,meanfPowerPPlus1,Size(3,3));
divide(meanfPowerPPlus1,meanfPowerP,r);
r.convertTo(result,CV_8U);
imshow("result",result);
waitKey();
LBerger gravatar imageLBerger ( 2015-11-06 00:44:14 -0600 )edit