将 NSImage 转换为 IplImage

发布于 2024-11-09 08:23:52 字数 1164 浏览 0 评论 0原文

将图像(仅 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听不够的曲调 2024-11-16 08:23:52

我解决了第二个问题。我像这样更改了代码:

NSBitmapImageRep *bitmap2 = [NSBitmapImageRep imageRepWithData:[bild TIFFRepresentation]];

NSImage* bild1 = [[NSImage alloc] initWithSize:NSMakeSize([bitmap2 pixelsWide],[bitmap2 pixelsHigh])];

int depth       = [bitmap2 bitsPerSample];
int channels    = [bitmap2 samplesPerPixel];
int height      = [bild1 size].height;
int width       = [bild1 size].width;


IplImage *iplpic = cvCreateImage(cvSize(  width,height), depth, channels);
cvSetImageData(iplpic, [bitmap2 bitmapData], [bitmap2 bytesPerRow]);

但我仍然遇到失去颜色强度的问题。

I solved the second problem. I changed the code like this:

NSBitmapImageRep *bitmap2 = [NSBitmapImageRep imageRepWithData:[bild TIFFRepresentation]];

NSImage* bild1 = [[NSImage alloc] initWithSize:NSMakeSize([bitmap2 pixelsWide],[bitmap2 pixelsHigh])];

int depth       = [bitmap2 bitsPerSample];
int channels    = [bitmap2 samplesPerPixel];
int height      = [bild1 size].height;
int width       = [bild1 size].width;


IplImage *iplpic = cvCreateImage(cvSize(  width,height), depth, channels);
cvSetImageData(iplpic, [bitmap2 bitmapData], [bitmap2 bytesPerRow]);

But I still have the problem losing the colour intensity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文