我有这张图片。
我想将所有彩色标题设为白色,将其中的文本设为黑色。
我尝试在下面使图像全黑。
img_grey = cv2.imread('img.jpg', cv2.IMREAD_GRAYSCALE)
thresh = 170
img_binary = cv2.threshold(img_grey, thresh, 250, cv2.THRESH_BINARY)[1]
cv2.imwrite('bw_img.jpg',img_binary)
现在这些标题是黑色的,其中的文字是白色的。但我想让文本变黑,标题布局变白。那么,有人可以帮我吗?
答案 0 :(得分:1)
您可以将图像转换为 HSV,应用阈值来查找彩色区域,并将 cv2.threshold
和 cv2.THRESH_BINARY_INV
的结果仅复制到彩色区域。
建议解决方案的主要阶段:
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
):
由于输入图像的质量相对较低,结果看起来不太好。
中间结果: