【问题标题】:How to merge 2 gray-scale images in Python with OpenCV如何在 Python 中使用 OpenCV 合并 2 个灰度图像
【发布时间】:2015-04-24 12:42:35
【问题描述】:

我想用 OpenCv 合并方法合并 2 个单通道灰度图像。就是下面的代码:

...
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
zeros = numpy.zeros(img_gray.shape)
merged = cv2.merge([img_gray, zeros])
...

问题在于灰度图像没有应该为1的深度属性,并且合并功能需要相同大小的图像和相同的深度。我得到错误:

error: /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/convert.cpp:296: error: (-215) mv[i].size == mv[0].size && mv[i].depth() == depth in function merge

如何合并这些数组?

【问题讨论】:

    标签: python-2.7 opencv merge channel


    【解决方案1】:

    已解决,我必须将 img_gray 的 dtype 从 uint8 更改为 float64

    img_gray = numpy.float64(img_gray)
    

    OpenCV 版本 2.4.11

    import numpy as np
    # Load the image
    img1 = cv2.imread(paths[0], cv2.IMREAD_UNCHANGED)
    
    # could also use cv2.split() but per the docs (link below) it's time consuming
    # split the channels using Numpy indexing, notice it's a zero based index unlike MATLAB
    b = img1[:, :, 0]
    g = img1[:, :, 1]
    r = img1[:, :, 2]
    
    # to avoid overflows and truncation in turn, clip the image in [0.0, 1.0] inclusive range
    b = b.astype(np.float)
    b /= 255
    

    操纵通道...在我的例子中,将高斯噪声添加到蓝色通道 (b => b1)

    b1 = b1.astype(np.float)
    g = g.astype(np.float)
    r = r.astype(np.float)
    
    # gotcha : notice the parameter is an array of channels
    noisy_blue = cv2.merge((b1, g, r))
    
    # store the outcome to disk
    cv2.imwrite('output/NoisyBlue.png', noisy_blue)
    

    注:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-16
      • 2019-03-04
      • 1970-01-01
      • 2017-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多