How do I detect the curvy lines in OpenCV?

asked 2018-02-24 09:28:32 -0500

Riya208 gravatar image

updated 2018-02-26 05:16:58 -0500

I am new in OpenCV and I would like to detect the curvy lines in an image. I tried Hough Transformation, but it detects only the straight line. I want to detect a line something similar to this, but not as curvy as this

Curvy lines

Thank you very much!

Regards

EDIT 1:

   import numpy as np
   import cv2

   # Read the main image

   inputImage = cv2.imread("input.png")

   # Convert it to grayscale

   inputImageGray = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)

   # Line Detection

   edges = cv2.Canny(inputImageGray,100,200,apertureSize = 3)

   minLineLength = 500 
   maxLineGap = 5 

   lines = cv2.HoughLinesP(edges,1,np.pi/180,90,minLineLength,maxLineGap)

   for x in range(0, len(lines)):
       for x1,y1,x2,y2 in lines[x]:
           cv2.line(inputImage,(x1,y1),(x2,y2),(0,128,0),2)

   cv2.putText(inputImage,"Tracks Detected", (500,250), font, 0.5, 255)

   # Show result

   cv2.imshow("Trolley_Problem_Result", inputImage)

This is my code, and the output of this code is (the green line in the image is the result):

Tracks detected

As you can see, the curved tracks are not detected properly. If I increase the threshold value, it detects the lines on the tram (it's detecting now too) and also on the humans.

What do you suggest, so that I can improve the code and detect the curved tracks as well?

Thanks a lot :)

Regards

edit retag flag offensive close merge delete

Comments

a curve is not a line. an there's nothing builtin specially to detect curves.

is it always the same curve ? you have to tell us more about the constraints.

berak gravatar imageberak ( 2018-02-24 09:59:45 -0500 )edit

Skeletonization (thinning) will give you a single-pixel-wide version of the curved line. Is that what you're looking for?

See: http://answers.opencv.org/question/18...

sjhalayka gravatar imagesjhalayka ( 2018-02-24 10:08:45 -0500 )edit

@berak: It's not always the same curve, but in general curves (what I mean it is always not a straight line). I want to detect the lines given in the image (http://nymag.com/selectall/2016/08/tr...). It's called Trolley Problem

Riya208 gravatar imageRiya208 ( 2018-02-24 10:15:16 -0500 )edit

detect always means: "something specific".

you still have to be more clear.

berak gravatar imageberak ( 2018-02-24 10:17:36 -0500 )edit

I want to detect the line given in the link (http://nymag.com/selectall/2016/08/tr...).

Riya208 gravatar imageRiya208 ( 2018-02-24 10:56:58 -0500 )edit

Use cv2.HoughLinesP. Then apply cv2.HOUGH_PROBABILISTIC.

supra56 gravatar imagesupra56 ( 2018-02-24 11:10:52 -0500 )edit

Hi @supra56, thank you very much. It almost works. I applied cv2.HoughLinesP, but I don't get it what you mean by cv2.HOUGH_PROBABILISTIC? It is one of the methods of cv2.houghlines, isn't it?

Riya208 gravatar imageRiya208 ( 2018-02-26 05:05:30 -0500 )edit

@Riya208. I used this for detected road lane. lines = cv2.HoughLinesP(thresh, cv2.HOUGH_PROBABILISTIC, np.pi/180, 90, minLineLength = 50, maxLineGap = 2) Don't use cv2.houghlines For better result use cv2.polylines.

supra56 gravatar imagesupra56 ( 2018-02-26 05:53:01 -0500 )edit

import numpy as np import cv2

inputImage = cv2.imread("input.png")
inputImageGray = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(inputImageGray,150,200,apertureSize = 3)
minLineLength = 30
maxLineGap = 5
lines = cv2.HoughLinesP(edges,cv2.HOUGH_PROBABILISTIC, np.pi/180, 30, minLineLength,maxLineGap)
for x in range(0, len(lines)):
    for x1,y1,x2,y2 in lines[x]:
        #cv2.line(inputImage,(x1,y1),(x2,y2),(0,128,0),2, cv2.LINE_AA)
        pts = np.array([[x1, y1 ], [x2 , y2]], np.int32)
        cv2.polylines(inputImage, [pts], True, (0,255,0))

font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(inputImage,"Tracks Detected", (500, 250), font, 0.5, 255)
cv2.imshow("Trolley_Problem_Result", inputImage)
cv2.imshow('edge', edges)
cv2.waitKey(0)
supra56 gravatar imagesupra56 ( 2018-02-26 08:02:31 -0500 )edit

@supra56 Thanks a ton!

Riya208 gravatar imageRiya208 ( 2018-03-18 21:04:01 -0500 )edit