How to replace cropped rectangle in opencv?

2024/11/16 14:48:17

I have managed to cropped a bounding box with text, e.g. given this image:

enter image description here

I'm able to exact the following box:

enter image description here

with this code:

import re
import shutilfrom IPython.display import Imageimport requests
import pytesseract, cv2"""https://www.geeksforgeeks.org/text-detection-and-extraction-using-opencv-and-ocr/"""
# Preprocessing the image starts
# Convert the image to gray scale
img = cv2.imread('img.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# Performing OTSU threshold
ret, thresh1 = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)# Specify structure shape and kernel size.
# Kernel size increases or decreases the area
# of the rectangle to be detected.
# A smaller value like (10, 10) will detect
# each word instead of a sentence.
rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (18, 18))# Applying dilation on the threshold image
dilation = cv2.dilate(thresh1, rect_kernel, iterations = 1)# Finding contours
contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)# Creating a copy of image
im2 = img.copy()for cnt in contours:x, y, w, h = cv2.boundingRect(cnt)# Drawing a rectangle on copied imagerect = cv2.rectangle(im2, (x, y), (x + w, y + h), (0, 255, 0), 2)# Cropping the text block for giving input to OCRcropped = im2[y:y + h, x:x + w]cv2.imwrite('image-notxt.png', cropped)
Image(filename='image-notxt.png',  width=200)

Part 1: How do I replace the cropped box and put back a clear text box? e.g. to get something like:

enter image description here

I've tried:

    for cnt in contours:x, y, w, h = cv2.boundingRect(cnt)# Drawing a rectangle on copied imagerect = cv2.rectangle(im2, (x, y), (x + w, y + h), (0, 255, 0), 2)# Cropping the text block for giving input to OCRcropped = im2[y:y + h, x:x + w]text = pytesseract.image_to_string(cropped).strip('\x0c').strip()text = re.sub(' +', ' ', text.replace('\n', ' ')).strip()if text:# White out the cropped box.cropped.fill(255)# Create the image with the translation.cv2.putText(img=cropped, text="foobar", org=(12, 15), fontFace=cv2.FONT_HERSHEY_TRIPLEX, fontScale=0.3, color=(0, 0, 0),thickness=1)cv2.imwrite('image-notxt.png', cropped)Image(filename='image-notxt.png',  width=200)

That managed to white out the cropped box and insert the text like this:

enter image description here

Part 2: How to create an opencv textbox rectangle with the same size as the cropped box? e.g. given a string foobar, how to get the final image like this:

enter image description here

Answer

In Python/OpenCV/Numpy, use Numpy to write a color to the area in the format:

img[y:y+h, x:x+w] = color tuple 

For example:

enter image description here

# Part 1 --- Blank Area# read image
photo = cv2.imread('Wurst.png')# define region arguments
x,y,w,h = 40,40,150,45# blank area
result1 = photo.copy()
result1[40:40+45, 40:40+150] = (255,255,255)# save results
cv2.imwrite("Wurst_blank_result.png", result1)# show the results
cv2.imshow("blanked result", result1)
cv2.waitKey(0)# Part 2 --- Write text# define text arguments
text = "FOOBAR"
thickness = 2
scale = .8
pad = 2# determine size for text image
(wd, ht), baseLine = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, scale, thickness)
print (wd, ht, baseLine)# add black text to white background image padded all around
th = ht + 2 * pad
tw = wd + 2 * pad
text_img = np.full((th,tw,3), (255,255,255), dtype=np.uint8)
text_img = cv2.putText(text_img, text, (pad,ht+pad), cv2.FONT_HERSHEY_SIMPLEX, scale, (0,0,0), thickness)
print(text_img.shape)# insert text image into region of photo
if w>=tw and h>=th:# compute offsetoffx = int((w-tw)/2)offy = int((h-th)/2)# combine the text with the imageresult2 = result1.copy()result2[y+offy:y+offy+th, x+offx:x+offx+tw] = text_img# save resultscv2.imwrite("Wurst_text_result.png", result2)# show the resultscv2.imshow("text image", text_img)cv2.imshow("text result", result2)cv2.waitKey(0)
else:print("Text Is Too Large")

enter image description here

See cv2.getTextSize() at https://docs.opencv.org/4.1.1/d6/d6e/group__imgproc__draw.html#ga3d2abfcb995fd2db908c8288199dba82 and cv2.putText() at https://docs.opencv.org/4.1.1/d6/d6e/group__imgproc__draw.html#ga5126f47f883d730f633d74f07456c576

https://en.xdnf.cn/q/119519.html

Related Q&A

How can I read hexadecimal data with python?

I have this c# app that Im trying to cooperate with a app written in python. The c# app send simple commands to the python app, for instance my c# app is sending the following:[Flags]public enum GameRo…

Want to scrape all the specific href from the a tag

I have search the specific brand Samsung , for this number of products are search ,I just wanted to scrape all the href from the of the search products with the product name . enter code here import u…

Encryption code in def function to be written in python

need some help in the following code as it goes into infinite loop and does not validate user input: the get_offset is the function. Just edited need some help with the encryption part to be done in a …

Creating xml from MySQL query with Python and lxml

I am trying to use Python and LXML to create an XML file from a Mysql query result. Here is the format I want.<DATA><ROW><FIELD1>content</FIELD1><FIELD2>content</FIELD2…

How to add another iterator to nested loop in python without additional loop?

I am trying to add a date to my nested loop without creating another loop. End is my list of dates and end(len) is equal to len(year). Alternatively I can add the date to the dataframe (data1) is that …

How to know where the arrow ends in matplotlib quiver

I have programmed plt.quiver(x,y,u,v,color), where there are arrows that start at x,y and the direction is determined by u,v. My question is how can I know exactly where the arrow ends?

how send text(sendkey) to ckeditor in selenium python scripting -chrome driver

I cant send a text to the text box of CKEditor while I scripting.it not shown in the seleniumIDE recording also.Help me to fix this issueASAP

How to replace the column of dataframe based on priority order?

I have a dataframe as follows df["Annotations"] missense_variant&splice_region_variant stop_gained&splice_region_variant splice_acceptor_variant&coding_sequence_variant&intron…

Scraping from web page and reformatting to a calender file

Im trying to scrape this site: http://stats.swehockey.se/ScheduleAndResults/Schedule/3940And Ive gotten as far (thanks to alecxe) as retrieving the date and teams.from scrapy.item import Item, Field fr…

Python Text to Data Frame with Specific Pattern

I am trying to convert a bunch of text files into a data frame using Pandas. Thanks to Stack Overflows amazing community, I almost got the desired output (OP: Python Text File to Data Frame with Specif…