从灰色 UIImage 创建灰色 IplImage
我正在尝试从灰度 UIImage 创建灰色 IplImage,并使用下面的方法。
- (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;
}
当我在 for 循环中记录数据时,缓冲区具有正确的值(0-255),但 iplimage->imageData 具有签名值,甚至其 IPL_DEPTH_8U 也是如此。
例如
在 creategrayiplimage - 149 --- -107 在创建grayiplimage - 247 --- -9 在创建grayiplimage - 127 --- 127 在 creategrayiplimage - 128 --- -128
这似乎是一个小问题,但我被卡住了,我想知道为什么我从 iplimage 得到负值。提前致谢
I'm a trying to create a gray IplImage from a gray scaled UIImage and Im using 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个字节是一个字节是一个字节。数据本身并不知道它是有符号的还是无符号的——这完全取决于你如何解释数据。
更改您的 NSLog() 语句以使用 %u (无符号整数)而不是 %d (有符号整数)作为格式说明符,并且您的数据将显示为无符号。
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.