这是一种可能的方法。如果转换为HSV色彩空间,蓝色阴影将显示比黑色和白色更高的饱和度,所以......
转换为HSV
找到每行的平均饱和度,并选择平均饱和度超过阈值的行
灰度化那些行,反转并阈值它们
如果反向(突出)背景是除黑色或白色之外的任何颜色,则此方法应该有效。它假设您根据示例将图像偏斜为真正的垂直/水平。
在Python中看起来像这样:
#!/usr/bin/env python3
import cv2
import numpy as np
# Load image
im = cv2.imread('form.jpg')
# Make HSV and extract S, i.e. Saturation
hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
s=hsv[:,:,1]
# Save saturation just for debug
cv2.imwrite('saturation.png',s)
# Make greyscale version and inverted, thresholded greyscale version
gr = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
_,grinv = cv2.threshold(gr,127,255,cv2.THRESH_BINARY_INV)
# Find row numbers of rows with colour in them
meanSatByRow=np.mean(s,axis=1)
rows = np.where(meanSatByRow>50)
# Replace selected rows with those from the inverted, thresholded image
gr[rows]=grinv[rows]
# Save result
cv2.imwrite('result.png',gr)
结果如下:
饱和度图像如下所示 - 请注意饱和色(即蓝色)显示为浅色,其他一切显示为黑色:
灰度,倒像图像如下所示: