質問編集履歴
1
変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,14 +1,71 @@
|
|
1
1
|
私はandroidstudioでアプリ開発を行っています。
|
2
2
|
|
3
|
+
以下のプログラムは赤い領域を見つけ出し、その中心に点を描画するものです。下の方に書いてあるx,yをテキストとして表示させるにはどうすればいいでしょうか?
|
3
4
|
|
4
|
-
|
5
|
+
|
5
|
-
(例えばlayoutには座標を表示しているテキストだけ)というものを作りたいと思っているのですがそもそも実現可能でしょうか?
|
6
|
-
onCreateの.setVisibility(SurfaceView.VISIBLE);をINVISIBLEにするだけで解決するのでしょうか?
|
7
6
|
```java
|
8
7
|
|
8
|
+
import android.app.Activity;
|
9
|
+
import android.os.Bundle;
|
10
|
+
import android.util.Log;
|
11
|
+
import android.view.MenuItem;
|
9
|
-
|
12
|
+
import android.view.SurfaceView;
|
13
|
+
import android.view.WindowManager;
|
14
|
+
import org.opencv.android.BaseLoaderCallback;
|
15
|
+
import org.opencv.android.CameraBridgeViewBase;
|
16
|
+
import org.opencv.android.LoaderCallbackInterface;
|
17
|
+
import org.opencv.android.OpenCVLoader;
|
18
|
+
import org.opencv.core.Core;
|
19
|
+
import org.opencv.core.CvType;
|
20
|
+
import org.opencv.core.Mat;
|
21
|
+
import org.opencv.core.MatOfPoint;
|
22
|
+
import org.opencv.core.MatOfPoint2f;
|
23
|
+
import org.opencv.core.Point;
|
24
|
+
import org.opencv.core.RotatedRect;
|
25
|
+
import org.opencv.core.Scalar;
|
26
|
+
import org.opencv.core.Size;
|
27
|
+
import org.opencv.imgproc.Imgproc;
|
10
28
|
|
29
|
+
|
30
|
+
import java.util.ArrayList;
|
31
|
+
import java.util.List;
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
public class MainActivity extends Activity implements CameraBridgeViewBase.CvCameraViewListener {
|
36
|
+
private static final String TAG = "OCVSample::Activity";
|
37
|
+
|
38
|
+
int i =0;
|
39
|
+
double x=0;
|
40
|
+
double y=0;
|
41
|
+
|
42
|
+
private CameraBridgeViewBase mOpenCvCameraView;
|
43
|
+
private boolean mIsJavaCamera = true;
|
44
|
+
private MenuItem mItemSwitchCamera = null;
|
45
|
+
|
46
|
+
|
47
|
+
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
|
11
|
-
|
48
|
+
@Override
|
49
|
+
public void onManagerConnected(int status) {
|
50
|
+
switch (status) {
|
51
|
+
// 読み込みが成功したらカメラプレビューを開始
|
52
|
+
case LoaderCallbackInterface.SUCCESS:
|
53
|
+
{
|
54
|
+
Log.i(TAG, "OpenCV loaded successfully");
|
55
|
+
mOpenCvCameraView.enableView();
|
56
|
+
} break;
|
57
|
+
default: {
|
58
|
+
super.onManagerConnected(status);
|
59
|
+
}break;
|
60
|
+
}
|
61
|
+
}
|
62
|
+
};
|
63
|
+
|
64
|
+
public MainActivity() {
|
65
|
+
Log.i(TAG, "Instantiated new " + this.getClass());
|
66
|
+
}
|
67
|
+
|
68
|
+
@Override
|
12
69
|
protected void onCreate(Bundle savedInstanceState) {
|
13
70
|
Log.i(TAG, "called onCreate");
|
14
71
|
super.onCreate(savedInstanceState);
|
@@ -17,8 +74,84 @@
|
|
17
74
|
|
18
75
|
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.camera_view);
|
19
76
|
|
20
|
-
mOpenCvCameraView.setVisibility(SurfaceView.
|
77
|
+
mOpenCvCameraView.setVisibility(SurfaceView.INVISIBLE);
|
21
78
|
|
22
79
|
mOpenCvCameraView.setCvCameraViewListener(this);
|
23
80
|
}
|
81
|
+
|
82
|
+
@Override
|
83
|
+
protected void onPause() {
|
84
|
+
super.onPause();
|
85
|
+
if (mOpenCvCameraView != null)
|
86
|
+
mOpenCvCameraView.disableView();
|
87
|
+
}
|
88
|
+
|
89
|
+
@Override
|
90
|
+
protected void onResume() {
|
91
|
+
super.onResume();
|
92
|
+
if (!OpenCVLoader.initDebug()) {
|
93
|
+
Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
|
94
|
+
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
|
95
|
+
} else {
|
96
|
+
Log.d(TAG, "OpenCV library found inside package. Using it!");
|
97
|
+
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
|
98
|
+
}
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
@Override
|
103
|
+
protected void onDestroy() {
|
104
|
+
super.onDestroy();
|
105
|
+
if (mOpenCvCameraView != null)
|
106
|
+
mOpenCvCameraView.disableView();
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
@Override
|
111
|
+
public void onCameraViewStarted(int width, int height) {// カメラプレビュー開始時に呼ばれる
|
112
|
+
}
|
113
|
+
|
114
|
+
@Override
|
115
|
+
public void onCameraViewStopped() {// カメラプレビュー終了時に呼ばれる
|
116
|
+
}
|
117
|
+
|
118
|
+
@Override
|
119
|
+
public Mat onCameraFrame(Mat inputFrame) {
|
120
|
+
Mat src = inputFrame;
|
121
|
+
Mat src1 = src.clone();
|
122
|
+
Imgproc.cvtColor(src, src, Imgproc.COLOR_RGB2HSV);//HSVに変換
|
123
|
+
Imgproc.medianBlur(src, src, 5);
|
124
|
+
Core.inRange(src, new Scalar(150,100,100),new Scalar(180,255,255),src);
|
125
|
+
Mat hierarchy=Mat.zeros(new Size(5,5), CvType.CV_8UC1);
|
126
|
+
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
|
127
|
+
Imgproc.findContours(src, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_TC89_L1);
|
128
|
+
//Scalar color=new Scalar(200,20,100);
|
129
|
+
// Imgproc.drawContours(src1, contours, -1, color,10);
|
130
|
+
int i=0;
|
131
|
+
int index = -1;
|
132
|
+
double area = 0;
|
133
|
+
for(i=0;i<contours.size();i++)
|
134
|
+
{
|
135
|
+
double tmp = Imgproc.contourArea(contours.get(i));
|
136
|
+
if (area < tmp) {
|
137
|
+
area = tmp;
|
138
|
+
index = i;
|
139
|
+
}
|
140
|
+
}
|
141
|
+
if (index != -1) {
|
142
|
+
MatOfPoint ptmat= contours.get(index);
|
143
|
+
Scalar color=new Scalar(0,200,0);
|
144
|
+
MatOfPoint2f ptmat2 = new MatOfPoint2f( ptmat.toArray() );
|
145
|
+
RotatedRect bbox=Imgproc.minAreaRect(ptmat2);
|
146
|
+
Point point = bbox.center;
|
147
|
+
x = point.x;
|
148
|
+
y = point.y;
|
149
|
+
Point point2 = new Point(x,y);
|
150
|
+
Imgproc.circle(src1, bbox.center,5, color,-1);
|
151
|
+
}
|
152
|
+
return src1;
|
153
|
+
}
|
154
|
+
|
155
|
+
}
|
156
|
+
|
24
157
|
```
|