What I want :
When the user clicks on a button, the program takes the screnshot in the clipboard 'no problem with this part), detects Zone Of Interest (ZOI), cuts the zone and extracts the text of multiple zones for my programme. I try to extract information from a prescription. I know the zones of interests will always be in the same rectangle of color but I'm not sure the prescription will be in fullsize each time I need to extract information. You can see the original image and what I want the ZOI are in red.
- First time I tried making the contour of the zone using the color in the font. The program can make a zone around the grey specific zone but the isn't what I want (green box). You can see above the original image, post-process image and the code
import numpy as np
import cv2
# Read input image
img = cv2.imread('test_image.png')
gray = np.all(img == (227,227,227), 2)
# Convert logical matrix to uint8
gray = gray.astype(np.uint8)*255
# Find contours
cnts = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2] # Use index [-2] to be compatible to OpenCV 3 and 4
# Get contour with maximum area
c = max(cnts, key=cv2.contourArea)
x, y, w, h = cv2.boundingRect(c)
# Draw green rectangle for testing
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), thickness = 2)
# Show result
cv2.imshow('gray', gray)
cv2.imwrite('niveau_gris.jpg', gray)
cv2.imshow('img', img)
cv2.imwrite('test_image_resultat.jpg', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
- Second time我试着用两个参数绘制区域轮廓,目的是用文本 Select 区域.该程序根本没有创建区域.您可以看到下面的代码
import numpy as np
import cv2
frame = cv2.imread('test_image_constrasate.jpg')
# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# define range of red color in HSV
lower_red = np.array([189,189,189])
upper_red = np.array([204,203,204])
mask = cv2.inRange (hsv, lower_red, upper_red)
contours = cv2.findContours(mask.copy(),
cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)[-2]
if len(contours) > 0:
red_area = max(contours, key=cv2.contourArea)
x, y, w, h = cv2.boundingRect(red_area)
cv2.rectangle(frame,(x, y),(x+w, y+h),(0, 0, 255), 2)
cv2.imshow('frame', frame)
cv2.imshow('mask', mask)
cv2.waitKey(0)
- Third time我try 使用Hough线变换,但我对处理图像的实验不够,我不确定该过程的重复性,因为屏幕截图可能不是全尺寸的,我不确定如何使用线来制作方框.
provide research
我在StackoverFlow上搜索了以下术语:"OpenCv Select 矩形"、"OpenCv Select 基于 colored颜色 的区域"、"OpenCv如何基于 colored颜色 Select 区域"....
你能帮帮我吗?感谢您今后的帮助