質問編集履歴

1

変更

2017/12/21 01:32

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -2,23 +2,137 @@
2
2
 
3
3
 
4
4
 
5
-
6
-
7
- 質問ですが、私は最終的にこの中心座標取得するプログラムをカメラ画面をディスプレイに表示させずに値だけを取得するもの
8
-
9
- (例えばlayoutには座標を表示しているテキストだけ)というものを作りたいと思っているのですがそもそも実現可能でしょうか?
10
-
11
- onCreate.setVisibility(SurfaceView.VISIBLE);INVISIBLEにするだけ解決するでしょうか?
5
+ 以下プログラムは赤い領域見つけ出し、その中心点を描画するものです。下の方に書いてあx,yをテキストとして表示させるにはどうすればいいでしょうか?
6
+
7
+
8
+
9
+
12
10
 
13
11
  ```java
14
12
 
15
13
 
16
14
 
15
+ import android.app.Activity;
16
+
17
+ import android.os.Bundle;
18
+
19
+ import android.util.Log;
20
+
21
+ import android.view.MenuItem;
22
+
23
+ import android.view.SurfaceView;
24
+
25
+ import android.view.WindowManager;
26
+
27
+ import org.opencv.android.BaseLoaderCallback;
28
+
29
+ import org.opencv.android.CameraBridgeViewBase;
30
+
31
+ import org.opencv.android.LoaderCallbackInterface;
32
+
33
+ import org.opencv.android.OpenCVLoader;
34
+
35
+ import org.opencv.core.Core;
36
+
37
+ import org.opencv.core.CvType;
38
+
39
+ import org.opencv.core.Mat;
40
+
41
+ import org.opencv.core.MatOfPoint;
42
+
43
+ import org.opencv.core.MatOfPoint2f;
44
+
45
+ import org.opencv.core.Point;
46
+
47
+ import org.opencv.core.RotatedRect;
48
+
49
+ import org.opencv.core.Scalar;
50
+
51
+ import org.opencv.core.Size;
52
+
53
+ import org.opencv.imgproc.Imgproc;
54
+
55
+
56
+
57
+
58
+
59
+ import java.util.ArrayList;
60
+
61
+ import java.util.List;
62
+
63
+
64
+
65
+
66
+
67
+
68
+
69
+ public class MainActivity extends Activity implements CameraBridgeViewBase.CvCameraViewListener {
70
+
71
+ private static final String TAG = "OCVSample::Activity";
72
+
73
+
74
+
75
+ int i =0;
76
+
77
+ double x=0;
78
+
79
+ double y=0;
80
+
81
+
82
+
17
- private CameraBridgeViewBase mOpenCvCameraView;
83
+ private CameraBridgeViewBase mOpenCvCameraView;
84
+
18
-
85
+ private boolean mIsJavaCamera = true;
86
+
19
-
87
+ private MenuItem mItemSwitchCamera = null;
88
+
89
+
90
+
91
+
92
+
20
-
93
+ private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
94
+
21
- @Override
95
+ @Override
96
+
97
+ public void onManagerConnected(int status) {
98
+
99
+ switch (status) {
100
+
101
+ // 読み込みが成功したらカメラプレビューを開始
102
+
103
+ case LoaderCallbackInterface.SUCCESS:
104
+
105
+ {
106
+
107
+ Log.i(TAG, "OpenCV loaded successfully");
108
+
109
+ mOpenCvCameraView.enableView();
110
+
111
+ } break;
112
+
113
+ default: {
114
+
115
+ super.onManagerConnected(status);
116
+
117
+ }break;
118
+
119
+ }
120
+
121
+ }
122
+
123
+ };
124
+
125
+
126
+
127
+ public MainActivity() {
128
+
129
+ Log.i(TAG, "Instantiated new " + this.getClass());
130
+
131
+ }
132
+
133
+
134
+
135
+ @Override
22
136
 
23
137
  protected void onCreate(Bundle savedInstanceState) {
24
138
 
@@ -36,7 +150,7 @@
36
150
 
37
151
 
38
152
 
39
- mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
153
+ mOpenCvCameraView.setVisibility(SurfaceView.INVISIBLE);
40
154
 
41
155
 
42
156
 
@@ -44,4 +158,156 @@
44
158
 
45
159
  }
46
160
 
161
+
162
+
163
+ @Override
164
+
165
+ protected void onPause() {
166
+
167
+ super.onPause();
168
+
169
+ if (mOpenCvCameraView != null)
170
+
171
+ mOpenCvCameraView.disableView();
172
+
173
+ }
174
+
175
+
176
+
177
+ @Override
178
+
179
+ protected void onResume() {
180
+
181
+ super.onResume();
182
+
183
+ if (!OpenCVLoader.initDebug()) {
184
+
185
+ Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
186
+
187
+ OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
188
+
189
+ } else {
190
+
191
+ Log.d(TAG, "OpenCV library found inside package. Using it!");
192
+
193
+ mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
194
+
195
+ }
196
+
197
+ }
198
+
199
+
200
+
201
+
202
+
203
+ @Override
204
+
205
+ protected void onDestroy() {
206
+
207
+ super.onDestroy();
208
+
209
+ if (mOpenCvCameraView != null)
210
+
211
+ mOpenCvCameraView.disableView();
212
+
213
+ }
214
+
215
+
216
+
217
+
218
+
219
+ @Override
220
+
221
+ public void onCameraViewStarted(int width, int height) {// カメラプレビュー開始時に呼ばれる
222
+
223
+ }
224
+
225
+
226
+
227
+ @Override
228
+
229
+ public void onCameraViewStopped() {// カメラプレビュー終了時に呼ばれる
230
+
231
+ }
232
+
233
+
234
+
235
+ @Override
236
+
237
+ public Mat onCameraFrame(Mat inputFrame) {
238
+
239
+ Mat src = inputFrame;
240
+
241
+ Mat src1 = src.clone();
242
+
243
+ Imgproc.cvtColor(src, src, Imgproc.COLOR_RGB2HSV);//HSVに変換
244
+
245
+ Imgproc.medianBlur(src, src, 5);
246
+
247
+ Core.inRange(src, new Scalar(150,100,100),new Scalar(180,255,255),src);
248
+
249
+ Mat hierarchy=Mat.zeros(new Size(5,5), CvType.CV_8UC1);
250
+
251
+ List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
252
+
253
+ Imgproc.findContours(src, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_TC89_L1);
254
+
255
+ //Scalar color=new Scalar(200,20,100);
256
+
257
+ // Imgproc.drawContours(src1, contours, -1, color,10);
258
+
259
+ int i=0;
260
+
261
+ int index = -1;
262
+
263
+ double area = 0;
264
+
265
+ for(i=0;i<contours.size();i++)
266
+
267
+ {
268
+
269
+ double tmp = Imgproc.contourArea(contours.get(i));
270
+
271
+ if (area < tmp) {
272
+
273
+ area = tmp;
274
+
275
+ index = i;
276
+
277
+ }
278
+
279
+ }
280
+
281
+ if (index != -1) {
282
+
283
+ MatOfPoint ptmat= contours.get(index);
284
+
285
+ Scalar color=new Scalar(0,200,0);
286
+
287
+ MatOfPoint2f ptmat2 = new MatOfPoint2f( ptmat.toArray() );
288
+
289
+ RotatedRect bbox=Imgproc.minAreaRect(ptmat2);
290
+
291
+ Point point = bbox.center;
292
+
293
+ x = point.x;
294
+
295
+ y = point.y;
296
+
297
+ Point point2 = new Point(x,y);
298
+
299
+ Imgproc.circle(src1, bbox.center,5, color,-1);
300
+
301
+ }
302
+
303
+ return src1;
304
+
305
+ }
306
+
307
+
308
+
309
+ }
310
+
311
+
312
+
47
313
  ```