将 NSImage 转换为 IplImage
将图像(仅 jpeg 文件)从 NSImage 转换为 IplImage 时遇到两个问题。我的第一个问题是转换后的图像的颜色强度与原始图像不同。转换后的图像在某种程度上比原始图像更加单调。
我的第二个问题是只有 8/10 图像被完全转换,2/10 图像只有部分图像会被转换。我的意思是只有图像的左上角会被转换。我希望你理解我,我的英语不是最好的。
IplImage* convert(NSImage* bild){
//converting the NSImage into an IplImage
NSBitmapImageRep *bitmap2 = [NSBitmapImageRep imageRepWithData:[bild TIFFRepresentation]];
int depth = [bitmap2 bitsPerSample];
int channels = [bitmap2 samplesPerPixel];
int height = [bitmap2 size].height;
int width = [bitmap2 size].width;
IplImage *iplpic = cvCreateImage(cvSize( width,height), depth, channels);
cvSetImageData(iplpic, [bitmap2 bitmapData], [bitmap2 bytesPerRow]);
for (int i = 0; i < iplpic->imageSize; i += 3) {
uchar tempR, tempG, tempB;
tempR = iplpic->imageData[i];
tempG = iplpic->imageData[i+1];
tempB = iplpic->imageData[i+2];
iplpic->imageData[i+2] = tempR;
iplpic->imageData[i+1] =tempG;
iplpic->imageData[i] = tempB;
}
cvSaveImage("/Users/goette/out.jpg", iplpic, 0);
return iplpic;
}
I have two problems converting my image (only jpeg-files) from NSImage to IplImage. My first problem is that the converted image has not the same colour intensity like the original one. The converted image is somehow more toneless than the original one.
My second problem is only 8/10 images are converted totally, by 2/10 images only a part of the image will be converted. What I mean is that only the upper left corner of the image will be converted. I hope you understand me, my english is not the best.
IplImage* convert(NSImage* bild){
//converting the NSImage into an IplImage
NSBitmapImageRep *bitmap2 = [NSBitmapImageRep imageRepWithData:[bild TIFFRepresentation]];
int depth = [bitmap2 bitsPerSample];
int channels = [bitmap2 samplesPerPixel];
int height = [bitmap2 size].height;
int width = [bitmap2 size].width;
IplImage *iplpic = cvCreateImage(cvSize( width,height), depth, channels);
cvSetImageData(iplpic, [bitmap2 bitmapData], [bitmap2 bytesPerRow]);
for (int i = 0; i < iplpic->imageSize; i += 3) {
uchar tempR, tempG, tempB;
tempR = iplpic->imageData[i];
tempG = iplpic->imageData[i+1];
tempB = iplpic->imageData[i+2];
iplpic->imageData[i+2] = tempR;
iplpic->imageData[i+1] =tempG;
iplpic->imageData[i] = tempB;
}
cvSaveImage("/Users/goette/out.jpg", iplpic, 0);
return iplpic;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我解决了第二个问题。我像这样更改了代码:
但我仍然遇到失去颜色强度的问题。
I solved the second problem. I changed the code like this:
But I still have the problem losing the colour intensity.