您可以将图像转换为 HSV,应用阈值来查找彩色区域,并将 cv2.threshold 和 cv2.THRESH_BINARY_INV 的结果仅复制到彩色区域。
建议解决方案的主要阶段:
- 从 BGR 转换为 HSV 颜色空间,并获得饱和颜色通道。
所有黑色和白色都为零,彩色像素高于零。
- 对饱和通道应用阈值。
- 在二值化饱和度通道上查找轮廓。
- 在黑色背景上将轮廓绘制为白色(255 个值)以形成蒙版。
- 应用形态闭合来闭合一些黑色间隙。
- 仅从
img_binary_inv 获取掩码内的区域(您的代码使用cv2.THRESH_BINARY_INV 的结果)。
- 仅将蒙版的
img_binary_inv 复制到img_grey,以蒙版为白色的像素为单位。
完整的代码示例:
import numpy as np
import cv2
img_bgr = cv2.imread('img.jpg') # Read image as BGR
img_grey = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY) # Convert from BGR to grayscale.
thresh = 170
img_binary_inv = cv2.threshold(img_grey, thresh, 255, cv2.THRESH_BINARY_INV)[1] # Apply threshold and invert black/white
# Convert from BGR to HSV color space.
hsv = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2HSV)
# Get the saturation color channel - all black and white are zero, and colored pixels are above zero.
s = hsv[:, :, 1]
thresh = 100
s_binary = cv2.threshold(s, thresh, 255, cv2.THRESH_BINARY)[1] # Apply threshold to the saturation channel.
# Find contours on s_binary
cnts = cv2.findContours(s_binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2] # Use index [-2] to be compatible to OpenCV 3 and 4
# Draw the contours as white (255 values) on black background.
mask = np.zeros_like(s_binary)
cv2.drawContours(mask, cnts, -1, 255, -1)
# Apply morphological closing for closing some black gaps.
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, np.ones((3, 3)))
# Get only the area inside the mask from img_binary_inv
masked_binary_inv = cv2.bitwise_or(img_binary_inv, img_binary_inv, mask=mask)
# Copy masked_binary_inv to img_grey only in pixels that mask is white.
cv2.copyTo(masked_binary_inv, mask, img_grey)
cv2.imwrite('img_grey.png', img_grey) # Save result (as PNG and not JPEG for better quality).
# Show images
cv2.imshow('img_bgr', img_bgr)
cv2.imshow('s_binary', s_binary)
cv2.imshow('mask', mask)
cv2.imshow('masked_binary_inv', masked_binary_inv)
cv2.imshow('img_grey', img_grey)
cv2.waitKey()
cv2.destroyAllWindows()
结果(img_grey):
由于输入图像的质量相对较低,结果看起来不太好。
中间结果:
s_binary:
mask:
masked_binary_inv: