std::bad_alloc at memory location 0x0016ef74..

Pages: 12
I have solved the problem of the issue mentioned above. For those doing the same stuff as mine in opencv, I would like to share with you guys the mistakes I made.

First of all, is the declaration of the multiple images array. This is the correct way of declaration:

IplImage **covarInterim;

covarInterim = (IplImage**)malloc(numImages * sizeof(IplImage *));


Secondly, is the method of changing Mat->IplImage*:

Mat dest_0;
IplImage Gab0= dest_0;
covarInterim[8]= cvCloneImage(&Gab0);

(p/s:http://stackoverflow.com/questions/6145661/mat-iplimage-conversion-with-data-copy)

Now I stuck in the second issue about covariance matrix. I will try to work it out and see. Thank those who helping me yesterday..^^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
IplImage** Feature(IplImage* in,IplImage Gab0,IplImage Gab85,IplImage Gab180,IplImage Gab265 )
{   
	CvSize sz = cvGetSize( in );

IplImage **covarInterim;

covarInterim = (IplImage**)malloc(12 * sizeof(IplImage *));
CvScalar sX;
CvScalar sY;
for (int i = 0; i < 12 ; i++) {
covarInterim[i] = cvCreateImage(sz, in->depth,1);
cvSetZero(covarInterim[i]);
}

for (int countY = 0; countY<in->height; countY++ ){
	for (int countX = 0; countX<in->width;countX++ ){
		sX.val[0]=countX;
		sY.val[0]=countY;
		cvSet2D(covarInterim[0],countY,countX,sX); //spatial layout of X
        cvSet2D(covarInterim[1],countY,countX,sY);  //spatial layout of Y  
}
}


IplImage* xsobel = cvCreateImage(sz, IPL_DEPTH_16S,1);
IplImage* ysobel = cvCreateImage(sz, IPL_DEPTH_16S,1);
IplImage* img_gray = cvCreateImage(cvGetSize(in), IPL_DEPTH_8U,1);
cvCvtColor(in,img_gray, CV_RGB2GRAY); 
cvSobel( img_gray, xsobel, 1,0, 3 ); 
cvSobel( img_gray, ysobel, 0, 1, 3 );  
cvConvertScaleAbs(xsobel,covarInterim[2], 1, 0); //gradient in X direction
cvConvertScaleAbs(ysobel,covarInterim[3], 1, 0); //gradient in Y direction
cvReleaseImage(&xsobel);
cvReleaseImage(&ysobel);
cvReleaseImage(&xsobel);
cvReleaseImage(&img_gray);

cvSplit(in,covarInterim[4],covarInterim[5],covarInterim[6],0);      //R,G,B channel image


float temp_gradient;
const float PI = 3.14159265f;

for (int y = 0; y <sz.height; y++) {

/* ptr1 and ptr2 point to beginning of the current row in the xsobel and ysobel images respectively. ptrs point to the beginning of the current rows in the O images */
float* ptr1 = (float*) (covarInterim[2]->imageData + y * (covarInterim[2]->widthStep));
float* ptr2 = (float*) (covarInterim[3]->imageData + y * (covarInterim[3]->widthStep));
float* ptrs = (float*) (covarInterim[7]->imageData + y * (covarInterim[7]->widthStep));


/*For every pixel in a row gradient orientation and magnitude are calculated and corresponding values set for the bin images. */
for (int x = 0; x <sz.width; x++) {

/* if the xsobel derivative is zero for a pixel, a small value is added to it, to avoid division by zero. atan returns values in radians, which on being converted to degrees, correspond to values between -90 and 90 degrees. 90 is added to each orientation, to shift the orientation values range from {-90-90} to {0-180}. This is just a matter of convention. {-90-90} values can also be used for the calculation. */

if (ptr1[x] == 0){
//temp_gradient = (float)(((atan(ptr2[x] / (ptr1[x] + 0.00001)) *(180/ PI))) + 90.0000);
temp_gradient = ((atan(ptr2[x] /(ptr1[x] + 0.00001))) * (180/ PI)) + 90;
}
else{
//temp_gradient = (float)(((atan(ptr2[x] / ptr1[x])) *(180 / PI)) + 90.0000);
	temp_gradient = ((atan(ptr2[x] / ptr1[x])) * (180 / PI)) + 90;
}
//temp_magnitude = sqrt((ptr1[x] * ptr1[x]) + (ptr2[x] * ptr2[x]));
ptrs[x] = temp_gradient;
}
}


covarInterim[8]= cvCloneImage(&Gab0);


covarInterim[9]= cvCloneImage(&Gab85);


covarInterim[10]= cvCloneImage(&Gab180);


covarInterim[11]= cvCloneImage(&Gab265);

return covarInterim;
}
Topic archived. No new replies allowed.
Pages: 12