【问题标题】:Multi-channels LUT opencv2 python assert error多通道LUT opencv2 python断言错误
【发布时间】:2013-05-16 03:31:09
【问题描述】:

我正在尝试使用 cv2 LUT 在 Python 中进行图像传输。 LUT 需要与图像具有相同数量的通道。但我无法解决一个错误:

image1Transfered = cv2.LUT(image1, lut) cv2.error: /build/buildd/opencv-2.3.1/modules/core/src/convert.cpp:1037: 错误: (-215) (lutcn == cn || lutcn == 1) && lut.total( ) == 256 && lut.isContinuous() && (src.depth() == CV_8U || src.depth() == CV_8S) 在函数LUT中

这是python代码,我相信我可以将图像拆分为多个单通道并分别应用LUT。但这是对资源的浪费。

    #!/usr/bin/python
    import sys
    import cv2
    import numpy as np

    image1 = cv2.imread("../pic1.jpg", 1)
    # apply look up table
    lut = np.arange(255, -1, -1, dtype = image1.dtype )
    lut = np.column_stack((lut, lut, lut))
    image1Converted = cv2.LUT(image1, lut)  # <-- this is where it fails

感谢您的宝贵时间。

【问题讨论】:

    标签: python opencv


    【解决方案1】:

    您正在使用np.column_stack() 创建一个 3 通道图像,但这不是正确的功能。您必须使用np.dstack()cv2.merge()。然后就可以正常使用了。

    例如:

    In [3]: x
    array([[0, 1, 2],
           [3, 4, 5],
           [6, 7, 8]])
    
    In [5]: np.column_stack((x,x,x))
    array([[0, 1, 2, 0, 1, 2, 0, 1, 2],
           [3, 4, 5, 3, 4, 5, 3, 4, 5],
           [6, 7, 8, 6, 7, 8, 6, 7, 8]])
    
    In [6]: np.dstack((x,x,x))
    array([[[0, 0, 0],
            [1, 1, 1],
            [2, 2, 2]],
    
           [[3, 3, 3],
            [4, 4, 4],
            [5, 5, 5]],
    
           [[6, 6, 6],
            [7, 7, 7],
            [8, 8, 8]]])
    
    In [11]: cv2.merge((x,x,x))
    array([[[0, 0, 0],
            [1, 1, 1],
            [2, 2, 2]],
    
           [[3, 3, 3],
            [4, 4, 4],
            [5, 5, 5]],
    
           [[6, 6, 6],
            [7, 7, 7],
            [8, 8, 8]]], dtype=int32)
    

    【讨论】:

      【解决方案2】:

      谢谢阿比德,我喜欢你的博客。我正在逐一浏览您的 Python CV 帖子。对学习Python opencv有很大帮助。你做了非常好的工作。

      这是我最终得到的结果:

      lut3 = np.column_stack((lut, lut, lut))
      lutIdxDot = np.array( [0, 1, 2], dtype=int)
      lutIdx0 = np.zeros( image1.shape[0] * image1.shape[1], dtype=int)
      lutIdx1 = np.ones( image1.shape[0] * image1.shape[1], dtype=int)
      lutIdx2 = lutIdx1 * 2
      lutIdx = np.column_stack((lutIdx0, lutIdx1, lutIdx2))
      lutIdx.shape = image1.shape
      
      image1Rev = lut3[image1, lutIdx] # numpy indexing will generate the expected LUT result. 
      

      我使用 numpy 索引来获得结果。我没有使用 cv LUT 功能。性能我不知道。

      一开始我觉得最后一行代码很奇怪。索引是 numpy 的一个非常有趣的特性。当代码运行到最后一行时,lut3为:

      ipdb> p lut3
      array([[255, 255, 255],
             [254, 254, 254],
             [253, 253, 253],
             [252, 252, 252],
             ...
             [  2,   2,   2],
             [  1,   1,   1],
             [  0,   0,   0]], dtype=uint8)
      
      ipdb> p lutIdx
      array([[[0, 1, 2],
              [0, 1, 2],
              [0, 1, 2],
              ..., 
              ..., 
              [0, 1, 2],
              [0, 1, 2],
              [0, 1, 2]]])
      

      lutIdx 与 image1 的形状相同。 lut3[image1, lutIdx] 要求一个数组,因为它的形状与 image1 和 lutIdx 相同。它的值来自 lut3。对于 image1 的每个项目,使用 lut[image1's Value of that spot, lutIdx's Value of that spot] 找到该输出值。 (我希望我能画一张图。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-03
        • 2015-02-02
        • 2015-07-29
        • 1970-01-01
        • 1970-01-01
        • 2016-02-07
        • 2015-12-25
        • 1970-01-01
        相关资源
        最近更新 更多