So say I have lots of images similar to these ones:
I am trying to get the rectangle with the numbers inside, its the one with the lighter background, also I would like to get rid of those lines but that isn't really as important, here is what it should look like:
I really just don't know how to go about solving this.
Here is the code I have, it recognizes the shapes and outlines them in green,
import numpy as np
import matplotlib.pyplot as plt
import cv2
import sys
# read the image from arguments
image = cv2.imread('OCRimgs/test2.jpg')
# convert to grayscale
grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# perform edge detection
edges = cv2.Canny(grayscale, 30, 100)
# detect lines in the image using hough lines technique
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 60, np.array([]), 50, 5)
# iterate over the output lines and draw them
for line in lines:
for x1, y1, x2, y2 in line:
cv2.line(image, (x1, y1), (x2, y2), color=(20, 220, 20), thickness=3)
# show the image
plt.imshow(image)
plt.show()
output:
if it helps I am making a OCR that takes in images like the ones you saw at the top and gets their images, this might seem pointless but trust me once I finish it, it will be super helpful to me.
here is the code I have for the OCR(no bugs here)
import cv2
import pytesseract
# from PIL import Image
img = cv2.imread('small.png')
try:
t = pytesseract.image_to_string(img, lang='lets', config='--psm 6 tessedit_char_whitelist=0123456789')
text = int(t.replace('\n', '').replace('\f', ''))
print(text)
except:
print('Error processing image')
the images it takes in have to be similar to these:
edit:
I feel like there could be a way to do this by using the color of the rectangle because it is light, but I'm not sure it that will work as the real data will be images take from a camera (images will be taken from the same camera in the same spot)