Ask Your Question
0

Calculate slope, length and angle of a specific part / side / line on a contour?

asked 2019-01-03 16:01:16 -0600

sonicdoo gravatar image

Original Picture

I got two detected contours in an image and need the diameter between the two vertical-edges of the top contour and the diameter between the vertical-edges of the lower contour. I achieved this with this code.

import cv2
import numpy as np
import math, os
import imutils

img = cv2.imread("1.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)
edges = cv2.Canny(gray, 200, 100)
edges = cv2.dilate(edges, None, iterations=1)
edges = cv2.erode(edges, None, iterations=1)

cnts = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

# sorting the contours to find the largest and smallest one
c1 = max(cnts, key=cv2.contourArea)
c2 = min(cnts, key=cv2.contourArea)

# determine the most extreme points along the contours
extLeft1 = tuple(c1[c1[:, :, 0].argmin()][0])
extRight1 = tuple(c1[c1[:, :, 0].argmax()][0])
extLeft2 = tuple(c2[c2[:, :, 0].argmin()][0])
extRight2 = tuple(c2[c2[:, :, 0].argmax()][0])

# show contour
cimg = cv2.drawContours(img, cnts, -1, (0,200,0), 2)

# set y of left point to y of right point
lst1 = list(extLeft1)
lst1[1] = extRight1[1]
extLeft1 = tuple(lst1)

lst2 = list(extLeft2)
lst2[1] = extRight2[1]
extLeft2= tuple(lst2)

# compute the distance between the points (x1, y1) and (x2, y2)
dist1 = math.sqrt( ((extLeft1[0]-extRight1[0])**2)+((extLeft1[1]-extRight1[1])**2) )
dist2 = math.sqrt( ((extLeft2[0]-extRight2[0])**2)+((extLeft2[1]-extRight2[1])**2) )

# draw lines
cv2.line(cimg, extLeft1, extRight1, (255,0,0), 1)
cv2.line(cimg, extLeft2, extRight2, (255,0,0), 1)

# draw the distance text
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 0.5
fontColor = (255,0,0)
lineType = 1
cv2.putText(cimg,str(dist1),(155,100),font, fontScale, fontColor, lineType)
cv2.putText(cimg,str(dist2),(155,280),font, fontScale, fontColor, lineType)

# show image
cv2.imshow("Image", img)
cv2.waitKey(0)

On the next Image you see the Output (green /blue)

Now I would also need the angle of the slope lines (red) on the bottom side of the upper contour.

Output 1

Any ideas how I can get this? Is it possible using contours?

Or is it necessary to use HoughLinesP and sort the regarding lines somehow?

And continued question: Maybe its also possible to get function which describes parabola slope of that sides ?

Demo

Thanks for any help =)

edit retag flag offensive close merge delete

Comments

Do you know the basic definitions of slope, angle, and length?

sjhalayka gravatar imagesjhalayka ( 2019-01-03 20:34:07 -0600 )edit

Try to use approxPolyDP

LBerger gravatar imageLBerger ( 2019-01-03 23:07:26 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-01-04 02:20:42 -0600

sonicdoo gravatar image

updated 2019-01-04 02:20:53 -0600

thanks for the hints. LBerger if I want to use approxPolyDP, how do I get the requiered InputArray curce out of the contour? maybe you can help me with some code? thanks!

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-01-03 16:01:16 -0600

Seen: 7,526 times

Last updated: Jan 03 '19