Ask Your Question
0

finding absolute straight lines/ rectangles for contours

asked 2019-04-01 15:22:28 -0500

shariq_azim gravatar image

updated 2019-04-01 21:36:21 -0500

supra56 gravatar image

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. image description

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
   img = cv2.imread('image5GR.jpg')
   #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) image description

same question is posted in StackOverflow question

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-04-01 21:34:17 -0500

supra56 gravatar image

Here is code:

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

import cv2

img =  cv2.imread('img.jpg')
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: image description

edit flag offensive delete link more

Comments

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?

shariq_azim gravatar imageshariq_azim ( 2019-04-02 08:03:28 -0500 )edit

You can use ROI.

supra56 gravatar imagesupra56 ( 2019-04-02 08:43:48 -0500 )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.

shariq_azim gravatar imageshariq_azim ( 2019-04-02 10:34:26 -0500 )edit

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

supra56 gravatar imagesupra56 ( 2019-04-02 11:13:23 -0500 )edit

hello,

the original image is this: image description

while I'm trying to derive something like this image description 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. :)

shariq_azim gravatar imageshariq_azim ( 2019-04-02 11:56:24 -0500 )edit

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

supra56 gravatar imagesupra56 ( 2019-04-02 15:46:39 -0500 )edit

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

supra56 gravatar imagesupra56 ( 2019-04-02 15:54:12 -0500 )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

shariq_azim gravatar imageshariq_azim ( 2019-04-03 05:29:39 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-04-01 15:22:28 -0500

Seen: 1,408 times

Last updated: Apr 01 '19