Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize OpenCV Segment Detection

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to implement OpenCV segment detection". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Line segment detection mainly uses Hough transform. Hough transform is one of the basic methods to identify geometric shapes from images in image processing. It is widely used, and there are many improved algorithms. It is mainly used to separate geometric shapes (such as lines, circles, etc.) with the same characteristics from the image. The most basic Hough transform is to detect lines (line segments) from black-and-white images.

In OpenCV programming, cvHoughLines2 function is mainly used to realize line segment detection.

Function prototype:

CvSeq* cvHoughLines2 (

CvArr* image

Void* line_storage

Int method

Double rho

Double theta

Int threshold

Double param1=0, double param2=0

);

Parameter description:

The first parameter represents the input image and must be a binary image (black-and-white image).

The second parameter represents the storage container, which can be passed in a pointer of type CvMemStorage.

The third parameter represents the transformation variable, and you can take the following values:

CV_HOUGH_STANDARD-traditional or standard Hough transform. Each line segment is represented by two floating point numbers (ρ, θ), where ρ is the distance between the line segment and the origin (0mem0), and the angle between the θ line segment and the x-axis.

CV_HOUGH_PROBABILISTIC-probabilistic Hough transform (more efficient if the image contains some long linear segmentation). It returns the segment segment instead of the entire segment. Each division is represented by a starting point and an end point.

CV_HOUGH_MULTI_SCALE-a multi-scale variant of traditional Hough transform. The encoding of line segments is the same as that of CV_HOUGH_STANDARD.

The fourth parameter represents the distance accuracy of the unit related to the pixel.

The fifth parameter represents the angular accuracy of Radian measurement.

The sixth parameter represents the maximum number of detected segments. If so many segments have been detected, the function returns.

The seventh parameter is related to the third parameter, and its meaning is as follows:

For traditional Hough transform, do not use (0).

For probabilistic Hough transform, it is the minimum segment length.

For the multi-scale Hough transform, it is the denominator of the distance precision rho (the approximate distance precision is rho and the accurate one should be rho / param1).

The eighth parameter is related to the third parameter, and its meaning is as follows:

For traditional Hough transform, do not use (0).

For probabilistic Hough transform, this parameter represents the maximum interval value (gap) for joining broken segments on the same line segment, that is, when the interval between two broken line segments on the same line segment is less than param2, it will be merged into one.

For multi-scale Hough transform, it is the denominator of angle accuracy theta (the approximate angle accuracy is theta and the exact angle should be theta / param2).

Sample program:

Hough.cpp

# include

# include

# include

# include

# include

Using namespace std

Int main (int argc, char * * argv)

{

Const char * pstrWindowsSrcTitle = "initial"

Const char * pstrWindowsLineName = "hough"

IplImage * pSrcImage = cvLoadImage ("hough.jpg", CV_LOAD_IMAGE_UNCHANGED)

IplImage * pGrayImage = cvCreateImage (cvGetSize (pSrcImage), IPL_DEPTH_8U, 1)

CvCvtColor (pSrcImage, pGrayImage, CV_BGR2GRAY)

IplImage * pCannyImage = cvCreateImage (cvGetSize (pSrcImage), IPL_DEPTH_8U, 1)

CvCanny (pGrayImage, pCannyImage, 30,90)

CvMemStorage * pcvMStorage = cvCreateMemStorage ()

Double fRho = 1

Double fTheta = CV_PI / 180

Int nMaxLineNumber = 50; / / detect lines at most

Double fMinLineLen = 50; / / minimum segment length

Double fMinLineGap = 10; / / minimum segment interval

CvSeq * pcvSeqLines = cvHoughLines2 (pCannyImage, pcvMStorage, CV_HOUGH_PROBABILISTIC, fRho, fTheta, nMaxLineNumber, fMinLineLen, fMinLineGap)

IplImage * pColorImage = cvCreateImage (cvGetSize (pSrcImage), IPL_DEPTH_8U, 3)

CvCvtColor (pCannyImage, pColorImage, CV_GRAY2BGR)

Int i

For (I = 0; I)

< pcvSeqLines->

Total; iTunes +)

{

CvPoint* line = (CvPoint*) cvGetSeqElem (pcvSeqLines, I)

CvLine (pColorImage, line [0], line [1], CV_RGB (255pd0), 2)

}

CvNamedWindow (pstrWindowsSrcTitle, CV_WINDOW_AUTOSIZE)

CvShowImage (pstrWindowsSrcTitle, pSrcImage)

CvNamedWindow (pstrWindowsLineName, CV_WINDOW_AUTOSIZE)

CvShowImage (pstrWindowsLineName, pColorImage)

CvWaitKey (0)

CvReleaseMemStorage & pcvMStorage)

CvDestroyWindow (pstrWindowsSrcTitle)

CvDestroyWindow (pstrWindowsLineName)

CvReleaseImage & pSrcImage)

CvReleaseImage & pGrayImage)

CvReleaseImage & pCannyImage)

CvReleaseImage & pColorImage)

Return 0

}

Makefile:

INCLUDE = $(shell pkg-config-- cflags opencv)

LIBS = $(shell pkg-config-- libs opencv)

SOURCES = hough.cpp

# Target file

OBJECTS = $(SOURCES:.cpp=.o)

# executable file

TARGET = hough

(TARGET): $(OBJECTS)

Gmail +-o $(TARGET) $(OBJECTS)-I $(INCLUDE) $(LIBS)

(OBJECTS): $(SOURCES)

Gmail +-c $(SOURCES)

Clean:

Rm $(OBJECTS) $(TARGET)

# compilation rule $@ represents the target file $< represents the first dependent file

%. O:%.cpp

Gmail +-I $(INCLUDE)-o $@-c $

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report