Zack12
asked on
Save bounding box as image
I have some python code that takes in an image of an A4 printed letter, then draws bounding boxes around each character.
I want to know how to save each bounding box as an image, so essentially it's taking every character it detects and saving it. Preferable as a .png resized to 20x20
Here is my code:
I've tried a few solutions such as adding the following in my for loop
image_patch = img[minc:maxc, minr:maxr] # get region of interest (slice)
plt.imsave("filename.png", image_patch)
But that doesn't obtain the right boundaries for some reason.
The hard part is already done, (Drawing the boundries around the characters) I literally just want to save each boundry as an image now but I have no idea how.
I want to know how to save each bounding box as an image, so essentially it's taking every character it detects and saving it. Preferable as a .png resized to 20x20
Here is my code:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from scipy.misc import imread,imresize
from skimage.segmentation import clear_border
from skimage.morphology import label
from skimage.measure import regionprops
image = imread('./ocr/testing/adobe.png',1)
#apply threshold in order to make the image binary
bw = image < 120
# remove artifacts connected to image border
cleared = bw.copy()
clear_border(cleared)
# label image regions
label_image = label(cleared,neighbors=8)
borders = np.logical_xor(bw, cleared)
label_image[borders] = -1
print label_image.max()
fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
ax.imshow(bw, cmap='jet')
for region in regionprops(label_image, ['Area', 'BoundingBox']):
# skip small images
if region['Area'] > 50:
# draw rectangle around segmented characters
minr, minc, maxr, maxc = region['BoundingBox']
rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
fill=False, edgecolor='red', linewidth=2)
ax.add_patch(rect)
plt.show()
I've tried a few solutions such as adding the following in my for loop
image_patch = img[minc:maxc, minr:maxr] # get region of interest (slice)
plt.imsave("filename.png",
But that doesn't obtain the right boundaries for some reason.
The hard part is already done, (Drawing the boundries around the characters) I literally just want to save each boundry as an image now but I have no idea how.
ASKER
Hi there,
What I meant by "it doesn't obtain the right boundaries" is that the program detects the characters just fine, and places red bounding boxes around each character; however when I try use
The image that is saved is not the content within the bounding boxes. Instead I get a lot of empty images and half-letters cut off, so this doesn't represent the right dimensions that was used for the bounding boxes.
I modified the code as you mentioned above and I noticed a slightly different area of the image was being saved, I modified a bit more but it was pointless as it never actually captures a single character like the bounding box.
I guess what I really need is the co-ordinates of the bounding boxes
And converted correctly into the
Thank you
What I meant by "it doesn't obtain the right boundaries" is that the program detects the characters just fine, and places red bounding boxes around each character; however when I try use
image_patch = img[minc:maxc, minr:maxr]
The image that is saved is not the content within the bounding boxes. Instead I get a lot of empty images and half-letters cut off, so this doesn't represent the right dimensions that was used for the bounding boxes.
I modified the code as you mentioned above and I noticed a slightly different area of the image was being saved, I modified a bit more but it was pointless as it never actually captures a single character like the bounding box.
I guess what I really need is the co-ordinates of the bounding boxes
rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr, fill=False, edgecolor='red', linewidth=2)
And converted correctly into the
image_patch = img[eminc:emaxc, eminr:emaxr]
code, unless there's a really easy way I can just save rect as an image?Thank you
I newver used scypi image manipulation, but I'm a little surprised by the order of coordinate.
minr, minc, maxr, maxc = region['BoundingBox']
rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
fill=False, edgecolor='red', linewidth=2)
which of the functions retunrs xcoordinates first and which requires y coordinates first???
perhaps all you have to do is to swap x, y???
minr, minc, maxr, maxc = region['BoundingBox']
rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
fill=False, edgecolor='red', linewidth=2)
which of the functions retunrs xcoordinates first and which requires y coordinates first???
perhaps all you have to do is to swap x, y???
image_patch = img[eminr:emaxr, eminc:emaxc]
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Are you unable to extract a regular region from the image and save it as png or is the problem 'just',
that you get the image of one character, but that you don't see the bounding box?
If it's just the bounding box, that's missing, then this is probably related to the fact, that
the line of the bounding box is drawn on the outside of the region, that you specified with minc:maxc minr:maxr
In order to test this hypothesis, you could try to reduce the min values by 1 and increase the maxx value by 1.
Open in new window