質問編集履歴
4
変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
-
この中心点の座標を獲得したいのですがどうすればいいでしょうか?
|
9
|
+
この中心点の座標を獲得したいのですがどうすればいいでしょうか?Pointをintなどに変換できるのでしょうか?
|
10
10
|
|
11
11
|
|
12
12
|
|
3
変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -26,67 +26,93 @@
|
|
26
26
|
|
27
27
|
|
28
28
|
|
29
|
+
import android.app.Activity;
|
30
|
+
|
31
|
+
import android.os.Bundle;
|
32
|
+
|
33
|
+
import android.util.Log;
|
34
|
+
|
35
|
+
import android.view.MenuItem;
|
36
|
+
|
37
|
+
import android.view.SurfaceView;
|
38
|
+
|
39
|
+
import android.view.WindowManager;
|
40
|
+
|
41
|
+
import org.opencv.android.BaseLoaderCallback;
|
42
|
+
|
43
|
+
import org.opencv.android.CameraBridgeViewBase;
|
44
|
+
|
45
|
+
import org.opencv.android.LoaderCallbackInterface;
|
46
|
+
|
47
|
+
import org.opencv.android.OpenCVLoader;
|
48
|
+
|
49
|
+
import org.opencv.core.Core;
|
50
|
+
|
51
|
+
import org.opencv.core.CvType;
|
52
|
+
|
53
|
+
import org.opencv.core.Mat;
|
54
|
+
|
55
|
+
import org.opencv.core.MatOfInt;
|
56
|
+
|
57
|
+
import org.opencv.core.MatOfInt4;
|
58
|
+
|
59
|
+
import org.opencv.core.MatOfPoint;
|
60
|
+
|
61
|
+
import org.opencv.core.MatOfPoint2f;
|
62
|
+
|
63
|
+
import org.opencv.core.Point;
|
64
|
+
|
65
|
+
import org.opencv.core.RotatedRect;
|
66
|
+
|
67
|
+
import org.opencv.core.Scalar;
|
68
|
+
|
69
|
+
import org.opencv.core.Size;
|
70
|
+
|
71
|
+
import org.opencv.imgproc.Imgproc;
|
72
|
+
|
73
|
+
import org.opencv.imgproc.Moments;
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
29
79
|
import java.util.ArrayList;
|
30
80
|
|
31
81
|
import java.util.List;
|
32
82
|
|
33
83
|
|
34
84
|
|
85
|
+
|
86
|
+
|
35
|
-
import org.opencv.
|
87
|
+
import static org.opencv.core.CvType.CV_8U;
|
36
|
-
|
37
|
-
|
88
|
+
|
38
|
-
|
39
|
-
import org.opencv.
|
89
|
+
import static org.opencv.core.CvType.CV_8UC1;
|
40
|
-
|
41
|
-
|
90
|
+
|
42
|
-
|
43
|
-
import org.opencv.core.
|
91
|
+
import static org.opencv.core.CvType.CV_8UC3;
|
44
|
-
|
45
|
-
|
92
|
+
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
93
|
+
|
50
|
-
|
51
|
-
|
94
|
+
|
52
|
-
|
53
|
-
|
95
|
+
|
54
|
-
|
55
|
-
|
96
|
+
|
56
|
-
|
57
|
-
import android.util.Log;
|
58
|
-
|
59
|
-
import android.view.View;
|
60
|
-
|
61
|
-
import android.view.View.OnClickListener;
|
62
|
-
|
63
|
-
import android.widget.Button;
|
64
|
-
|
65
|
-
import android.widget.ImageView;
|
66
|
-
|
67
|
-
import android.widget.TextView;
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
public class MainActivity extends Activity {
|
97
|
+
public class MainActivity extends Activity implements CameraBridgeViewBase.CvCameraViewListener {
|
76
|
-
|
98
|
+
|
77
|
-
private static final String TAG = "
|
99
|
+
private static final String TAG = "OCVSample::Activity";
|
100
|
+
|
101
|
+
|
102
|
+
|
78
|
-
|
103
|
+
int i =0;
|
104
|
+
|
105
|
+
|
106
|
+
|
79
|
-
|
107
|
+
private CameraBridgeViewBase mOpenCvCameraView;
|
80
|
-
|
108
|
+
|
81
|
-
private
|
109
|
+
private boolean mIsJavaCamera = true;
|
82
|
-
|
110
|
+
|
83
|
-
private Im
|
111
|
+
private MenuItem mItemSwitchCamera = null;
|
84
|
-
|
85
|
-
|
112
|
+
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
113
|
+
|
114
|
+
|
115
|
+
|
90
116
|
|
91
117
|
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
|
92
118
|
|
@@ -96,19 +122,23 @@
|
|
96
122
|
|
97
123
|
switch (status) {
|
98
124
|
|
125
|
+
// 読み込みが成功したらカメラプレビューを開始
|
126
|
+
|
99
127
|
case LoaderCallbackInterface.SUCCESS:
|
100
128
|
|
129
|
+
{
|
130
|
+
|
101
|
-
Log.i(TAG, "loaded successfully");
|
131
|
+
Log.i(TAG, "OpenCV loaded successfully");
|
102
|
-
|
132
|
+
|
103
|
-
m
|
133
|
+
mOpenCvCameraView.enableView();
|
104
|
-
|
134
|
+
|
105
|
-
|
135
|
+
} break;
|
106
|
-
|
136
|
+
|
107
|
-
default:
|
137
|
+
default: {
|
108
138
|
|
109
139
|
super.onManagerConnected(status);
|
110
140
|
|
111
|
-
|
141
|
+
}break;
|
112
142
|
|
113
143
|
}
|
114
144
|
|
@@ -118,105 +148,165 @@
|
|
118
148
|
|
119
149
|
|
120
150
|
|
151
|
+
public MainActivity() {
|
152
|
+
|
153
|
+
Log.i(TAG, "Instantiated new " + this.getClass());
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
|
158
|
+
|
121
159
|
@Override
|
122
160
|
|
123
161
|
protected void onCreate(Bundle savedInstanceState) {
|
124
162
|
|
163
|
+
Log.i(TAG, "called onCreate");
|
164
|
+
|
125
165
|
super.onCreate(savedInstanceState);
|
126
166
|
|
167
|
+
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
168
|
+
|
127
169
|
setContentView(R.layout.activity_main);
|
128
170
|
|
129
171
|
|
130
172
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
m
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
173
|
+
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.camera_view);
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
mOpenCvCameraView.setCvCameraViewListener(this);
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
@Override
|
188
|
+
|
189
|
+
protected void onPause() {
|
190
|
+
|
191
|
+
super.onPause();
|
192
|
+
|
193
|
+
if (mOpenCvCameraView != null)
|
194
|
+
|
195
|
+
mOpenCvCameraView.disableView();
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
@Override
|
202
|
+
|
203
|
+
protected void onResume() {
|
204
|
+
|
205
|
+
super.onResume();
|
206
|
+
|
207
|
+
if (!OpenCVLoader.initDebug()) {
|
208
|
+
|
209
|
+
Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
|
210
|
+
|
211
|
+
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
|
212
|
+
|
213
|
+
} else {
|
214
|
+
|
215
|
+
Log.d(TAG, "OpenCV library found inside package. Using it!");
|
216
|
+
|
217
|
+
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
@Override
|
228
|
+
|
229
|
+
protected void onDestroy() {
|
230
|
+
|
231
|
+
super.onDestroy();
|
232
|
+
|
233
|
+
if (mOpenCvCameraView != null)
|
234
|
+
|
235
|
+
mOpenCvCameraView.disableView();
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
@Override
|
244
|
+
|
245
|
+
public void onCameraViewStarted(int width, int height) {// カメラプレビュー開始時に呼ばれる
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
@Override
|
252
|
+
|
253
|
+
public void onCameraViewStopped() {// カメラプレビュー終了時に呼ばれる
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
@Override
|
260
|
+
|
261
|
+
public Mat onCameraFrame(Mat inputFrame) {
|
262
|
+
|
263
|
+
Mat src = inputFrame;
|
264
|
+
|
265
|
+
Mat src1 = src.clone();
|
266
|
+
|
267
|
+
Imgproc.cvtColor(src, src, Imgproc.COLOR_RGB2HSV);//HSVに変換
|
268
|
+
|
269
|
+
Imgproc.medianBlur(src, src, 5);
|
270
|
+
|
271
|
+
Core.inRange(src, new Scalar(150,100,100),new Scalar(180,255,255),src);
|
272
|
+
|
273
|
+
Mat hierarchy=Mat.zeros(new Size(5,5), CvType.CV_8UC1);
|
274
|
+
|
275
|
+
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
|
276
|
+
|
277
|
+
Imgproc.findContours(src, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_TC89_L1);
|
278
|
+
|
279
|
+
Scalar color=new Scalar(200,20,100);
|
280
|
+
|
281
|
+
// Imgproc.drawContours(src1, contours, -1, color,10);
|
282
|
+
|
283
|
+
int i=0;
|
284
|
+
|
285
|
+
int index = -1;
|
286
|
+
|
287
|
+
double area = 0;
|
288
|
+
|
289
|
+
for(i=0;i<contours.size();i++)
|
290
|
+
|
291
|
+
{
|
292
|
+
|
293
|
+
double tmp = Imgproc.contourArea(contours.get(i));
|
294
|
+
|
295
|
+
if (area < tmp) {
|
296
|
+
|
297
|
+
area = tmp;
|
298
|
+
|
299
|
+
index = i;
|
154
300
|
|
155
301
|
}
|
156
302
|
|
157
|
-
});
|
158
|
-
|
159
|
-
}
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
private void turnover(Bitmap bmp){
|
164
|
-
|
165
|
-
Mat src = new Mat();
|
166
|
-
|
167
|
-
Utils.bitmapToMat(bmp, src);
|
168
|
-
|
169
|
-
Mat src1 = src.clone();
|
170
|
-
|
171
|
-
Imgproc.cvtColor(src, src, Imgproc.COLOR_RGB2HSV);
|
172
|
-
|
173
|
-
Imgproc.medianBlur(src, src, 5);
|
174
|
-
|
175
|
-
Core.inRange(src, new Scalar(150,100,100),new Scalar(180,255,255),src);
|
176
|
-
|
177
|
-
Mat hierarchy=Mat.zeros(new Size(5,5), CvType.CV_8UC1);
|
178
|
-
|
179
|
-
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
|
180
|
-
|
181
|
-
Imgproc.findContours(src, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_TC89_L1);
|
182
|
-
|
183
|
-
Mat dst=Mat.zeros(new Size(src.width(),src.height()),CvType.CV_8UC3);
|
184
|
-
|
185
|
-
Scalar color=new Scalar(100,200,100);
|
186
|
-
|
187
|
-
//Imgproc.drawContours(src1, contours, -1, color,10);
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
int i=0;
|
192
|
-
|
193
|
-
int index = -1;
|
194
|
-
|
195
|
-
double area = 0;
|
196
|
-
|
197
|
-
for(i=0;i<contours.size();i++)
|
198
|
-
|
199
|
-
{
|
200
|
-
|
201
|
-
double tmp = Imgproc.contourArea(contours.get(i));
|
202
|
-
|
203
|
-
if (area < tmp) {
|
204
|
-
|
205
|
-
area = tmp;
|
206
|
-
|
207
|
-
index = i;
|
208
|
-
|
209
|
-
}
|
210
|
-
|
211
303
|
}
|
212
304
|
|
213
|
-
|
214
|
-
|
215
305
|
if (index != -1) {
|
216
306
|
|
217
307
|
MatOfPoint ptmat= contours.get(index);
|
218
308
|
|
219
|
-
color=new Scalar(0,200,0);
|
309
|
+
color=new Scalar(0,200,0);
|
220
310
|
|
221
311
|
MatOfPoint2f ptmat2 = new MatOfPoint2f( ptmat.toArray() );
|
222
312
|
|
@@ -226,39 +316,7 @@
|
|
226
316
|
|
227
317
|
}
|
228
318
|
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
Utils.matToBitmap(src1, bmp); // Mat -> Bitmap
|
234
|
-
|
235
|
-
mImageView.setImageBitmap(bmp);
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
}
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
@Override
|
244
|
-
|
245
|
-
public void onResume() {
|
246
|
-
|
247
|
-
|
319
|
+
return src1;
|
248
|
-
|
249
|
-
if (!OpenCVLoader.initDebug()) {
|
250
|
-
|
251
|
-
Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
|
252
|
-
|
253
|
-
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
|
254
|
-
|
255
|
-
} else {
|
256
|
-
|
257
|
-
Log.d(TAG, "OpenCV library found inside package. Using it!");
|
258
|
-
|
259
|
-
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
|
260
|
-
|
261
|
-
}
|
262
320
|
|
263
321
|
}
|
264
322
|
|
@@ -266,6 +324,8 @@
|
|
266
324
|
|
267
325
|
}
|
268
326
|
|
327
|
+
|
328
|
+
|
269
329
|
```
|
270
330
|
|
271
331
|
|
2
変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -313,9 +313,3 @@
|
|
313
313
|
</RelativeLayout>
|
314
314
|
|
315
315
|
```
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
[こちら](https://qiita.com/kodai100/items/6c9e8a34d0714913c017)を参考にしてopencvをandroidに導入した後
|
320
|
-
|
321
|
-
コピペしてXXXと書かれている部分を直してもらえれば使えるようになります。
|
1
変更
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
android opencv
|
1
|
+
android opencv 座標を返す
|
test
CHANGED
File without changes
|