Segmentation fault when calling a method which contains an image as parameter
Hi everybody ! I'm going to start by showing you the debugger output :
#0 0x0804c2cb in cv::Mat::Mat (this=0xbffff110, m=...)
at /usr/local/include/opencv2/core/mat.hpp:117
#1 0x0804ad14 in decompose (input=...) at main.cpp:241
#2 0x0804bbd7 in main (argc=2, argv=0xbffff6c4) at main.cpp:322
Here's my code :
#define MAX_ITERATIONS 15
//////////////////
/////////////////
float sum(cv::Mat img, int startedY, int startedX, int w) {
float res = 0.0f;
for (int j = startedY - ((w - 1) / 2) ; j < startedY + ((w + 1) / 2); j++)
for (int i = startedX - ((w - 1) / 2); i < startedX + ((w + 1) / 2); i++) {
{
if ((i >= 0 && i < img.size().width) && (j >= 0 && j < img.size().height)) {
res += img.at<float>(j,i);
}}} return res; }
cv::Mat decompose(cv::Mat input)
{
///////////////////
cv::Mat imgSource(inputImg);
cv::Mat imgMax(inputImg);
cv::Mat newImgMax(imgMax.size().height,imgMax.size().width,CV_32F);
/////////////////////////////////
////////////////////////////////////
for (int l = 0; l < imgSource.size().width; l++){
for (int k = 0; k < imgSource.size().height; k++){
if( (k >= 0 && k < imgSource.size().height) && (l >= 0 && l < imgSource.size().width) ) {
newImgMax.at<float>(k,l) = (float)sum(imgMax,k, l, wmax) / (wmax * wmax);******* *LIGNE 241*****
}}}
////////////////////
/////////////////////
}
int main(int argc, char **argv)
{
char modeTitle[30]= {0}, residueTitle[50];
double variance = 1000000;
if (argc != 2) {
std::cout << "Usage: ./emd <image>" << std::endl;
return 1;
}
cv::Mat inputImg;
cv::Mat imgMode;
inputImg=imread(argv[1]);
if(! inputImg.data )
{cout << "Could not open or find the image" << std::endl ;
return -1; }
namedWindow("Source Image",WINDOW_AUTOSIZE);
imshow("Source Image",inputImg);
cv::waitKey(1000);
for (int i = 1; i < MAX_ITERATIONS + 1; i++) {
sprintf(modeTitle, "BEMC-%d", i);
std::cout << "Decomposing " << modeTitle << std::endl;
imgMode = decompose(inputImg);**************LIGNE 322****************
//////////////
/////////////
}
here's a portion of /usr/local/include/opencv2/core/mat.hpp :
inline Mat::Mat(const Mat& m)
: flags(m.flags), dims(m.dims), rows(m.rows), cols(m.cols), data(m.data),
refcount(m.refcount), datastart(m.datastart), dataend(m.dataend),
datalimit(m.datalimit), allocator(m.allocator), size(&rows)
{
if( refcount )
CV_XADD(refcount, 1);**************** LIGNE 117******************
if( m.dims <= 2 )
{
step[0] = m.step[0]; step[1] = m.step[1];
}
else
{
dims = 0;
copySize(m);
}
}
Any ideas how I could fix this issue?
Where are
imgSource
andnewImgMax
? Have you even defined them?yes I did, I defined them like this :
Could you please update your question and provide all relevant code? Even so, you seem to lack knowledge of very basic C++ programming things, and you keep doing per-pixel loops even though we already told you that it's a terrible terrible idea
thank you @LorenaGdL for your reply ,please is there any other method to access the pixels ?
refer to your own previous question: http://answers.opencv.org/question/91...