Create gray IplImage from gray UIImage
I'm a trying to create a gray IplImage from a gray scaled UIImage and Im u开发者_如何学Csing the method below.
- (IplImage *)createGrayIplImageFromUIImage:(UIImage *)image {
CGImageRef imageRef = [image CGImage];
CFDataRef dat = CGDataProviderCopyData(CGImageGetDataProvider(imageRef));
const unsigned char *buffer = CFDataGetBytePtr(dat);
IplImage *iplimage = cvCreateImage(cvSize(image.size.width, image.size.height), IPL_DEPTH_8U, 1);
for (int i=0; i<image.size.width*image.size.height; i++) {
iplimage->imageData[i] = buffer[i];
NSLog(@"in creategrayiplimage - %d --- %d",buffer[i], iplimage->imageData[i]);
}
return iplimage;
}
When i log the data in for loop, buffer has correct values (0-255) but iplimage->imageData has signed values even its IPL_DEPTH_8U.
e.g
in creategrayiplimage - 149 --- -107 in creategrayiplimage - 247 --- -9 in creategrayiplimage - 127 --- 127 in creategrayiplimage - 128 --- -128
This seems like a small problem but I am stuck and I wonder why i get minus values from iplimage. Thanks in advance
A byte is a byte is a byte. The data itself doesn't know whether it's signed or unsigned -- it's all in how you interpret the data.
Change your NSLog() statement to use %u (unsigned int) instead of %d (signed int) for the format specifier and your data will be displayed unsigned.
精彩评论