-2

Im creating a system that will be able to register people who will have written texts by hand, later I will have to analyze this image and detect if the writing is ascending, descending or straight. With graphology, I will be able to create the person's profile, but I have no ideas on how to analyze that image.

System using Python and Django, i just need to read the image to make this analysis.Does anyone have a suggestion on how to do it?

Example of the inclinations

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). “Show me how to solve this coding problem” is not a Stack Overflow issue. We expect you to make an honest attempt, and *then* ask a *specific* question about your algorithm or technique. – Prune Sep 17 '20 at 21:19
  • Maybe apply some morphology to get a cluster of nice blobs and fit a straight line to their horizontal coordinate, then compute the angle counterclockwise between the line and a 0 degrees reference. – stateMachine Sep 17 '20 at 21:36
  • Get the angle of the rotated rectangle bound box (minAreaRect) from the contour after morphology to connect the text characters into one contiguous region – fmw42 Sep 17 '20 at 21:45
  • 1
    Very interesting problem, but unfortunately, SO is not the right place to ask this question. I have done something similar in the past. My recommendation would be to scale down the input image and then try to fit a straight line. You only need the slope of that line to decide whether the baseline is ascending, descending etc. A more interesting problem is when you want to calculate the slant (graphology term) of a given hand written text. If you happen to be active in a different forum where this kind of discussion is allowed, I'll be happy to continue the conversation. – Raiyan Sep 17 '20 at 23:49
  • Something similar to what @Raiyan suggested. You can also try finding the minimum area rectangle of the point set in the writing using `minAreaRect`, or try fitting an ellipse around the point set using `fitEllipse`. Both will give you a `RotatedRect` from which you can get the angle. – dhanushka Sep 18 '20 at 13:59

1 Answers1

5

A possible solution is to use the minAreaRect() that give you the angle. Once you have it, just set your thresh to say if the writing is ['Ascending', 'Descending', 'Level']:

write = cv2.imread('your_image.png', cv2.IMREAD_COLOR)
write_gray = cv2.cvtColor(write, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(write_gray, 150, 255, cv2.THRESH_BINARY_INV)
# dilate the write
elem = cv2.getStructuringElement(cv2.MORPH_RECT, (4, 4), (2, 2))
dilat = cv2.dilate(thresh, elem, iterations=1)
contours, hierarchy = cv2.findContours(dilat, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    if cv2.contourArea(cnt) > 1600: # only keep the writing text
        box = cv2.minAreaRect(cnt)
        (pos, size, angle) = box
        box = cv2.boxPoints(box)
        box = np.int0(box)
        cv2.drawContours(write, [box], 0, (0,255,0), 2)
        angle = angle if size[0] > size[1] else angle + 90
        pos = (cnt[cnt[:, :, 0].argmin()][0][0], cnt[cnt[:, :, 1].argmin()][0][1])
        #print(angle)
        if -2 <= angle <= 2:
            cv2.putText(write, 'Level', pos, cv2.FONT_HERSHEY_SIMPLEX, 0.7, (190, 123, 68), 2)
        elif angle < -2:
            cv2.putText(write, 'Ascending', pos, cv2.FONT_HERSHEY_SIMPLEX, 0.7, (190, 123, 68), 2)
        elif 2 < angle:
            cv2.putText(write, 'Descending', pos, cv2.FONT_HERSHEY_SIMPLEX, 0.7, (190, 123, 68), 2)

cv2.imshow('resultat', write)

You will have something like this: result

thibsc
  • 3,747
  • 2
  • 18
  • 38