I contact a problem in my opencv poroject that the cvCopy function don't have normal behavior ! so I write this code to test it.
int n = 6;
IplImage* img = cvCreateImage( cvSize(n,n) ,1 ,1 );
uchar* ptr = (uchar*)(img->imageData);
for(int i = 0 ; i< n*n ; i++)
{
ptr[i] = i+1;
}
std::cout << "befor copy" << std::endl;
for( int i =0 ; i < n*n ;i++) //print the imageData of img
{
if ( i % n == 0 )
std::cout<<std::endl;
std::cout << std::setw(4) <<(int) ptr[i];
}
IplImage* img2 = cvCreateImage(cvGetSize(img),img->depth,img->nChannels);
cvCopy(img,img2);
ptr = (uchar*)(img2->imageData);
std::cout << "\nafter copy" << std::endl;
for( int i =0 ; i < n*n ;i++) //print the imageData of img2
{
if ( i % n == 0 )
std::cout<<std::endl;
std::cout << std::setw(4) <<(int) ptr[i];
}
std::cout<<std::endl;
but the output is :
befor copy
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
after copy
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 0 0 0 0
0 0 0 0 0 0
So, it just copied until the number 26; not all the imageData !
why cvCopy behaves like this ?
The probem is with:
IplImage* img = cvCreateImage( cvSize(n,n) ,1 ,1 );
You have set the depth to 1
, but it should be something like IPL_DEPTH_8U. Here depth=1 does not mean 1 byte, it is depth in bits. You would be best to use one of the predefined values . Eg
IplImage* img = cvCreateImage(cvSize(n, n), IPL_DEPTH_8U, 1);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.