質問編集履歴
2
試したこと追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,21 +3,19 @@
|
|
3
3
|
テンプレートマッチングでマッチングした箇所だけを切り取りたいです。
|
4
4
|
どのような関数を使い,切り取ればいいのでしょうか。
|
5
5
|
|
6
|
+
###ソースコード
|
6
7
|
|
7
|
-
|
8
|
+
include <vector>
|
9
|
+
include <stdio.h>
|
10
|
+
include <iostream>
|
11
|
+
include <string.h>
|
12
|
+
include <opencv2/core/core.hpp>
|
13
|
+
include <opencv2/imgproc/imgproc.hpp>
|
14
|
+
include <opencv2/highgui/highgui.hpp>
|
15
|
+
include "opencv2/opencv.hpp"
|
16
|
+
include <opencv2/opencv.hpp>
|
17
|
+
pragma comment(lib, "opencv_world320d.lib")
|
8
18
|
|
9
|
-
```C++
|
10
|
-
#include <vector>
|
11
|
-
#include <stdio.h>
|
12
|
-
#include <iostream>
|
13
|
-
#include <string.h>
|
14
|
-
#include <opencv2/core/core.hpp>
|
15
|
-
#include <opencv2/imgproc/imgproc.hpp>
|
16
|
-
#include <opencv2/highgui/highgui.hpp>
|
17
|
-
#include "opencv2/opencv.hpp"
|
18
|
-
#include <opencv2/opencv.hpp>
|
19
|
-
#pragma comment(lib, "opencv_world320d.lib")
|
20
|
-
|
21
19
|
using namespace std;
|
22
20
|
using namespace cv;
|
23
21
|
|
@@ -45,35 +43,34 @@
|
|
45
43
|
else if(key == 's')
|
46
44
|
{
|
47
45
|
cv::waitKey(1);
|
48
|
-
cv::imwrite("img.png", frame);
|
46
|
+
cv::imwrite("/Users/i-am-kaito/Downloads/img.png", frame);
|
49
47
|
}
|
50
48
|
}
|
51
49
|
|
52
50
|
Mat src;
|
53
51
|
//テンプレート画像とその名称
|
54
|
-
vector<Mat> img{ imread("
|
52
|
+
vector<Mat> img{ imread("/Users/i-am-kaito/Downloads/IMG_4483.png"),
|
55
|
-
imread("
|
53
|
+
imread("/Users/i-am-kaito/Downloads/IMG_4484.png"),
|
56
|
-
imread("
|
54
|
+
imread("/Users/i-am-kaito/Downloads/IMG_4448.png")
|
57
55
|
};
|
58
56
|
vector<string> name{ "680","1000","150"};
|
59
57
|
|
60
58
|
//色情報
|
61
|
-
Mat src_image = imread("img.png", 1); // カラーで読み込む
|
59
|
+
Mat src_image = imread("/Users/i-am-kaito/Downloads/img.png", 1); // カラーで読み込む
|
62
60
|
|
63
61
|
// 画像の座標(0,0)の画素値を取得する
|
64
62
|
Vec3b pix = src_image.at<Vec3b>(Point(0, 0));
|
65
63
|
|
66
|
-
// 画素値はBGRの順に格納
|
64
|
+
// 画素値はBGRの順に格納されている
|
67
65
|
cout << (int)pix[0] << "," << (int)pix[1] << "," << (int)pix[2] << endl;
|
68
|
-
|
69
|
-
|
66
|
+
|
70
67
|
while (1) {
|
71
68
|
cap >> src; if (src.empty()) break;
|
72
69
|
|
73
70
|
for (int num = 0; num < 3; num++){
|
74
71
|
Mat mapCC;
|
75
72
|
//テンプレートマッチング
|
76
|
-
matchTemplate(src, img[num], mapCC, cv::TM_CCOEFF_NORMED);
|
73
|
+
matchTemplate(src, img[num], mapCC, cv::TM_CCOEFF_NORMED);//ZNCC法
|
77
74
|
double maxCC;
|
78
75
|
Point maxLoc;
|
79
76
|
//相互相関係数の最大値探索
|
@@ -84,26 +81,36 @@
|
|
84
81
|
maxLoc + Point(img[num].cols, img[num].rows), 0, 4);
|
85
82
|
putText(src, name[num], maxLoc + Point(10, 30),
|
86
83
|
cv::FONT_HERSHEY_SIMPLEX, 1, 0, 2);
|
84
|
+
|
87
|
-
|
85
|
+
const int key = cv::waitKey(1);
|
86
|
+
|
87
|
+
if(key == 'k'){
|
88
|
+
cv::Rect roi(cv::Point(img[num].cols, img[num].rows), cv::Size(600,600));
|
89
|
+
cv::Mat subImg = src(roi);
|
90
|
+
imshow("kiri", src(roi));
|
91
|
+
}
|
92
|
+
else if(key == 'q'){
|
93
|
+
break;
|
94
|
+
}
|
88
95
|
}
|
89
|
-
imshow("sougosoukan" + name[num], mapCC);
|
90
96
|
|
91
|
-
}
|
92
|
-
|
93
97
|
imshow("Temp", src);
|
94
98
|
if (waitKey(20) == 27) break;
|
95
99
|
|
96
100
|
}
|
97
|
-
|
101
|
+
}
|
98
102
|
cv::destroyAllWindows();
|
99
103
|
return 0;
|
100
|
-
}
|
101
104
|
|
102
|
-
|
105
|
+
}
|
103
106
|
|
107
|
+
|
108
|
+
|
104
109
|
### 試したこと
|
105
110
|
|
106
|
-
rectangleでマッチング箇所を四角形で囲っています。
|
111
|
+
rectangleでマッチング箇所を四角形で囲っています。
|
112
|
+
cv::Rect roi(cv::Point(x, y), cv::Size(600,600));
|
113
|
+
マッチング箇所だけを切り取る際はx,yに何を入れたらいいのでしょうか
|
107
114
|
|
108
115
|
### 補足情報(FW/ツールのバージョンなど)
|
109
116
|
|
1
ソースコードの記載不備
title
CHANGED
File without changes
|
body
CHANGED
@@ -6,8 +6,99 @@
|
|
6
6
|
|
7
7
|
### 該当のソースコード
|
8
8
|
|
9
|
-
```
|
9
|
+
```C++
|
10
|
+
#include <vector>
|
11
|
+
#include <stdio.h>
|
12
|
+
#include <iostream>
|
13
|
+
#include <string.h>
|
14
|
+
#include <opencv2/core/core.hpp>
|
15
|
+
#include <opencv2/imgproc/imgproc.hpp>
|
16
|
+
#include <opencv2/highgui/highgui.hpp>
|
17
|
+
#include "opencv2/opencv.hpp"
|
18
|
+
#include <opencv2/opencv.hpp>
|
19
|
+
#pragma comment(lib, "opencv_world320d.lib")
|
20
|
+
|
21
|
+
using namespace std;
|
22
|
+
using namespace cv;
|
23
|
+
|
24
|
+
int main(int argh, char* argv[])
|
25
|
+
{
|
26
|
+
cv::VideoCapture cap(0);
|
27
|
+
|
28
|
+
if(!cap.isOpened())
|
29
|
+
{
|
30
|
+
cout << "ERROR: cannot open cam device." << endl;
|
31
|
+
return -1;
|
32
|
+
}
|
33
|
+
|
34
|
+
cv::Mat frame;
|
35
|
+
while(cap.read(frame))
|
36
|
+
{
|
37
|
+
|
38
|
+
cv::imshow("win", frame);
|
39
|
+
const int key = cv::waitKey(1);
|
40
|
+
if(key == 'q')
|
41
|
+
{
|
10
|
-
|
42
|
+
break;
|
43
|
+
}
|
44
|
+
|
45
|
+
else if(key == 's')
|
46
|
+
{
|
47
|
+
cv::waitKey(1);
|
48
|
+
cv::imwrite("img.png", frame);
|
49
|
+
}
|
50
|
+
}
|
51
|
+
|
52
|
+
Mat src;
|
53
|
+
//テンプレート画像とその名称
|
54
|
+
vector<Mat> img{ imread("img1.png"),
|
55
|
+
imread("img2.png"),
|
56
|
+
imread("img3.png")
|
57
|
+
};
|
58
|
+
vector<string> name{ "680","1000","150"};
|
59
|
+
|
60
|
+
//色情報
|
61
|
+
Mat src_image = imread("img.png", 1); // カラーで読み込む
|
62
|
+
|
63
|
+
// 画像の座標(0,0)の画素値を取得する
|
64
|
+
Vec3b pix = src_image.at<Vec3b>(Point(0, 0));
|
65
|
+
|
66
|
+
// 画素値はBGRの順に格納
|
67
|
+
cout << (int)pix[0] << "," << (int)pix[1] << "," << (int)pix[2] << endl;
|
68
|
+
|
69
|
+
|
70
|
+
while (1) {
|
71
|
+
cap >> src; if (src.empty()) break;
|
72
|
+
|
73
|
+
for (int num = 0; num < 3; num++){
|
74
|
+
Mat mapCC;
|
75
|
+
//テンプレートマッチング
|
76
|
+
matchTemplate(src, img[num], mapCC, cv::TM_CCOEFF_NORMED);
|
77
|
+
double maxCC;
|
78
|
+
Point maxLoc;
|
79
|
+
//相互相関係数の最大値探索
|
80
|
+
minMaxLoc(mapCC, NULL, &maxCC, NULL, &maxLoc);
|
81
|
+
|
82
|
+
if (maxCC > 0.4){ //類似度が0.4以上なら枠描写
|
83
|
+
rectangle(src, maxLoc,
|
84
|
+
maxLoc + Point(img[num].cols, img[num].rows), 0, 4);
|
85
|
+
putText(src, name[num], maxLoc + Point(10, 30),
|
86
|
+
cv::FONT_HERSHEY_SIMPLEX, 1, 0, 2);
|
87
|
+
operator()( const cv::Rect );
|
88
|
+
}
|
89
|
+
imshow("sougosoukan" + name[num], mapCC);
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
imshow("Temp", src);
|
94
|
+
if (waitKey(20) == 27) break;
|
95
|
+
|
96
|
+
}
|
97
|
+
|
98
|
+
cv::destroyAllWindows();
|
99
|
+
return 0;
|
100
|
+
}
|
101
|
+
|
11
102
|
```
|
12
103
|
|
13
104
|
### 試したこと
|