OpenCV扭曲了静止图像的麻烦

时间:2013-02-08 11:46:01

标签: image opencv quartz-composer

我正在使用OpenCV开发Quartz Composer插件,我有这个问题,静止图像(仅)通过cvCvtColor转换为灰色。我正在研究2.4但我在2.3中遇到了同样的问题:

网络摄像头图像一切正常,而且 - 奇怪,不是吗? - 从iPhone直接jpeg图片。但是对于其他图像我有这种扭曲的麻烦。

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

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

enter image description here

有没有人遇到过这个麻烦。我想知道在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];

}

谢谢!

1 个答案:

答案 0 :(得分:0)

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

所以,而不是r->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);