首页 > 解决方案 > OpenCV python使用阈值搜索颜色

问题描述

我正在尝试按以下方式组合两个图像。我有这两个图像 一个文件夹封面图像,结合这两个图像我想得到这个最终结果。为此,我执行以下操作。

import cv2
import numpy as np

img = cv2.imread('carpeta3.png', cv2.IMREAD_UNCHANGED)
cover = cv2.imread('cover.png', cv2.IMREAD_UNCHANGED)
pos = np.full((img.shape[0],img.shape[1]), False) 

#Search for green pixels  
for i in range (0,img.shape[0]):
    for j in range (0,img.shape[1]):
        color = img[i,j]
        if color[0] == 0 and color[1] == 255 and color[2] == 0:
            pos[i,j] = True
        else:
            continue
        
#cropping the cover image
for i in range (0,img.shape[0]):
    for j in range (0,img.shape[1]):
        if pos[i,j] == False:
            cover[i,j] = [0,0,0,255]
        else:
            continue

#combining the two images
for i in range (0,img.shape[0]):
    for j in range (0,img.shape[1]):
        if pos[i,j] == True:
            img[i,j] = cover[i,j]
        else:
            continue

cv2.imwrite('D:/Img/test.png',img)

作为最终结果,我在执行代码后得到了这个最终结果 。如您所见,还剩下一些绿色像素,有没有办法用阈值搜索这些像素?

标签: pythonnumpyopencv-python

解决方案


推荐阅读