简体   繁体   English

Python:在opencv中手动合并通道

[英]Python: merging channels in opencv and manually

def frame_processing(frame):
out_frame = np.zeros((frame.shape[0],frame.shape[1],4),dtype = np.uint8)
b,g,r = cv2.split(frame)
alpha = np.zeros_like(b , dtype=np.uint8)
print(out_frame.shape)
print(b.shape);print(g.shape);print(r.shape);print(alpha.shape)
for i in range(frame.shape[0]):
    for j in range(frame.shape[1]):
        a = (frame[i,j,0],frame[i,j,1],frame[i,j,2])
        b = (225,225,225)
        if all(i > j for i, j in zip(a,b)):  #all(a>b) :
            alpha[i,j] = 0
        else:
            alpha[i,j] = 255
out_frame[:,:,0] = b
out_frame[:,:,1] = g
out_frame[:,:,2] = r
out_frame[:,:,3] = alpha
#out_frame = cv2.merge((b,g,r,alpha))
return out_frame

Wanted to add an alpha channel; 想添加一个alpha通道; tried cv2.Merge() and manual stacking of channels but failed. 尝试了cv2.Merge()和手动堆叠频道但失败了。

When using cv2.merge() : 使用cv2.merge()

error: OpenCV(3.4.2) C:\projects\opencv- 
python\opencv\modules\core\src\merge.cpp:458: error: (-215:Assertion failed) 
mv[i].size == mv[0].size && mv[i].depth() == depth in function 'cv::merge'

When manually adding channels: 手动添加频道时:

ValueError: could not broadcast input array from shape (3) into shape 
(225,225)

Use cv2.inRange to find the mask, then merge them with np.dstack : 使用cv2.inRange查找掩码,然后将它们与np.dstack合并:

#!/use/bin/python3
# 2018/09/24 11:51:31 (CST)
import cv2
import numpy as np

#frame = ...
mask = cv2.inRange(frame, (225,225,225), (255,255,255))

#dst = np.dstack((frame, 255-mask))
dst = np.dstack((frame, mask))

cv2.imwrite("dst.png", dst)

To find the specific color, maybe you will be interested with this question: 要找到特定的颜色,也许您会对这个问题感兴趣:

Choosing the correct upper and lower HSV boundaries for color detection with`cv::inRange` (OpenCV) 使用`cv :: inRange`(OpenCV)为颜色检测选择正确的上下HSV边界

Its a simple typo. 这是一个简单的错字。 You are changing the variable "b" in the for loop and it conflicts with variable of blue channel. 您正在更改for循环中的变量“b”,它与蓝色通道的变量冲突。 Change b = (225,225,225) to threshold = (225, 255, 255) and zip(a,b) to zip(a, threshold) should fix the problem. b = (225,225,225)更改为threshold = (225, 255, 255) b = (225,225,225)并将zip(a,b)更改为zip(a, threshold)解决问题。
By the way, you can use this to create your alpha channel: 顺便说一下,您可以使用它来创建您的Alpha通道:

alpha = np.zeros(b.shape, dtype=b.dtype)

Also you can fill your alpha channel like this if you need more speed (you can measure time difference): 如果您需要更快的速度(可以测量时差),也可以像这样填充Alpha通道:

alpha[~((b[:,:]>threshold[0]) & (g[:,:]>threshold[1]) & (r[:,:]>threshold[2]))] = 255

So your function becomes: 所以你的功能变成了:

def frame_processing(frame):
    #  split channels
    b,g,r = cv2.split(frame)

    #  initialize alpha to zeros
    alpha = np.zeros(b.shape, dtype=b.dtype)

    #  fill alpha values
    threshold = (225, 225, 225)
    alpha[~((b[:,:]>threshold[0]) & (g[:,:]>threshold[1]) & (r[:,:]>threshold[2]))] = 255

    #  merge all channels back
    out_frame = cv2.merge((b, g, r, alpha))

    return out_frame

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

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