Here's a receipt image that I've got and I've plotted it using matplotlib and If you see the image the text in it is not straight. How can I de-skew and fix it?
from skimage import io
import cv2
# x1, y1, x2, y2, x3, y3, x4, y4
bbox_coords = [[20, 68], [336, 68], [336, 100], [20, 100]]
image = io.imread('https://i.ibb.co/3WCsVBc/test.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
fig, ax = plt.subplots(figsize=(20, 20))
ax.imshow(gray, cmap='Greys_r')
# for plotting bounding box uncomment the two lines below
#rect = Polygon(bbox_coords, fill=False, linewidth=1, edgecolor='r')
#ax.add_patch(rect)
plt.show()
print(gray.shape)
(847, 486)
I think if we want to de-skew first we have to find the edges, so I tried to find the edges using canny algorithm and then get contours like below.
from skimage import filters, feature, measure
def edge_detector(image):
image = filters.gaussian(image, 2, mode='reflect')
edges = feature.canny(image)
contours = measure.find_contours(edges, 0.8)
return edges, contours
fig, ax = plt.subplots(figsize=(20, 20))
ax.imshow(gray, cmap='Greys_r');
gray_image, contours = edge_detector(gray)
for n, contour in enumerate(contours):
ax.plot(contour[:, 1], contour[:, 0], linewidth=2)
The edges that I've got from above code is the edges of each text but that is not what I needed. I need to get edges of receipt right?
Also I need a way to get the new bounding box coordinates after de-skewing the image (i.e straightening the image)?
If anyone has worked on similar problem please help me out? Thanks.
See Question&Answers more detail:
os