【问题标题】:OpenCV distort trouble with still images静止图像的 OpenCV 失真问题
【发布时间】:2013-02-08 11:46:00
【问题描述】:

我正在使用 OpenCV 开发 Quartz Composer 插件,我遇到了通过 cvCvtColor 将静态图像(仅)转换为灰色的问题。我正在开发 2.4,但我遇到了与 2.3 相同的问题:

网络摄像头图像一切正常,而且 - 很奇怪,不是吗? - 用于来自 iPhone 的直接 jpeg 图片。但是对于其他图像,我会遇到这种失真问题。

当我缩放原始图片时,它可以纠正问题,但这并不是解决问题的好方法。

这是原始图像,右侧是高度缩放 ​​1.226 (???) 的图像:

有没有人遇到过这个问题。我想知道这不是我在 IplImage 中转换输入图像的方式,但我的代码似乎是正确的,因为我发现其他程序使用相同的方式......

会不会是输出通道数的问题?

谢谢。

编辑:

这里是方法的代码。

- (void) createWithInputImage: (id<QCPlugInInputImageSource>) image {

IplImage *r = NULL;
if (image != nil) {
    // NSLog(@"Carasuelo OpenCV - width: %f", [image imageBounds].size.width);
    CvSize size = cvSize([image imageBounds].size.width, [image imageBounds].size.height);
    const char *colorModel;
    const char *channelSeq;
    int depth;
    int channels;
    if ([image bufferPixelFormat] == QCPlugInPixelFormatARGB8) {
        depth = IPL_DEPTH_8U;
        channels = 4;
        colorModel = (char *)"RGBA";
        channelSeq = (char *)"ARGB";

    } else if ([image bufferPixelFormat] == QCPlugInPixelFormatBGRA8) {
        depth = IPL_DEPTH_8U;
        channels = 4;
        colorModel = (char *)"RGBA";
        channelSeq = (char *)"BGRA";

        // QUARTZ COMPOSER IMAGES ARE ALWAYS BGRA8 -> 8U

    } else if ([image bufferPixelFormat] == QCPlugInPixelFormatRGBAf) {
        depth = IPL_DEPTH_32F;
        channels = 4;
        colorModel = (char *)"RGBA";
        channelSeq = (char *)"RGBA";

    } else if ([image bufferPixelFormat] == QCPlugInPixelFormatI8) {
        depth = IPL_DEPTH_8U;
        channels = 1;
        colorModel = (char *)"GREY";
        channelSeq = (char *)"GREY";

    } else if ([image bufferPixelFormat] == QCPlugInPixelFormatIf) {
        depth = IPL_DEPTH_32F;
        channels = 1;
        colorModel = (char *)"GREY";
        channelSeq = (char *)"GREY";

    } else {
        NSLog(@"Format d'image non supporté: %@", [image bufferPixelFormat]);
    }


    r = cvCreateImage(size, depth, channels);

    r->imageData = (char *)[image bufferBaseAddress];

    strcpy(r->colorModel, colorModel);
    strcpy(r->channelSeq, channelSeq);
}

[self setImageCV:r];

}

谢谢!

【问题讨论】:

    标签: image opencv quartz-composer


    【解决方案1】:

    [image bytesPerRow] 返回一行像素中的字节数,可能与[image imageBounds].size.width * bytesPerPixel 不同(为了处理器寻址效率,添加了填充,使每一行从 16 的倍数开始)。

    所以,不要尝试r-&gt;imageData = (char *)[image bufferBaseAddress];,而是尝试这样的事情:

    unsigned long qcImageHeight = [image imageBounds].size.height;
    unsigned long qcImageBytesPerRow = [image bytesPerRow];
    char * qcImageData = (char *)[image imageBufferBaseAddress];
    for(unsigned long y = 0; y < height; ++y)
        memcpy((char *)r->imageData + y * r->widthStep, qcImageData + y * qcImageBytesPerRow, r->widthStep);
    

    【讨论】:

    • 谢谢@smokris!不幸的是,我在 XCode 中出现错误: memcpy(r->imageData + y * r->widthStep, qcImageData + y * qcImageBytesPerRow, r->widthStep); 指向 void 的指针的算术运算 我知道必须有一个明显的解决方案,但是……我必须强制转换吗?适合什么类型?
    • 谢谢,对不起:(char *)也在我的初始函数中......但是,好吧......它不起作用,我对你的方法也有同样的问题......: -/
    • 该死。那么我不确定。
    猜你喜欢
    • 1970-01-01
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    相关资源
    最近更新 更多