2017-02-10 7 views
2

Ich möchte eine benutzerdefinierte Farbe Look-up-Tabelle (LUT) auf ein Bild anwenden.Bewerben openCV Look Up Table (LUT) auf ein Bild

Ich habe über die openCVLUT Api geschaut, aber ich kann nicht scheinen, es richtig zu machen.

#!/bin/bash 
# -*- coding: utf-8 -*- 

import os 
import numpy as np 
import cv2 

b_max = 230 
g_max = 220 
r_max = 250 

identity = np.arange(256, dtype = np.dtype('uint8')) 

b_channel = np.arange(b_max, dtype = np.dtype('uint8')) 
b_channel = np.append(b_channel, (256-b_max)*[b_max]) 
g_channel = np.arange(g_max, dtype = np.dtype('uint8')) 
g_channel = np.append(g_channel, (256-g_max)*[g_max]) 
r_channel = np.arange(r_max, dtype = np.dtype('uint8')) 
r_channel = np.append(r_channel, (256-r_max)*[r_max]) 

if 256 != b_channel.size or 256 != g_channel.size or 256 != r_channel.size: 
    print "size of arrays don't match!" 

lut = np.dstack((identity, identity, b_channel, g_channel, r_channel)) 

# Load the image 
img = cv2.imread('input.jpg',cv2.IMREAD_COLOR) 
dstImage = cv2.LUT(img, lut) 
cv2.imwrite('output.jpg', dstImage) 

bekomme ich folgende Fehlermeldung:

OpenCV Error: Assertion failed ((lutcn == cn || lutcn == 1) && _lut.total() == 256 && _lut.isContinuous() && (depth == CV_8U || depth == CV_8S)) in cv::LUT, file C:\build\master_winpack-bindings-win32-vc14-static\opencv\modules\core\src\convert.cpp, line 5690 
Traceback (most recent call last): 
    File "ApplyCLUT.py", line 33, in <module> 
    dstImage = cv2.LUT(img, lut) 
cv2.error: C:\build\master_winpack-bindings-win32-vc14-static\opencv\modules\core\src\convert.cpp:5690: error: (-215) (lutcn == cn || lutcn == 1) && _lut.total() == 256 && _lut.isContinuous() && (depth == CV_8U || depth == CV_8S) in function cv::LUT 

EDIT: Änderung dieser Zeile:

lut = np.dstack((b_channel, g_channel, r_channel)) 

und es wird funktionieren!

+0

"lut ist kein numpliges Array" - machen Sie es vielleicht zu einem numpligen Array? Wie in 'np.array (....)' –

Antwort

2

Im Folgenden ein Beispiel für Gebäude LUT ist:

identity = np.arange(256, dtype = np.dtype('uint8')) 
zeros = np.zeros(256, np.dtype('uint8')) 
lut = np.dstack((identity, identity, zeros)) 

Hinweis, ist es viel einfacher und einfache Art und Weise den roten Kanal Nullstellung:

img[:,:,2] = 0 

Auch Aufruf LUT-Funktion sollte wie folgt aussehen:

dstImage = cv2.LUT(img, lut) 

Setzen Sie blaue Werte größer als b_max auf b_max:

b_max = 20 
img[img[:,:,0] > b_max, 0] = b_max; 
+0

Danke für die Antwort, ich möchte den Maximalwert jedes Kanals einzeln senken. Ich habe die Frage jetzt aktualisiert. – theAlse

+0

Funktioniert es mit meinen Vorschlägen? –

+0

ja, die lut in Ihrem Code entfernt alle roten Farben, aber ich möchte Änderungen an alle 3 Kanäle direkt anwenden – theAlse