OpenCV  5.0.0alpha-pre
Open Source Computer Vision
Loading...
Searching...
No Matches
samples/python/snippets/houghlines.py

An example using the Hough line detector in python

1#!/usr/bin/python
2
3'''
4This example illustrates how to use Hough Transform to find lines
5
6Usage:
7 houghlines.py [<image_name>]
8 image argument defaults to pic1.png
9'''
10
11# Python 2/3 compatibility
12from __future__ import print_function
13
14import cv2 as cv
15import numpy as np
16
17import sys
18import math
19
20def main():
21 try:
22 fn = sys.argv[1]
23 except IndexError:
24 fn = 'pic1.png'
25
27 dst = cv.Canny(src, 50, 200)
28 cdst = cv.cvtColor(dst, cv.COLOR_GRAY2BGR)
29
30 if True: # HoughLinesP
31 lines = cv.HoughLinesP(dst, 1, math.pi/180.0, 40, np.array([]), 50, 10)
32 a, b, _c = lines.shape
33 for i in range(a):
34 cv.line(cdst, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 3, cv.LINE_AA)
35
36 else: # HoughLines
37 lines = cv.HoughLines(dst, 1, math.pi/180.0, 50, np.array([]), 0, 0)
38 if lines is not None:
39 a, b, _c = lines.shape
40 for i in range(a):
41 rho = lines[i][0][0]
42 theta = lines[i][0][1]
43 a = math.cos(theta)
44 b = math.sin(theta)
45 x0, y0 = a*rho, b*rho
46 pt1 = ( int(x0+1000*(-b)), int(y0+1000*(a)) )
47 pt2 = ( int(x0-1000*(-b)), int(y0-1000*(a)) )
48 cv.line(cdst, pt1, pt2, (0, 0, 255), 3, cv.LINE_AA)
49
50 cv.imshow("detected lines", cdst)
51
52 cv.imshow("source", src)
53 cv.waitKey(0)
54 print('Done')
55
56
57if __name__ == '__main__':
58 print(__doc__)
59 main()
cv::String findFile(const cv::String &relative_path, bool required=true, bool silentMode=false)
Try to find requested data file.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
void destroyAllWindows()
Destroys all of the HighGUI windows.
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
Loads an image from a file.
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0, AlgorithmHint hint=cv::ALGO_HINT_DEFAULT)
Converts an image from one color space to another.
void line(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a line segment connecting two points.
void Canny(InputArray image, OutputArray edges, double threshold1, double threshold2, int apertureSize=3, bool L2gradient=false)
Finds edges in an image using the Canny algorithm .
void HoughLines(InputArray image, OutputArray lines, double rho, double theta, int threshold, double srn=0, double stn=0, double min_theta=0, double max_theta=CV_PI)
Finds lines in a binary image using the standard Hough transform.
void HoughLinesP(InputArray image, OutputArray lines, double rho, double theta, int threshold, double minLineLength=0, double maxLineGap=0)
Finds line segments in a binary image using the probabilistic Hough transform.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3