错误:合并中的断言失败(0< cn& cn< = CV_CN_MAX)
在合并功能
中cv2.merge(channels,img2)
如果参数被替换如下所示:
cv2.merge(img2,channels)
它不会给出错误,但均衡之前和之后的直方图将是相同的。我能在这段代码中做些什么。
代码:
import cv2,cv
import cv2.cv as cv
import numpy as np
from matplotlib import pyplot as plt
capture = cv.CaptureFromCAM(0)
img = cv.QueryFrame(capture)
img_size = cv.GetSize(img)
width,height = img_size
size = width,height,3
channels = np.zeros(size , np.uint8)
while (1):
img = cv.QueryFrame(capture)
img = np.asarray(img[:,:])
cv2.imshow("original",img)
hist = cv2.calcHist([img],[2],None,[256],[0,256])
#convert img to YCR_CB
img2 = cv2.cvtColor(img,cv2.COLOR_BGR2YCR_CB)
#split image to Y, CR, CB
cv2.split(img2,channels)
#histogram equalization to Y-MATRIX
cv2.equalizeHist(channels[0],channels[0])
#merge this matrix to reconstruct our colored image
cv2.merge(channels,img2)
#convert this output image to rgb
rgb = cv2.cvtColor(img2,cv2.COLOR_YCR_CB2BGR)
hist2 = cv2.calcHist([rgb],[2],None,[256],[0,256])
plt.plot(hist)
plt.plot(hist2)
plt.show()
答案 0 :(得分:6)
不要使用split
和merge
,而是利用numpy切片。
img2[:, :, 0] = cv2.equalizeHist(img2[:, :, 0])
# or run a small loop over each channel
答案 1 :(得分:2)
由于您没有捕获返回值,因此您的频道未初始化
>>> import cv2
>>> help(cv2.split)
Help on built-in function split in module cv2:
split(...)
split(m[, mv]) -> mv
所以看起来应该是这样的:
channels = cv2.split(img2)
并请,避免旧的cv api,而是坚持使用cv2。 (使用cv2.VideoCapture,而不是cv.CaptureFromCAM)