使轮廓内的所有白色像素变为黄色

时间:2021-03-23 13:57:21

标签: python opencv contour

您可以在下面找到我正在使用的输入图像。我有以下代码:

# import 
import cv2
import numpy as np

param_kernel = 45
param_kernel2 = 2
kernel = np.ones((param_kernel, param_kernel), np.uint8) 
kernel2 = np.ones((param_kernel2, param_kernel2), np.uint8) 

GMM_image = cv2.imread('frameGMMmorf5822a.jpg',0)
closing = cv2.morphologyEx(GMM_image, cv2.MORPH_CLOSE, kernel) 

cv2.imshow('Input image', GMM_image) 
cv2.waitKey(0)
cv2.imshow('Closing input image', closing)
cv2.waitKey(0)

area=[]
maskcont = np.zeros(GMM_image.shape, dtype=np.uint8)
contours, c = cv2.findContours(closing, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
for id_cnt,cnt in enumerate(contours):
    cv2.drawContours(maskcont, [cnt], 0, (75), 2)
    area.append(cv2.contourArea(cnt))
    
cv2.imshow('Contours around closed regions', maskcont) 
cv2.waitKey(0)

final_contours=[]
for id_i,i in enumerate(area):
    if i > 2500:
        final_contours.append(contours[id_i])
maskcontfinal = np.zeros(GMM_image.shape, dtype=np.uint8)
GMM_image_cnt = GMM_image.copy()    
for id_finalcnt,finalcnt in enumerate(final_contours):
    cv2.drawContours(maskcontfinal, [finalcnt] , 0, (75), 2)
    cv2.drawContours(GMM_image_cnt, [finalcnt] , 0, (75), 2)
    
cv2.imshow('Only keep regions with large area', maskcontfinal) 
cv2.waitKey(0)
cv2.imshow('Unwanted pixels inside grey-contour', GMM_image_cnt) 
cv2.waitKey(0)

在标题为“灰色轮廓内不需要的像素”的最后一张图像中,我想将灰色轮廓内的所有白色像素都放入黄色像素。 我找不到将灰色轮廓内的所有白色像素都放入黄色像素的方法......有人可以帮忙吗?

输入图像: enter image description here

带有灰色轮廓和灰色轮廓内的白色像素的输出图像: enter image description here

0 个答案:

没有答案
相关问题