How can I extract a bounding box around a picture of a slide in Python using cv2?

42 Views Asked by At

I have an image of a slide taken on a RaspberryPi, and I eventually want to be able to extract the slide number, which will always be in the bottom-right corner. I know that I can just use Tesseract to do this part, but first I need to know where the slide is in the image, so I need to draw a bounding box around it.

Here is an example image: picture of a numbered slide

And here is what I'm looking for, and I eventually need to extract "503": numbered slide with bounding box

Another issue is that there's no guarantee that the user will take the image from this exact angle, and the slide might be in a room with different lighting. Is there a way to correct for that such that it can identify the bounding box around any image of any slide regardless of the lighting and angle and distance from the screen (or a way that it'll work in most cases)?

So far, I've followed this tutorial for car license plates. I've applied blurring, grayscaling, OTSU thresholding, but it just doesn't produce good results: preprocessed image

This is the code I used:

import cv2
import numpy as np

image = cv2.imread("extract_num2.jpg")

imgBlurred = cv2.GaussianBlur(image, (7, 7), 0)
         
gray = cv2.cvtColor(imgBlurred, cv2.COLOR_BGR2GRAY)
   
sobelx = cv2.Sobel(gray, cv2.CV_8U, 1, 0, ksize = 3) 
   
ret2, threshold_img = cv2.threshold(sobelx, 0, 255,
                   cv2.THRESH_BINARY + cv2.THRESH_OTSU)


cv2.imshow("Original Image", image)
cv2.imshow("Morphed", sobelx)
cv2.waitKey(0)
cv2.destroyAllWindows()

I don't have much experience in this kind of image processing, so any help is much appreciated!

0

There are 0 best solutions below