OpenCVで円検出を勉強しています。
精度は荒いですが、とりあえず円が検出できました。
そこで円を検出するごとに円の中心の座標をコマンドプロントに出力するようにプログラムしたのですが、うまくいっているのかダメなのかすらわかりません。
まずコードです。
c++
1 2#define _CRT_SECURE_NO_WARNINGS 3#define _USE_MATH_DEFINES 4#include <iostream> 5#include <string> 6#include <cmath> 7#include <opencv2/opencv.hpp> 8using namespace std; 9using namespace cv; 10string win_src = "src"; 11string win_dst = "dst"; 12int main() 13{ 14 string file_src = "picture1.png";//入力画像のファイル名 15 string file_dst = "picture3.png";//出力画像のファイル名 16 Mat img_src = imread(file_src, 1);//入力画像(カラー)の読み込み 17 //Mat img_src=imread(file_src,0) //入力画像(グレー)の読み込み 18 Mat img_dst; 19 if (!img_src.data) { 20 cout << "error" << endl; 21 return -1; 22 } 23 //ウインドウ生成 24 namedWindow(win_src, WINDOW_AUTOSIZE); 25 namedWindow(win_src, WINDOW_AUTOSIZE); 26 //ここに核となる処理を記述する 27 //GaussianBlurを実施 28 GaussianBlur(img_src, img_src, Size(5, 5), 0); 29 //グレイ画像にする 30 Mat gray_image; 31 cvtColor(img_src, gray_image, CV_BGR2GRAY); 32 //HoughCirclesを実施 33 vector<Vec3f>circles; 34 HoughCircles(gray_image, circles, CV_HOUGH_GRADIENT, 1, 10, 80, 50, 1, 1000); 35 Mat src_image_write = img_src.clone(); 36 for (auto it = circles.begin(); it != circles.end(); ++it) 37 { 38 int radius = (*it)[2]; 39 Point center = Point((*it)[0], (*it)[1]); 40 circle(src_image_write, center, radius, Scalar(0, 255, 255), 2); 41 //円の中心表示したい(できているかわからない) 42 cout << circles[0] << "," << circles[1] << endl; 43 } 44 45 46 //核終了 47 48 imshow(win_src, img_src);//入力画像を表示 49 imshow(win_dst, src_image_write);//出力画像を表示 50 imwrite(file_dst, src_image_write);//処理結果の保存 51 52 system("pause");//キー入力待ち 53 return 0; 54} 55
このうち、
c++
1 //HoughCirclesを実施 2 vector<Vec3f>circles; 3 HoughCircles(gray_image, circles, CV_HOUGH_GRADIENT, 1, 10, 80, 50, 1, 1000); 4 Mat src_image_write = img_src.clone(); 5 for (auto it = circles.begin(); it != circles.end(); ++it) 6 { 7 int radius = (*it)[2]; 8 Point center = Point((*it)[0], (*it)[1]); 9 circle(src_image_write, center, radius, Scalar(0, 255, 255), 2); 10 //円の中心表示したい(できているかわからない) 11 cout << circles[0] << "," << circles[1] << endl; 12 } 13
ここの最後のcoutで円の中心を出力させているつもりです。
調べてみると、circlesベクトルはcircles[0]で中心のx座標を、circles[1]でy座標を示すとあったのですが、これをcoutで出力しても???座標を表していないようですし、私は円が認識されるたびに中心の座標を出力させている(つもり)ですが、値が毎回同じです。
どこを改良すべきでしょうか。また私の考えとプログラムにミスがあれば教えて頂きたいです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/12 13:22