C# と VB.NET の質問掲示板

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト

Re[1]: code format


(過去ログ 176 を表示中)

[トピック内 2 記事 (1 - 2 表示)]  << 0 >>

■101401 / inTopicNo.1)  code format
  
□投稿者/ abcd (1回)-(2023/02/17(Fri) 06:14:07)

分類:[C/C++] 

	void QuickDemo::contour_get(Mat& image, vector<vector<Point>>& contours)
	{
		//高斯模糊
		Mat dst;
		GaussianBlur(image, dst, Size(3, 3), 0);
		Mat gray;
		cvtColor(dst, gray, COLOR_BGR2GRAY);
	 
		Mat binary;
		threshold(gray, binary, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
		/*namedWindow("THRESH_OTSU", WINDOW_FREERATIO);
		imshow("THRESH_OTSU", binary);*/
		//&#26597;找&#36718;廓
		vector<Vec4i> hierachy;
		findContours(binary, contours, hierachy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point());
		cout << contours.size() << endl;
	}

18.	void QuickDemo::contour_match(Mat& image1, Mat&image2)
19.	{
20.		vector<vector<Point>> contours1;
21.		vector<vector<Point>> contours2;
22.		contour_get(image1, contours1);
23.		contour_get(image2, contours2);
24.	 
25.		/*
26.		* &#27493;&#39588;:
27.		* 1、任&#36873;&#22270;像2中的一个&#36718;廓,&#35745;算其Hu矩
28.		* 2、&#23545;&#22270;像1所有&#36718;廓&#35745;算Hu矩,将&#22270;像2的Hu矩与&#22270;像1的所有Hu&#36827;行比&#36739;
29.		* 3、相似度&#38408;&#20540;操作。
30.		*/
31.		//Hu矩&#35745;算
32.		Moments mm2 = moments(contours2[0]);//先&#35745;算几何矩
33.		Mat hu2;
34.		HuMoments(mm2, hu2);
35.		for (size_t t = 0; t < contours1.size(); ++t) {
36.			Moments mm = moments(contours1[t]);//先&#35745;算几何矩
37.			Mat hu;
38.			HuMoments(mm, hu);
39.			double sim_value = matchShapes(hu, hu2, CONTOURS_MATCH_I1, 0);
40.			//在原&#22270;&#32472;制相似&#36718;廓
41.			if (sim_value < 1) {
42.				cout << "第" << t << "个&#36718;廓的相似度&#20540;&#20026;:" << (float)(1 - sim_value) << endl;
43.				drawContours(image1, contours1, t, Scalar(0, 255, 0), 2, 8);
44.				drawContours(image2, contours2, 0, Scalar(0, 255, 0), 2, 8);
45.			}
46.			
47.			//&#33719;取&#22270;像1&#36718;廓的中心位置
48.			double cx = mm.m10 / mm.m00;
49.			double cy = mm.m01 / mm.m00;
50.			circle(image1, Point(cx, cy), 2, Scalar(255, 0, 0), 2, 8);//在中心位置画&#22278;
51.		}
52.		namedWindow("contours1", WINDOW_FREERATIO);
53.		imshow("contours1", image1);
54.		namedWindow("image2", WINDOW_FREERATIO);
55.		imshow("image2", image2);
56.	}


引用返信 編集キー/
■101402 / inTopicNo.2)  Re[1]: code format
□投稿者/ abcd (2回)-(2023/02/17(Fri) 06:19:25)
2023/02/17(Fri) 06:24:25 編集(投稿者)
2023/02/17(Fri) 06:20:08 編集(投稿者)

void QuickDemo::contour_get(Mat& image, vector<vector<Point>>& contours)
{
//高斯模糊
Mat dst;
GaussianBlur(image, dst, Size(3, 3), 0);
Mat gray;
cvtColor(dst, gray, COLOR_BGR2GRAY);

Mat binary;
threshold(gray, binary, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
/*namedWindow("THRESH_OTSU", WINDOW_FREERATIO);
imshow("THRESH_OTSU", binary);*/
//&#26597;找&#36718;廓
vector<Vec4i> hierachy;
findContours(binary, contours, hierachy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point());
cout << contours.size() << endl;
}




void QuickDemo::contour_match(Mat& image1, Mat&image2)
{
vector<vector<Point>> contours1;
vector<vector<Point>> contours2;
contour_get(image1, contours1);
contour_get(image2, contours2);

/*
* &#27493;&#39588;:
* 1、任&#36873;&#22270;像2中的一个&#36718;廓,&#35745;算其Hu矩
* 2、&#23545;&#22270;像1所有&#36718;廓&#35745;算Hu矩,将&#22270;像2的Hu矩与&#22270;像1的所有Hu&#36827;行比&#36739;
* 3、相似度&#38408;&#20540;操作。
*/
//Hu矩&#35745;算
Moments mm2 = moments(contours2[0]);//先&#35745;算几何矩
Mat hu2;
HuMoments(mm2, hu2);
for (size_t t = 0; t < contours1.size(); ++t) {
Moments mm = moments(contours1[t]);//先&#35745;算几何矩
Mat hu;
HuMoments(mm, hu);
double sim_value = matchShapes(hu, hu2, CONTOURS_MATCH_I1, 0);
//在原&#22270;&#32472;制相似&#36718;廓
if (sim_value < 1) {
cout << "第" << t << "个&#36718;廓的相似度&#20540;&#20026;:" << (float)(1 - sim_value) << endl;
drawContours(image1, contours1, t, Scalar(0, 255, 0), 2, 8);
drawContours(image2, contours2, 0, Scalar(0, 255, 0), 2, 8);
}

//&#33719;取&#22270;像1&#36718;廓的中心位置
double cx = mm.m10 / mm.m00;
double cy = mm.m01 / mm.m00;
circle(image1, Point(cx, cy), 2, Scalar(255, 0, 0), 2, 8);//在中心位置画&#22278;
}
namedWindow("contours1", WINDOW_FREERATIO);
imshow("contours1", image1);
namedWindow("image2", WINDOW_FREERATIO);
imshow("image2", image2);
解決済み
引用返信 編集キー/


トピック内ページ移動 / << 0 >>

このトピックに書きこむ

過去ログには書き込み不可

管理者用

- Child Tree -