python检测范围

时间:2016-02-22 22:03:37

标签: python opencv pillow

背景

我正在构建一个程序,将消息传递应用程序的屏幕截图转换为文本。一步是使用开放的CV matchTemplate函数来查找屏幕截图中是否存在任何表情符号。由于我必须迭代超过1600个表情符号字符,我想首先检查屏幕截图,看看它是否有一个范围之外的颜色(蓝色和白色)。如果此检查为真,我将运行耗时的模板匹配方法。

问题

根据下面的截图,如何找到蓝色和白色以外的颜色(背景和文字颜色?我正在考虑使用openCV inRange来查找与蓝色和白色匹配的颜色,计算不要的像素数匹配,并查看该数字是否小于图像的总像素数。

问题

我定义的颜色范围太宽,无法处理字体周围的所有锯齿。字体的边缘是各种蓝色。如果我定义范围很大,我可能会错过找到表情符号。

enter image description here

到目前为止

代码

# 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!!!

1 个答案:

答案 0 :(得分:0)

我的解决方案。

# 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"