通过图像中的颜色选择对象? [英] Select object by color in an image?

查看:107
本文介绍了通过图像中的颜色选择对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以找到具有特定颜色的对象(例如,带有白色文本的红色矩形100px 50px),然后按原样选择该对象并将其剪切为新文件?看下面的图片。我正在尝试制作一个脚本,用于从图像中选择数据,然后转换为文本,最后写入Excel。



我用Google搜索了很多howtos,但是没有找到任何解决我问题的方法。





首先,我们减少除红色外的所有颜色。 描述了我们要过滤的BGR值(RGB =红色,绿色,蓝色)。

 图像= cv.imread( AR87t.jpg)

低= np.array ([0,0,200])
upper = np.array([100,100,255])
shapeMask = cv.inRange(图像,下部,上部)

cv.imshow( obj shapeMask,shapeMask)
cv.waitKey(0)

显示如下:






寻找轮廓

接下来,我们找到轮廓并进行迭代。如果找到4个角,我们将做下一个工作...

  cnts = cv.findContours(shapeMask.copy() ,cv.RETR_EXTERNAL,
cv.CHAIN_APPROX_SIMPLE)[0]

for c in cnts:
peri = cv.arcLength(c,True)
rox = cv .approxPolyDP(c,0.04 * peri,True)如果len(approx)== 4:
....





掩盖原始

使用boundingRect,我们提取 x y w h

 (x,y,w,h)= cv.boundingRect(大约)
cv.rectangle(图像,(x,y),(x + w,y + h),(0,255,0),厚度= 5)








ocr在面具上

魔术来了!首先,我们提取蒙版部分并将openCV图像导出到PIL图像。然后我们就可以运行tesseract。

  el = shapeMask.copy()[y:y + h,x:x + w] 
pil_im = Image.fromarray(el)

cv.imshow( obj,el)
cv.waitKey(0)

print(pytesseract.image_to_string(pil_im))



这显示了每个矩形作为小图像。您的控制台将打印出:

  L2 = 33,33 
L3 = 44,44
L1 = 12,22





代码

 导入cv2为cv 
导入numpy为np
从PIL导入pytesseract
导入图像



图像= cv.imread( AR87t.jpg)

低= np。 array([0,0,200])
upper = np.array([100,100,255])
shapeMask = cv.inRange(image,lower,upper)

cv.imshow( obj shapeMask,shapeMask)
cv.waitKey(0)


cnts = cv.findContours(shapeMask.copy(),cv.RETR_EXTERNAL ,
cv.CHAIN_APPROX_SIMPLE)[0]

以c为单位:
peri = cv.arcLength(c,True)
rox = cv.approxPolyDP(c ,0.04 * peri,True)如果len(approx)== 4:
(x,y,w,h)= cv.boundingRect(大约)
cv.rectangle(image, (x,y),(x + w,y + h),(0,255,0),厚度= 5)

print( w:%s, y:%s,w:%s,h:%s%(x,y,w,h))

el = shapeMask.copy()[y:y + h,x: x + w]
pil_im = Image.fromarray(el)

cv.imshow( obj,el)
cv.waitKey(0)

print(pytesseract.image_to_string(pil_im))


cv.imshow( obj矩形,图像)
cv.waitKey(0)


Is there a way to find object that have specific color (for example red rectangle 100px 50px with white text) and then select that object as it is and cut it to new file? Look at the picture below. I'm trying to make a script for selecting data from image, then convert to text and finally write to Excel.

I googled a lot of howtos but didn't find any that address my problem.

Sample image

解决方案

I don't know your real intention, would you like only read the text or do you like also extract the parts? Anyway, I'm going to show you a straight forward and general solution. Take the parts you need, at the end you find the hole code.

For the hole bunch you need 4 modules:
cv2 (openCV) for image processing
numpy to handle special operations on the images
pytesseract to recognize text (ocr)
pillow (pil) to prepare the image for pytesseract

Load und filter

Your original image:

First we reduce all colors except red. lower and upper describes the values from BGR (RGB = red, green, blue) we like to filter.

image = cv.imread("AR87t.jpg")

lower = np.array([0, 0, 200])
upper = np.array([100, 100, 255])
shapeMask = cv.inRange(image, lower, upper)

cv.imshow("obj shapeMask", shapeMask)
cv.waitKey(0)

This shows:


finding contours
Next, we find the contours and iterating through. If we find 4 corners, we will do the next stuff...

cnts = cv.findContours(shapeMask.copy(), cv.RETR_EXTERNAL,
                       cv.CHAIN_APPROX_SIMPLE)[0]

for c in cnts:
    peri = cv.arcLength(c, True)
    approx = cv.approxPolyDP(c, 0.04 * peri, True)
    if len(approx) == 4:
    ....



mask the original
With boundingRect, we extract x, y, w, h

(x, y, w, h) = cv.boundingRect(approx)
cv.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), thickness=5)




ocr on the mask
And here comes the magic! First we extract the mask parts and export the openCV image to an PIL image. We are then able to run tesseract over.

el = shapeMask.copy()[y:y + h, x:x + w]
pil_im = Image.fromarray(el)

cv.imshow("obj", el)
cv.waitKey(0)

print(pytesseract.image_to_string(pil_im))


this shows you every rectangle as small image. You console will print out:

L2 = 33,33
L3 = 44,44
L1 = 12,22



code

import cv2 as cv
import numpy as np
import pytesseract
from PIL import Image



image = cv.imread("AR87t.jpg")

lower = np.array([0, 0, 200])
upper = np.array([100, 100, 255])
shapeMask = cv.inRange(image, lower, upper)

cv.imshow("obj shapeMask", shapeMask)
cv.waitKey(0)


cnts = cv.findContours(shapeMask.copy(), cv.RETR_EXTERNAL,
                       cv.CHAIN_APPROX_SIMPLE)[0]

for c in cnts:
    peri = cv.arcLength(c, True)
    approx = cv.approxPolyDP(c, 0.04 * peri, True)
    if len(approx) == 4:
        (x, y, w, h) = cv.boundingRect(approx)
        cv.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), thickness=5)

        print("w:%s, y:%s, w:%s, h:%s" % (x, y, w, h))

        el = shapeMask.copy()[y:y + h, x:x + w]
        pil_im = Image.fromarray(el)

        cv.imshow("obj", el)
        cv.waitKey(0)

        print(pytesseract.image_to_string(pil_im))


cv.imshow("obj rectangle", image)
cv.waitKey(0)

这篇关于通过图像中的颜色选择对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆