简体   繁体   English

多通道LUT opencv2 python断言错误

[英]Multi-channels LUT opencv2 python assert error

I am trying to use the cv2 LUT to do a image transfer in Python. 我试图使用cv2 LUT在Python中进行图像传输。 The LUT needs to be has same number of channels as the image. LUT需要与图像具有相同数量的通道。 But I can't solve one error: 但我无法解决一个错误:

image1Transfered = cv2.LUT(image1, lut) cv2.error: /build/buildd/opencv-2.3.1/modules/core/src/convert.cpp:1037: error: (-215) (lutcn == cn || lutcn == 1) && lut.total() == 256 && lut.isContinuous() && (src.depth() == CV_8U || src.depth() == CV_8S) in function 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

Here is the python code, I believe I can split the image to multiple single channels and apply the LUT respectively. 这是python代码,我相信我可以将图像分割成多个单个通道并分别应用LUT。 But this is waste of resource. 但这是浪费资源。

    #!/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

Thank you for your time. 感谢您的时间。

You are using np.column_stack() to create a 3-channel image, but that is not the right function. 您正在使用np.column_stack()创建一个3通道图像,但这不是正确的功能。 You have to use either np.dstack() or cv2.merge() . 您必须使用np.dstack()cv2.merge() Then it works fine. 然后它工作正常。

eg: 例如:

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)

Thank you Abid, I like your blog. 谢谢阿比德,我喜欢你的博客。 I am going through you Python CV post one by one. 我将逐一通过你的Python CV帖子。 It's a great help on learning Python opencv. 这对学习Python opencv有很大帮助。 You did a very nice work. 你做了一件非常好的工作。

Here is what I ended up with: 这是我最终得到的:

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. 

I used the numpy indexing to get the result. 我使用numpy索引来获得结果。 I didn't use the cv LUT function. 我没有使用cv LUT功能。 The performance is unknown to me. 表演对我来说不得而知。

The last line of code was strange to me at first. 最后一行代码起初很奇怪。 The indexing is a very interesting feature of numpy. 索引是numpy的一个非常有趣的特性。 When the code run to the last line, the lut3 is: 当代码运行到最后一行时,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 has the same shape of the image1. lutIdx具有与image1相同的形状。 The lut3[image1, lutIdx] is asking for an array as the result that its shape is same with image1, and lutIdx. lut3 [image1,lutIdx]要求一个数组,因为它的形状与image1和lutIdx相同。 Its values are from lut3. 它的价值来自lut3。 For each item of image1, use lut[image1's Value of that spot, lutIdx's Value of that spot] to find that output value. 对于image1的每个项目,使用lut [image1的那个点的值,lutIdx的那个点的值]来查找该输出值。 (I wish I can draw a diagram.) (我希望我能画一张图。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM