背景
我正在构建一个程序,将消息应用程序的屏幕截图转换为文本。第一步是使用打开的 CV matchTemplate
函数来查找屏幕截图中是否存在任何表情符号。由于我必须迭代超过 1600 个表情符号字符,我想首先检查屏幕截图以查看它是否具有超出范围的颜色(蓝色和白色)。如果此检查为真,我将运行耗时的模板匹配方法。
问题
下面给出了一个截图,我如何找到蓝色和白色之外的颜色(背景和文本颜色?我正在考虑使用 openCV inRange
来找到匹配蓝色和白色的颜色,计算不匹配的像素,并查看该数字是否小于图像的像素总数。
问题
我定义的颜色范围太宽泛,无法处理字体周围的所有锯齿。字体的边缘是各种蓝色。如果我定义的范围过大,我可能会错过寻找表情符号的机会。
到目前为止的代码
# load the image and set up variables
image = cv2.imread(args["image"])
iW, iH = image.shape[:2]
pixels = iW * iH
masked = 0
# define the list of color boundaries
boundaries = [
([240, 130, 30], [255, 150, 80]), # blue
([250, 250, 250], [255, 255, 255]) # white
]
# loop over the boundaries
for (lower, upper) in boundaries:
# create NumPy arrays from the boundaries
lower = np.array(lower, dtype="uint8")
upper = np.array(upper, dtype="uint8")
# find the colors within the specified boundaries
mask = cv2.inRange(image, lower, upper)
for row in mask:
for px in row:
if px == 255:
masked += 1
if masked < pixels:
# probably has emojis!!!
最佳答案
我的解决方案。
# load the image and up some tracking variables
image = cv2.imread(args["image"])
accumMask = np.zeros(image.shape[:2], dtype="uint8")
# define the list of color boundaries
boundaries = [
([255, 140, 71], [255, 200, 200]),
([255, 150, 100], [255, 255, 255])
]
# loop over the boundaries
for (lower, upper) in boundaries:
# create NumPy arrays from the boundaries
lower = np.array(lower, dtype="uint8")
upper = np.array(upper, dtype="uint8")
# find the colors within the specified boundaries
mask = cv2.inRange(image, lower, upper)
# merge the mask into the accumulated masks
accumMask = cv2.bitwise_or(accumMask, mask)
accumMask = cv2.bitwise_not(accumMask)
# show the images
# cv2.imshow("images", np.hstack([accumMask]))
# cv2.waitKey(0)
unmasked = cv2.countNonZero(accumMask)
if unmasked:
print "has emoji"
else:
print "none"
关于python检测超出范围的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35564845/