Home > other >  Counting curves, angles and straights in a binary image in openCV and python
Counting curves, angles and straights in a binary image in openCV and python

Time:05-07

I want to write a tool for finding the number of angles, curves and straight lines within each bounded object in an image. All input images will be black on white background and all will represent characters.

As illustrated in the image, for each bounded region, each shape occurrence is noted. It would be preferable to be able to have a threshold for how curvy a curve must be to be considered a curve and not an angle etc. And the same for straight lines and angles.

I have used Hough Line Transform for detecting straight lines on other images and it might work in combination with something here I thought.

Am open to other libraries than opencv - this is just what I have some experience with.

Thanks in advance

IMAGE: Occurences of shape in the letters

EDIT: So based on the answer from Markus, I made a program using the findContours() with CHAIN_APPROX_SIMPLE.

It produces a somewhat wierd result inputting a 'k' where it correctly identifies some points around the angles but then the 'leg' (the lower diagonal part) has many many points on it. I am unsure how to go about segmenting this to group into straights, angles and curves.

Code:

import numpy as np

img = cv2.imread('Helvetica-K.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
edges = cv2.Canny(blurred, 50, 150, apertureSize=3)
ret, thresh = cv2.threshold(gray, 180, 255, cv2.THRESH_BINARY_INV)

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#cv2.drawContours(img, contours, 0, (0,255,0), 1)

#Coordinates of each contour
for i in range(len(contours[0])):
    print(contours[0][i][0][0])
    print(contours[0][i][0][1])
    cv2.circle(img, (contours[0][i][0][0], contours[0][i][0][1]), 2, (0,0,255), -1)

cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Img example: K-helvitica

CodePudding user response:

You can use enter image description here

  • Related