blech
commit
72ba9964bb
|
|
@ -0,0 +1,30 @@
|
|||
# https://stackoverflow.com/questions/24385714/detect-text-region-in-image-using-opencv
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
# Load image, convert to HSV format, define lower/upper ranges, and perform
|
||||
# color segmentation to create a binary mask
|
||||
image = cv2.imread('1.jpg')
|
||||
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
|
||||
lower = np.array([0, 0, 218])
|
||||
upper = np.array([157, 54, 255])
|
||||
mask = cv2.inRange(hsv, lower, upper)
|
||||
|
||||
# Create horizontal kernel and dilate to connect text characters
|
||||
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,3))
|
||||
dilate = cv2.dilate(mask, kernel, iterations=5)
|
||||
|
||||
# Find contours and filter using aspect ratio
|
||||
# Remove non-text contours by filling in the contour
|
||||
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
||||
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
|
||||
for c in cnts:
|
||||
x,y,w,h = cv2.boundingRect(c)
|
||||
ar = w / float(h)
|
||||
if ar < 5:
|
||||
cv2.drawContours(dilate, [c], -1, (0,0,0), -1)
|
||||
|
||||
# Bitwise dilated image with mask, invert, then OCR
|
||||
result = 255 - cv2.bitwise_and(dilate, mask)
|
||||
result.show()
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
import cv2
|
||||
import numpy as np
|
||||
from PIL import Image, ImageDraw
|
||||
|
||||
def dbg(arr2d):
|
||||
dbg = {}
|
||||
for x in arr2d:
|
||||
for y in x:
|
||||
dbg[y] = dbg.get(y, 0) + 1
|
||||
print(dbg)
|
||||
#return
|
||||
img = Image.new("1", (max([len(x) for x in arr2d]), len(arr2d)))
|
||||
draw = ImageDraw.Draw(img)
|
||||
for x in range(len(arr2d)):
|
||||
for y in range(len(arr2d[x])):
|
||||
draw.point( (y,x), fill=1 if arr2d[x][y] > 100 else 0)
|
||||
img.show()
|
||||
|
||||
def draw_boxes(arr2d, boxes):
|
||||
img = Image.new("1", (max([len(x) for x in arr2d]), len(arr2d)))
|
||||
draw = ImageDraw.Draw(img)
|
||||
for x in range(len(arr2d)):
|
||||
for y in range(len(arr2d[x])):
|
||||
draw.point( (y,x), fill=1 if arr2d[x][y] > 100 else 0)
|
||||
for box in boxes:
|
||||
x0 = box[0]
|
||||
y0 = box[1]
|
||||
x1 = box[0] + box[2]
|
||||
y1 = box[1] + box[3]
|
||||
draw.line( [(x0,y0), (x1,y0)], fill=1)
|
||||
draw.line( [(x1,y0), (x1,y1)], fill=1)
|
||||
draw.line( [(x1,y1), (x0,y1)], fill=1)
|
||||
draw.line( [(x0,y1), (x0,y0)], fill=1)
|
||||
img.show()
|
||||
|
||||
# Load image, convert to HSV format, define lower/upper ranges, and perform
|
||||
# color segmentation to create a binary mask
|
||||
image = cv2.imread('1.png')
|
||||
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
|
||||
lower = np.array([0, 0, 0])
|
||||
upper = np.array([0, 255, 200])
|
||||
mask = cv2.inRange(hsv, lower, upper)
|
||||
#dbg(mask)
|
||||
|
||||
# Create horizontal kernel and dilate to connect text characters
|
||||
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,3))
|
||||
dilate = cv2.dilate(mask, kernel, iterations=0)
|
||||
#dbg(dilate)
|
||||
|
||||
# Find contours and filter using aspect ratio
|
||||
# Remove non-text contours by filling in the contour
|
||||
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
||||
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
|
||||
draw_boxes(dilate, [cv2.boundingRect(c) for c in cnts])
|
||||
for c in cnts:
|
||||
x,y,w,h = cv2.boundingRect(c)
|
||||
ar = w / float(h)
|
||||
print(x, y, w, h)
|
||||
if ar < 5:
|
||||
cv2.drawContours(dilate, [c], -1, (0,0,0), -1)
|
||||
#dbg(dilate)
|
||||
exit()
|
||||
|
||||
# Bitwise dilated image with mask, invert, then OCR
|
||||
result = 255 - cv2.bitwise_and(dilate, mask)
|
||||
dbg(result)
|
||||
Loading…
Reference in New Issue