# finding absolute straight lines/ rectangles for contours

While trying to detect the straight lines in an image, I'm unable to find the Hough transform of the image.

I have first converted the image to grayscale but unable to find the straight lines.

Thus, can you help me to make the contours which were detected as pure straight lines/rectangles. Such that I can easily find the horizontal lines(i will do it later) in the image.

def hou():
import math
#gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(img,50,150,apertureSize = 3)
minLineLength = 20
maxLineGap = 10
lines = cv2.HoughLinesP(edges,1,math.pi/180,100,minLineLength,maxLineGap)
for x1,y1,x2,y2 in lines[0]:
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)
while True:
cv2.imshow('houghlines',img)
if cv2.waitKey(20) & 0xFF == 27:
break
cv2.destroyAllWindows()

import cv2
hou()


Finally, the image I am getting, which isn't appropriate(only shows a small green line at the bottom)

same question is posted in StackOverflow question

edit retag close merge delete

Sort by ยป oldest newest most voted

Here is code:

#!/usr/bin/env python3.5
#Opencv 4.0.1
#Date: 1st April, 2019

import cv2

im2 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(im2,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

for i in range(0,len(contours)):
cnt = contours[i]
cv2.drawContours(img, [cnt],-1,(0,255,0))
cv2.imshow('Features', img)
cv2.waitKey(1)


Output:

more

1

take my bow and respects sir !!! <3

you solved my initial problem with which i was struggling. next i would be looking to extract the horizontal lines only . do you have any recommendations for it too?

( 2019-04-02 08:03:28 -0600 )edit

You can use ROI.

( 2019-04-02 08:43:48 -0600 )edit

no no i mean, during the lines are generation how can i only draw horizontal lines instead of those having slope more than some angles.

because for i in range(0,len(contours)): cnt = contours[i] returns some point cordinates but line start and points, as i understood.

( 2019-04-02 10:34:26 -0600 )edit

Shariq, Used cv2.polylines or cv2.HoughLinesS. I wouldn't recommended HoughLineP.

( 2019-04-02 11:13:23 -0600 )edit

hello,

the original image is this:

while I'm trying to derive something like this as you notice there are two lines as marked .

only once i know the coordinates and dimensions of the lines i can make some decisions out of it. :)

( 2019-04-02 11:56:24 -0600 )edit

Ok. I see that u're posted original image. That is fine.

( 2019-04-02 15:46:39 -0600 )edit

Shariq, I have to redo it over again because original image. In meantime, I am very busy for home renovation.

( 2019-04-02 15:54:12 -0600 )edit

it is ok Supra, atleast you are taking your time for me. :) Btw, let me also tell you, that whole intention is to get process data and decide movement of a robot. so its ok if the lines are close if not perfect

( 2019-04-03 05:29:39 -0600 )edit

Official site

GitHub

Wiki

Documentation