Ask Your Question

how to get desired line in the frame or measure the distance between two line?

asked 2015-06-09 07:46:30 -0600

Akki gravatar image

updated 2015-06-11 03:18:13 -0600

Eduardo gravatar image


I an using this code for hough line

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;
Mat frame;
int main()
   Mat src = imread("building.jpg", 0); 

    Mat dst, cdst;

    Canny(src, dst, 50, 200, 3); 
    cvtColor(dst, cdst, CV_GRAY2BGR); 

    vector<Vec2f> lines;
    // detect lines
    HoughLines(dst, lines, 1, CV_PI/180, 150, 0, 0 );

    // draw lines
    for( size_t i = 0; i < lines.size(); i++ )

        float rho = lines[i][0], theta = lines[i][1];

                if( theta>CV_PI/180*170 || theta<CV_PI/180*10)
        Point pt1, pt2;

        double a = cos(theta), b = sin(theta);
        double x0 = a*rho, y0 = b*rho;

        pt1.x = cvRound(x0 + 1000*(-b));
        pt1.y = cvRound(y0 + 1000*(a));
        pt2.x = cvRound(x0 - 1000*(-b));
        pt2.y = cvRound(y0 - 1000*(a));

        line( src, pt1, pt2, Scalar(0,0,255), 3, CV_AA);


    imshow("source", src;
    imshow("detected lines", cdst);

    return 0;

I want line aroung the tube only and other i don't want.image description the width of tube is aroung 46 pixel, from the frame. is there any suggestions how to detect the desired line.

Thank you very much in advance.

edit retag flag offensive close merge delete



Why you are using hough ? If you know that tubes are vertical you could analyse your frame with 4 or 5 horiz scan line to find the edge of your tube than interpolate the point.

pklab gravatar imagepklab ( 2015-06-09 14:19:58 -0600 )edit

thank you for comment. in my case i am trying to detect the tube , but i have no idea about how to do it, so i tried with hough line. can you please give me detail how to go forward.

Akki gravatar imageAkki ( 2015-06-10 02:35:39 -0600 )edit

Skip hough line which is a giant overkill, go for a vertical Sobel filter, than interconnect points like @pklab says!

StevenPuttemans gravatar imageStevenPuttemans ( 2015-06-11 07:52:48 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-06-10 13:10:15 -0600

pklab gravatar image

It seems you have to start from scratch with your project, right ?

If you want to use hough you could group your lines by distance and choose your best

Anyway before coding you have to set up the right environment like light colour and direction, background colour or pattern and some consideration like: can the labels appear in front of the tube ? which accuracy is needed... measure or just localization ?

Good settings will improve results and simplify algorithms... for example you could start trying dark or reflective background than use some threshold in HSV, fill holes than blobs

You could proceed with feasibility using some tools like Fiji or Cassandra

don't ask for full code please :) specific question about OpenCV are welcome !

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2015-06-09 07:46:30 -0600

Seen: 1,906 times

Last updated: Jun 11 '15