Applying filter keeps crashing? [closed]
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.
---Update--- Using the code snippet posted below things the output image becomes this
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.
couple more things wrong here:
for(int row = kernel/2; row < temp.rows-kernel/2; row++)
, same for colstemp.at<int>
is sure the wrong typeoutput.at<uchar>
you will need a float Mat, else either pow() will overflow, and you get rounding errorsfloor(kernel/2)
since kernel is int, floor has no effectit seems to work.. but the output image is just blank.. nothing gets shows.
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.Please look at the updates.
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 ?
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
(temp.rows - kernel)/2 should be temp.rows - kernel/2 and same for (temp.cols - kernel)/2 should be temp.cols - kernel/2
It ended in a segmentation fault (core dumped)
(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