OpenCV学习笔记十:hough变换
生活随笔
收集整理的這篇文章主要介紹了
OpenCV学习笔记十:hough变换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
關于hough變換函數的bug
根據官方教程或其他博客教程調用hough函數的時候總是會報錯,主要是返回的circles錯誤,或者lines錯誤,這可能和輸入參數有關。但是明確的輸入是image格式并沒有錯,其他的參數也沒有明確要求,暫時放下,以后再看。報錯是再畫圖是報錯,實際上錯誤是產生再調用hough函數時。
#include<opencv2/opencv.hpp> #include<iostream>using namespace cv; using namespace std;int main() {Mat srcImage = imread("circle.jpg");if (srcImage.data == 0) { cout << "there is no file"; return -1; }imshow("srcImage", srcImage);//HoughCircle 變換Mat gray;//轉成灰度圖cvtColor(srcImage, gray, COLOR_BGR2GRAY);//濾波medianBlur(gray, gray, 5);//使用houghcircles函數vector<Vec3f> circles;HoughCircles(gray, circles, HOUGH_GRADIENT, 1, 1, 100, 30, 1, 100);//畫出hough函數的結果for (size_t i = 0; i < circles.size(); i++){cout << circles[i]<<endl;Vec3i c = circles[i];if (c[2] > 0) {circle(srcImage, Point(c[0], c[1]), c[2], Scalar(0, 0, 255), 3, LINE_AA);circle(srcImage, Point(c[0], c[1]), c[2], Scalar(0, 255, 0), 3, LINE_AA);}}imshow("detected circles", srcImage);waitKey(0); }已經解決了上面的問題,主要是環境配置,版本適應。
根官方教程做了如下案例:
/OpenCV4.1.1+VS2017 author :Chenandong time:2019.08.10 #include "opencv2/imgcodecs.hpp" #include "opencv2/highgui.hpp" #include "opencv2/imgproc.hpp" #include<iostream> using namespace cv; using namespace std;int main(int argc, char** argv) {// Loads an imageMat src = imread("circle7from6.png", IMREAD_COLOR);// Check if image is loaded fineif (src.empty()) {printf(" Error opening image\n");printf(" Program Arguments: [image_name -- default %s] \n");return -1;}//show the origin imageimshow("origin image", src);//HOughCircles detect//----------------------------------------------------------// change to gray imageMat gray;cvtColor(src, gray, COLOR_BGR2GRAY);//濾波處理medianBlur(gray, gray, 5);imshow("gray", gray);//Hough圓檢測vector<Vec3f> circles;HoughCircles(gray, circles, HOUGH_GRADIENT, 1,gray.rows / 8, // change this value to detect circles with different distances to each other60, 25, 30, 300 // change the last two parameters// (min_radius & max_radius) to detect larger circles);//draw the circle be detectedfor (size_t i = 0; i < circles.size(); i++){Vec3i c = circles[i];Point center = Point(c[0], c[1]);cout << center << endl;// circle centercircle(src, center, 1, Scalar(0, 100, 100), 3, LINE_AA);// circle outlineint radius = c[2];circle(src, center, radius, Scalar(0, 100, 100), 3, LINE_AA);}//show the resultimshow("detected circles", src);//------------------------------------------------waitKey();return 0; }[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-EzH49hre-1574154766281)(https://chenandongtime.github.io/img/1.png)]
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-Kx3Upl8f-1574154766283)(https://chenandongtime.github.io/img/2.png)]
總結
以上是生活随笔為你收集整理的OpenCV学习笔记十:hough变换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV学习笔记九-Canny边缘检
- 下一篇: OpenCV学习笔记十一-findcou