簡體   English   中英

多通道LUT opencv2 python斷言錯誤

[英]Multi-channels LUT opencv2 python assert error

我試圖使用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

感謝您的時間。

您正在使用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)

謝謝阿比德,我喜歡你的博客。 我將逐一通過你的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的那個點的值,lutIdx的那個點的值]來查找該輸出值。 (我希望我能畫一張圖。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM