Home > database >  Unable to detect rectangle inside image using cloud vision python
Unable to detect rectangle inside image using cloud vision python

Time:07-20

I am new to image-processing. I have an image where a rectangle shaped content followed by a prefilled form. I would like to read content of the form where key, value pair available in multi lines.How to detect and get x,y, height, width of rectangle box from image. So that I could iterate to the next row to read my form content. This is the code I have tried. But unable to get valid Contours. Size of conts returns zero.

image = cv2.imread('F:/Python-Backend_Script/sample.jpeg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY)
cnts, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

My rectangle image border is very thin. And its like a dotted line.

enter image description here

CodePudding user response:

You can highlight the local detail with this code

import numpy as np
import cv2
import matplotlib.pyplot as plt


def downloadImage(URL):
    """Downloads the image on the URL, and convers to cv2 RGB format"""
    from io import BytesIO
    from PIL import Image as PIL_Image
    import requests

    response = requests.get(URL)
    image = PIL_Image.open(BytesIO(response.content))
    return cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)


URL = "https://i.stack.imgur.com/tfHij.jpg"

# Read image
# colorImage = downloadImage(URL)

img = cv2.cvtColor(downloadImage(URL), cv2.COLOR_RGB2GRAY)

# calculate local median
blurImg = cv2.medianBlur(img, 31)
# difference relative to local median
diff = cv2.absdiff(img, blurImg) / blurImg * 255
# local threshold to highlight max values
diff = cv2.threshold(diff, 10, 255, cv2.THRESH_BINARY)[1]

plt.imshow(diff, cmap="gray")
plt.show()

enter image description here

  • Related