1

I am using MSER from opencv-python to detect text using the code from this stackoverflow question. Can anyone help me understand why the contour p is being reshaped to (-1, 1, 2) before computing the convex hull of the objects?

The code is as below:

import cv2
import numpy as np

#Create MSER object
mser = cv2.MSER_create()

#Your image path i-e receipt path
img = cv2.imread('/home/rafiullah/PycharmProjects/python-ocr-master/receipts/73.jpg')

#Convert to gray scale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

vis = img.copy()

#detect regions in gray scale image
regions, _ = mser.detectRegions(gray)

hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]

cv2.polylines(vis, hulls, 1, (0, 255, 0))

cv2.imshow('img', vis)

cv2.waitKey(0)

mask = np.zeros((img.shape[0], img.shape[1], 1), dtype=np.uint8)

for contour in hulls:

    cv2.drawContours(mask, [contour], -1, (255, 255, 255), -1)

#this is used to find only text regions, remaining are ignored
text_only = cv2.bitwise_and(img, img, mask=mask)

cv2.imshow("text only", text_only)

cv2.waitKey(0)
Shreyas Moolya
  • 339
  • 4
  • 19
  • What is your question? What does `numpy.rehsape()` do? Why is the first argument -1? Or why is `mser.detectRegions()`'s output and `cv2.convexHull()`'s input in different format? – zteffi Aug 27 '19 at 12:11

1 Answers1

2

It doesn't matter if you reshape or not.

The reshaping is unnecessary. cv2.convexHull() can take either input format. The following images show that the results are the same whether the contours in regions are reshaped or not.

hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]
hulls1 = [cv2.convexHull(p) for p in regions]

they are the same

This is how the p contour changes when it is reshaped:

>>> p
array([[305, 382],
       [306, 382],
       [308, 380],
       [309, 380]...


>>> p.reshape(-1, 1, 2)
array([[[305, 382]],    
       [[306, 382]],    
       [[308, 380]],    
       [[309, 380]]...
Stephen Meschke
  • 2,820
  • 1
  • 13
  • 25
  • There's not much difference in this image, but it may have some different contour regions in other images, though I have not tested it on other images. – Shreyas Moolya Aug 28 '19 at 06:57