Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I am having a problem with making my code more consize in opencv

import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd="D:\\Tesseract\\tesseract.exe"
i = cv2.imread('1.png')
himg,wimg,_ = i.shape
k= [b.split(' ') for b in pytesseract.image_to_boxes(i).splitlines()]
for x,y,w,h in  [int(x) for x in [a[1],a[2],a[3],a[4]] for a in k] :
    cv2.rectangle(i, (x, himg-y), (w, himg + h))

My end goal is to draw boxes around each letter in the image using cv2.rectangle(i, (x, himg-y), (w, himg + h)). Pls help with the last 2 lines. I want it as consize as possible

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

To draw boxes around each letter in the image, you can use a loop to iterate over the list of bounding boxes and draw a rectangle for each one:

for box in k:
    x, y, w, h = box[1], box[2], box[3], box[4]
    cv2.rectangle(i, (x, y), (x+w, y+h), (0, 255, 0), 2)

Complete code:

import cv2
import pytesseract

# Set the path to the Tesseract executable
pytesseract.pytesseract.tesseract_cmd = "D:\\Tesseract\\tesseract.exe"

# Read the image
i = cv2.imread('1.png')

# Extract the bounding boxes for each letter in the image
k = [b.split(' ') for b in pytesseract.image_to_boxes(i).splitlines()]

# Iterate over the bounding boxes and draw a rectangle around each letter
for box in k:
    x, y, w, h = box[1], box[2], box[3], box[4]
    cv2.rectangle(i, (x, y), (x+w, y+h), (0, 255, 0), 2)

# Show the image
cv2.imshow('image', i)
cv2.waitKey(0)
cv2.destroyAllWindows()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading