生成一串基于opencv4c++可以检测图片中直线且运用到hough变化的代码
时间: 2023-06-03 18:06:56 浏览: 94
以下是基于OpenCV4C的直线检测代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
int main(int argc, char** argv) {
IplImage* img = NULL;
if (argc == 2) {
img = cvLoadImage(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
if (img == NULL) {
printf("Can't open %s\n", argv[1]);
exit(1);
}
} else {
printf("Usage: ./detect_lines <image file>\n");
exit(1);
}
IplImage* color_img = cvLoadImage(argv[1], CV_LOAD_IMAGE_COLOR);
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* lines = NULL;
int i;
lines = cvHoughLines2(img, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 50, 50, 10);
for (i = 0; i < lines->total; i++) {
CvPoint* line = (CvPoint*) cvGetSeqElem(lines, i);
cvLine(color_img, line[0], line[1], CV_RGB(255, 0, 0), 3, 8);
}
cvNamedWindow("Lines Detection", CV_WINDOW_NORMAL);
cvShowImage("Lines Detection", color_img);
cvWaitKey(0);
cvReleaseMemStorage(&storage);
cvReleaseImage(&color_img);
cvReleaseImage(&img);
cvDestroyWindow("Lines Detection");
return 0;
}
当然,需要安装OpenCV4C库,这里不再赘述。
阅读全文