質問編集履歴
4
変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
私が書いたプログラムはカメラで撮っている映像の中に映る赤い物の中心に点を描画する物です。
|
4
4
|
|
5
|
-
この中心点の座標を獲得したいのですがどうすればいいでしょうか?
|
5
|
+
この中心点の座標を獲得したいのですがどうすればいいでしょうか?Pointをintなどに変換できるのでしょうか?
|
6
6
|
|
7
7
|
[こちら](https://qiita.com/kodai100/items/6c9e8a34d0714913c017)を参考にしてopencvをandroidに導入した後
|
8
8
|
以下のコードをそれぞれコピペしてXXXと書かれている部分を直してもらえれば実際に使えるようになると思います。
|
3
変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -12,87 +12,133 @@
|
|
12
12
|
```java
|
13
13
|
package com.example.XXX.XXX;
|
14
14
|
|
15
|
+
import android.app.Activity;
|
15
|
-
import
|
16
|
+
import android.os.Bundle;
|
16
|
-
import
|
17
|
+
import android.util.Log;
|
17
|
-
|
18
|
+
import android.view.MenuItem;
|
19
|
+
import android.view.SurfaceView;
|
20
|
+
import android.view.WindowManager;
|
18
21
|
import org.opencv.android.BaseLoaderCallback;
|
22
|
+
import org.opencv.android.CameraBridgeViewBase;
|
19
23
|
import org.opencv.android.LoaderCallbackInterface;
|
20
24
|
import org.opencv.android.OpenCVLoader;
|
21
|
-
import org.opencv.
|
25
|
+
import org.opencv.core.Core;
|
26
|
+
import org.opencv.core.CvType;
|
22
|
-
import org.opencv.core.
|
27
|
+
import org.opencv.core.Mat;
|
28
|
+
import org.opencv.core.MatOfInt;
|
29
|
+
import org.opencv.core.MatOfInt4;
|
30
|
+
import org.opencv.core.MatOfPoint;
|
31
|
+
import org.opencv.core.MatOfPoint2f;
|
32
|
+
import org.opencv.core.Point;
|
33
|
+
import org.opencv.core.RotatedRect;
|
34
|
+
import org.opencv.core.Scalar;
|
35
|
+
import org.opencv.core.Size;
|
23
36
|
import org.opencv.imgproc.Imgproc;
|
37
|
+
import org.opencv.imgproc.Moments;
|
24
38
|
|
25
|
-
import android.os.Bundle;
|
26
|
-
import android.app.Activity;
|
27
|
-
import android.graphics.Bitmap;
|
28
|
-
import android.graphics.BitmapFactory;
|
29
|
-
import android.util.Log;
|
30
|
-
import android.view.View;
|
31
|
-
import android.view.View.OnClickListener;
|
32
|
-
import android.widget.Button;
|
33
|
-
import android.widget.ImageView;
|
34
|
-
import android.widget.TextView;
|
35
39
|
|
40
|
+
import java.util.ArrayList;
|
41
|
+
import java.util.List;
|
36
42
|
|
37
43
|
|
38
|
-
|
44
|
+
import static org.opencv.core.CvType.CV_8U;
|
39
|
-
|
45
|
+
import static org.opencv.core.CvType.CV_8UC1;
|
46
|
+
import static org.opencv.core.CvType.CV_8UC3;
|
40
47
|
|
41
|
-
private Button mButton = null;
|
42
|
-
private ImageView mImageView = null;
|
43
|
-
private Bitmap bmp = null;
|
44
48
|
|
49
|
+
public class MainActivity extends Activity implements CameraBridgeViewBase.CvCameraViewListener {
|
50
|
+
private static final String TAG = "OCVSample::Activity";
|
51
|
+
|
52
|
+
int i =0;
|
53
|
+
|
45
|
-
|
54
|
+
private CameraBridgeViewBase mOpenCvCameraView;
|
55
|
+
private boolean mIsJavaCamera = true;
|
56
|
+
private MenuItem mItemSwitchCamera = null;
|
57
|
+
|
58
|
+
|
46
59
|
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
|
47
60
|
@Override
|
48
61
|
public void onManagerConnected(int status) {
|
49
62
|
switch (status) {
|
63
|
+
// 読み込みが成功したらカメラプレビューを開始
|
50
64
|
case LoaderCallbackInterface.SUCCESS:
|
65
|
+
{
|
51
|
-
Log.i(TAG, "loaded successfully");
|
66
|
+
Log.i(TAG, "OpenCV loaded successfully");
|
52
|
-
|
67
|
+
mOpenCvCameraView.enableView();
|
53
|
-
|
68
|
+
} break;
|
54
|
-
default:
|
69
|
+
default: {
|
55
70
|
super.onManagerConnected(status);
|
56
|
-
|
71
|
+
}break;
|
57
72
|
}
|
58
73
|
}
|
59
74
|
};
|
60
75
|
|
76
|
+
public MainActivity() {
|
77
|
+
Log.i(TAG, "Instantiated new " + this.getClass());
|
78
|
+
}
|
79
|
+
|
61
80
|
@Override
|
62
81
|
protected void onCreate(Bundle savedInstanceState) {
|
82
|
+
Log.i(TAG, "called onCreate");
|
63
83
|
super.onCreate(savedInstanceState);
|
84
|
+
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
64
85
|
setContentView(R.layout.activity_main);
|
65
86
|
|
87
|
+
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.camera_view);
|
66
88
|
|
67
|
-
|
89
|
+
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
|
68
90
|
|
91
|
+
mOpenCvCameraView.setCvCameraViewListener(this);
|
92
|
+
}
|
69
93
|
|
94
|
+
@Override
|
95
|
+
protected void onPause() {
|
96
|
+
super.onPause();
|
70
|
-
|
97
|
+
if (mOpenCvCameraView != null)
|
71
|
-
|
98
|
+
mOpenCvCameraView.disableView();
|
99
|
+
}
|
72
100
|
|
73
|
-
mButton = (Button) findViewById(R.id.button1);
|
74
|
-
mButton.setOnClickListener(new OnClickListener() {
|
75
|
-
|
101
|
+
@Override
|
76
|
-
|
102
|
+
protected void onResume() {
|
77
|
-
|
103
|
+
super.onResume();
|
104
|
+
if (!OpenCVLoader.initDebug()) {
|
105
|
+
Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
|
106
|
+
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
|
107
|
+
} else {
|
108
|
+
Log.d(TAG, "OpenCV library found inside package. Using it!");
|
109
|
+
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
|
78
|
-
|
110
|
+
}
|
79
|
-
});
|
80
111
|
}
|
81
112
|
|
113
|
+
|
114
|
+
@Override
|
82
|
-
|
115
|
+
protected void onDestroy() {
|
116
|
+
super.onDestroy();
|
117
|
+
if (mOpenCvCameraView != null)
|
118
|
+
mOpenCvCameraView.disableView();
|
119
|
+
}
|
120
|
+
|
121
|
+
|
122
|
+
@Override
|
123
|
+
public void onCameraViewStarted(int width, int height) {// カメラプレビュー開始時に呼ばれる
|
124
|
+
}
|
125
|
+
|
126
|
+
@Override
|
127
|
+
public void onCameraViewStopped() {// カメラプレビュー終了時に呼ばれる
|
128
|
+
}
|
129
|
+
|
130
|
+
@Override
|
131
|
+
public Mat onCameraFrame(Mat inputFrame) {
|
83
|
-
Mat src =
|
132
|
+
Mat src = inputFrame;
|
84
|
-
Utils.bitmapToMat(bmp, src);
|
85
133
|
Mat src1 = src.clone();
|
86
|
-
Imgproc.cvtColor(src, src, Imgproc.COLOR_RGB2HSV);
|
134
|
+
Imgproc.cvtColor(src, src, Imgproc.COLOR_RGB2HSV);//HSVに変換
|
87
135
|
Imgproc.medianBlur(src, src, 5);
|
88
136
|
Core.inRange(src, new Scalar(150,100,100),new Scalar(180,255,255),src);
|
89
137
|
Mat hierarchy=Mat.zeros(new Size(5,5), CvType.CV_8UC1);
|
90
138
|
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
|
91
139
|
Imgproc.findContours(src, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_TC89_L1);
|
92
|
-
Mat dst=Mat.zeros(new Size(src.width(),src.height()),CvType.CV_8UC3);
|
93
|
-
Scalar color=new Scalar(
|
140
|
+
Scalar color=new Scalar(200,20,100);
|
94
|
-
|
141
|
+
// Imgproc.drawContours(src1, contours, -1, color,10);
|
95
|
-
|
96
142
|
int i=0;
|
97
143
|
int index = -1;
|
98
144
|
double area = 0;
|
@@ -104,34 +150,18 @@
|
|
104
150
|
index = i;
|
105
151
|
}
|
106
152
|
}
|
107
|
-
|
108
153
|
if (index != -1) {
|
109
154
|
MatOfPoint ptmat= contours.get(index);
|
110
|
-
|
155
|
+
color=new Scalar(0,200,0);
|
111
156
|
MatOfPoint2f ptmat2 = new MatOfPoint2f( ptmat.toArray() );
|
112
157
|
RotatedRect bbox=Imgproc.minAreaRect(ptmat2);
|
113
158
|
Imgproc.circle(src1, bbox.center,5, color,-1);
|
114
159
|
}
|
115
|
-
|
116
|
-
|
117
|
-
Utils.matToBitmap(src1, bmp); // Mat -> Bitmap
|
118
|
-
|
160
|
+
return src1;
|
119
|
-
|
120
161
|
}
|
121
162
|
|
122
|
-
@Override
|
123
|
-
public void onResume() {
|
124
|
-
super.onResume();
|
125
|
-
if (!OpenCVLoader.initDebug()) {
|
126
|
-
Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
|
127
|
-
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
|
128
|
-
} else {
|
129
|
-
Log.d(TAG, "OpenCV library found inside package. Using it!");
|
130
|
-
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
|
131
|
-
|
163
|
+
}
|
132
|
-
}
|
133
164
|
|
134
|
-
}
|
135
165
|
```
|
136
166
|
|
137
167
|
#activity_main.xml
|
2
変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -155,7 +155,4 @@
|
|
155
155
|
|
156
156
|
|
157
157
|
</RelativeLayout>
|
158
|
-
```
|
158
|
+
```
|
159
|
-
|
160
|
-
[こちら](https://qiita.com/kodai100/items/6c9e8a34d0714913c017)を参考にしてopencvをandroidに導入した後
|
161
|
-
コピペしてXXXと書かれている部分を直してもらえれば使えるようになります。
|
1
変更
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
android opencv
|
1
|
+
android opencv 座標を返す
|
body
CHANGED
File without changes
|