回答編集履歴
2
修正
test
CHANGED
@@ -132,7 +132,7 @@
|
|
132
132
|
|
133
133
|
// if(_img[x] == 255){
|
134
134
|
|
135
|
-
for(int x = 0
|
135
|
+
for(int x = 0, width = img.cols ; x < width; x++){
|
136
136
|
|
137
137
|
if(img( y, x ) == 255){
|
138
138
|
|
1
追記
test
CHANGED
@@ -44,7 +44,7 @@
|
|
44
44
|
|
45
45
|
for(int x = 0 ; x < img.cols; x++){
|
46
46
|
|
47
|
-
if(img.at( y, x ) == 255){
|
47
|
+
if(img.at<uchar>( y, x ) == 255){
|
48
48
|
|
49
49
|
count++;
|
50
50
|
|
@@ -89,3 +89,89 @@
|
|
89
89
|
|
90
90
|
|
91
91
|
とするだけですよ
|
92
|
+
|
93
|
+
**修正:** 指摘を受け、img.at( y, x )をimg.at<uchar>( y, x )に変更
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
---
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
**追記**
|
102
|
+
|
103
|
+
Mat1b つまり Mat_<uchar>を使うと
|
104
|
+
|
105
|
+
```c++
|
106
|
+
|
107
|
+
#include <stdio.h>
|
108
|
+
|
109
|
+
#include "opencv/cv.h"
|
110
|
+
|
111
|
+
#include "opencv/highgui.h"
|
112
|
+
|
113
|
+
using namespace cv;
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
int main(){
|
118
|
+
|
119
|
+
Mat1b img = imread("sample.jpg", IMREAD_GRAYSCALE);
|
120
|
+
|
121
|
+
int count=0;
|
122
|
+
|
123
|
+
double x_g=0.0,y_g=0.0;
|
124
|
+
|
125
|
+
for(int y = 0, height = img.rows; y < height; y++){
|
126
|
+
|
127
|
+
// パフォーマンス的には↓3行を置き換えた方が速い
|
128
|
+
|
129
|
+
// const uchar *_img = img[y];
|
130
|
+
|
131
|
+
// for(int x = 0, width = img.cols ; x < width; x++){ //変化なし
|
132
|
+
|
133
|
+
// if(_img[x] == 255){
|
134
|
+
|
135
|
+
for(int x = 0 ; x < img.cols; x++){
|
136
|
+
|
137
|
+
if(img( y, x ) == 255){
|
138
|
+
|
139
|
+
count++;
|
140
|
+
|
141
|
+
x_g += x;
|
142
|
+
|
143
|
+
y_g += y;
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
x_g /= conut;
|
152
|
+
|
153
|
+
y_g /= count;
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
Point2f mc = Point2f( x_g, y_g);
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
circle( img, mc, 4, Scalar(100), 2, 4);
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
printf("x: %f y: %f", mc.x, mc.y);
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
imshow("img",img);
|
170
|
+
|
171
|
+
waitKey(0);
|
172
|
+
|
173
|
+
return 0;
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
```
|