python - 无法找出 cv.mat.step[0] 的 Numpy 等价物

标签 python objective-c macos opencv numpy

我目前正在将代码从旧的 OpenCV 示例转移到 Python 中的 OpenCV3(使用 PyObjC 和 Quartz 模块)。 Objective-C 代码采用 UIImage 并创建可供 OpenCV 使用的 Material 。我的 python 代码采用 CGImage 并执行相同的操作。

这是 Objective-C 代码:

(cv::Mat)cvMatFromUIImage:(UIImage *)image {
  CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
  CGFloat cols = image.size.width;
  CGFloat rows = image.size.height;

  cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels (color channels + alpha)

  CGContextRef contextRef = CGBitmapContextCreate(cvMat.data,                 // Pointer to  data
                                                 cols,                       // Width of bitmap
                                                 rows,                       // Height of bitmap
                                                 8,                          // Bits per component
                                                 cvMat.step[0],              // Bytes per row
                                                 colorSpace,                 // Colorspace
                                                 kCGImageAlphaNoneSkipLast |
                                                 kCGBitmapByteOrderDefault); // Bitmap info flags

  CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
  CGContextRelease(contextRef);

  return cvMat;
}

这是我的 Python 等价物:

def macToOpenCV(image):
    color_space = CGImageGetColorSpace(image)
    column = CGImageGetHeight(image)
    row = CGImageGetWidth(image)
    mat = np.ndarray(shape=(row, column, 4), dtype=np.uint8)
    c_ref = CGBitmapContextCreate(mat,
                                  row,
                                  column,
                                  8,
                                  ,  # mat.step[0],
                                  color_space,
                                  kCGImageAlphaNoneSkipLast |
                                  kCGBitmapByteOrderDefault)

    CGContextDrawImage(c_ref, CGRectMake(0, 0, column, row), image)
    return mat

我相当有信心我目前拥有大部分权利,但是我失去了我应该调用的相当于 Numpy 中的 cvMat.step[0] 的东西。我也欢迎对代码段进行一些一般代码审查,因为当我使用 cv2.imshow() 时,我根本没有得到我期望的图像:)。

谢谢!

最佳答案

我最终放弃了上述方法,并在这个堆栈溢出问题上找到了一个经过一些编辑后有效的答案:Converting CGImage to python image (pil/opencv)

image_ref = CGWindowListCreateImage(CGRectNull,
                                        kCGWindowListOptionIncludingWindow,
                                        wid,
                                        kCGWindowImageBoundsIgnoreFraming)

pixeldata = CGDataProviderCopyData(CGImageGetDataProvider(image_ref))

height = CGImageGetHeight(image_ref)
width = CGImageGetWidth(image_ref)

image = Image.frombuffer("RGBA", (width, height),
                         pixeldata, "raw", "RGBA", 0, 1)

# Color correction from BGRA to RGBA
b, g, r, a = image.split()
image = Image.merge("RGBA", (r, g, b, a))
np.array(image)
return np.array(image)

本例中的图像是 PIL.Image。您还可以看到我选择了自动步幅计算(frombuffer() 中的参数 0),主要是因为答案使用的函数我无法开始工作。

关于python - 无法找出 cv.mat.step[0] 的 Numpy 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37359192/

相关文章:

python - 当不同时间满足条件时停止 python for 循环

iphone - 通过使用低分辨率图像然后交换高分辨率图像来加速 UIImagePickerController

ios - 播放视频时,UIWebview获取事件

python - 需要初始化的 tf.keras 自定义层无法使用 tf.contrib.saved_model.save_keras_model 保存

python - Django Haystack - 无需搜索查询即可显示结果?

ios - TableView 折叠/展开部分

macos - 如何在 OS X 中通过命令行获取事件用户的名称?

swift - 无法形成 Range with end < start 在执行 for 循环之前检查范围?

Excel VBA – 如何创建和转储数组复杂结构的哈希值?

python - Django 发送请求花费太多时间