質問編集履歴

4

ソースコードの削除

2022/12/15 03:32

投稿

naki_a
naki_a

スコア6

test CHANGED
File without changes
test CHANGED
@@ -29,114 +29,6 @@
29
29
  ### 該当のソースコード
30
30
 
31
31
  ```Java
32
- package com.android.example.samplecameraxandopencv;
33
-
34
- import static org.opencv.core.CvType.CV_8U;
35
- import static org.opencv.core.CvType.CV_8UC3;
36
- import static org.opencv.core.CvType.CV_8UC4;
37
-
38
- import androidx.annotation.NonNull;
39
- import androidx.appcompat.app.AppCompatActivity;
40
- import androidx.camera.core.Camera;
41
- import androidx.camera.core.CameraSelector;
42
- import androidx.camera.core.ImageAnalysis;
43
- import androidx.camera.core.ImageProxy;
44
- import androidx.camera.core.Preview;
45
- import androidx.camera.lifecycle.ProcessCameraProvider;
46
- import androidx.camera.view.PreviewView;
47
- import androidx.core.app.ActivityCompat;
48
- import androidx.core.content.ContextCompat;
49
- import androidx.lifecycle.LifecycleOwner;
50
-
51
- import android.Manifest;
52
- import android.app.Activity;
53
- import android.content.Context;
54
- import android.content.pm.PackageManager;
55
- import android.graphics.Bitmap;
56
- import android.os.Bundle;
57
- import android.util.Log;
58
- import android.view.Surface;
59
- import android.widget.ImageView;
60
-
61
- import com.google.common.util.concurrent.ListenableFuture;
62
-
63
- import org.opencv.android.OpenCVLoader;
64
- import org.opencv.android.Utils;
65
- import org.opencv.core.Core;
66
- import org.opencv.core.CvType;
67
- import org.opencv.core.Mat;
68
- import org.opencv.core.Point;
69
- import org.opencv.core.Rect;
70
- import org.opencv.core.Scalar;
71
- import org.opencv.core.Size;
72
- import org.opencv.imgproc.Imgproc;
73
-
74
- import java.nio.ByteBuffer;
75
- import java.util.concurrent.ExecutorService;
76
- import java.util.concurrent.Executors;
77
-
78
- public class MainActivity extends AppCompatActivity {
79
-
80
- /*** Fixed values - 固定値 ***/
81
- private static final String TAG = "MyApp";
82
- private int REQUEST_CODE_FOR_PREMISSIONS = 1234;
83
- private final String[] REQUIRED_PERMISSIONS = new String[]{"android.permission.CAMERA", "android.permission.WRITE_EXTERNAL_STORAGE"};
84
-
85
- /*** Views ***/
86
- private PreviewView previewView;
87
- private ImageView imageView;
88
- /*** For CameraX ***/
89
- private Camera camera = null;
90
- private Preview preview = null;
91
- private ImageAnalysis imageAnalysis = null;
92
- private ExecutorService cameraExecutor = Executors.newSingleThreadExecutor();
93
-
94
- static {
95
- System.loadLibrary("opencv_java4"); // 追加
96
- }
97
-
98
- @Override
99
- protected void onCreate(Bundle savedInstanceState) {
100
- super.onCreate(savedInstanceState);
101
- setContentView(R.layout.activity_main);
102
-
103
- previewView = findViewById(R.id.previewView);
104
- imageView = findViewById(R.id.imageView);
105
-
106
- if(checkPermissions()){
107
- startCamera();
108
- } else {
109
- ActivityCompat.requestPermissions(this,REQUIRED_PERMISSIONS,REQUEST_CODE_FOR_PREMISSIONS);
110
- }
111
- }
112
-
113
- private void startCamera(){
114
- final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
115
- Context context = this;
116
- cameraProviderFuture.addListener(new Runnable() {
117
- @Override
118
- public void run() {
119
- try {
120
- ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
121
- preview = new Preview.Builder().build();
122
- imageAnalysis = new ImageAnalysis.Builder().build();
123
- imageAnalysis.setAnalyzer(cameraExecutor, new MyImageAnalyzer());
124
- CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
125
-
126
- cameraProvider.unbindAll();
127
- camera = cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector,preview, imageAnalysis);
128
- preview.setSurfaceProvider(previewView.createSurfaceProvider(camera.getCameraInfo()));
129
- } catch (Exception e){
130
- Log.e(TAG, "[StartCamera] Use case binding failed", e);
131
- }
132
- }
133
- },ContextCompat.getMainExecutor(this));
134
- }
135
-
136
- private class MyImageAnalyzer implements ImageAnalysis.Analyzer {
137
- private Mat matPrevious = null;
138
-
139
- @Override
140
32
  public void analyze(@NonNull ImageProxy image) {
141
33
  /* Create cv::mat(RGB888) from image(NV21) */
142
34
  Mat matOrg = getMatFromImage(image);
@@ -161,88 +53,6 @@
161
53
  mat.put(i,j,data);
162
54
  }
163
55
  }
164
-
165
- /* Convert cv::mat to bitmap for drawing */
166
- Bitmap bitmap = Bitmap.createBitmap(mat.cols(), mat.rows(),Bitmap.Config.ARGB_8888);
167
- Utils.matToBitmap(mat, bitmap);
168
-
169
- /* Display the result onto ImageView */
170
- runOnUiThread(new Runnable() {
171
- @Override
172
- public void run() {
173
- imageView.setImageBitmap(bitmap);
174
- }
175
- });
176
-
177
- /* Close the image otherwise, this function is not called next time */
178
- image.close();
179
- }
180
-
181
-
182
- //NV21画像をOpenCVで使えるようにする(mat)
183
- private Mat getMatFromImage(ImageProxy image) {
184
- /* https://stackoverflow.com/questions/30510928/convert-android-camera2-api-yuv-420-888-to-rgb */
185
- ByteBuffer yBuffer = image.getPlanes()[0].getBuffer();
186
- ByteBuffer uBuffer = image.getPlanes()[1].getBuffer();
187
- ByteBuffer vBuffer = image.getPlanes()[2].getBuffer();
188
- int ySize = yBuffer.remaining();
189
- int uSize = uBuffer.remaining();
190
- int vSize = vBuffer.remaining();
191
- byte[] nv21 = new byte[ySize + uSize + vSize];
192
- yBuffer.get(nv21, 0, ySize);
193
- vBuffer.get(nv21, ySize, vSize);
194
- uBuffer.get(nv21, ySize + vSize, uSize);
195
- Mat yuv = new Mat(image.getHeight() + image.getHeight() / 2, image.getWidth(), CvType.CV_8UC1);
196
- yuv.put(0, 0, nv21);
197
- Mat mat = new Mat();
198
- Imgproc.cvtColor(yuv, mat, Imgproc.COLOR_YUV2RGB_NV21, 3);
199
- return mat;
200
- }
201
-
202
- private Mat fixMatRotation(Mat matOrg) {
203
- Mat mat;
204
- switch (previewView.getDisplay().getRotation()){
205
- default:
206
- case Surface.ROTATION_0:
207
- mat = new Mat(matOrg.cols(), matOrg.rows(), matOrg.type());
208
- Core.transpose(matOrg, mat);
209
- Core.flip(mat, mat, 1);
210
- break;
211
- case Surface.ROTATION_90:
212
- mat = matOrg;
213
- break;
214
- case Surface.ROTATION_270:
215
- mat = matOrg;
216
- Core.flip(mat, mat, -1);
217
- break;
218
- }
219
- return mat;
220
- }
221
- }
222
-
223
- private boolean checkPermissions(){
224
- for(String permission : REQUIRED_PERMISSIONS){
225
- if(ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED){
226
- return false;
227
- }
228
- }
229
- return true;
230
- }
231
-
232
- @Override
233
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
234
- // super.onRequestPermissionsResult(requestCode, permissions, grantResults);
235
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
236
- if (requestCode == REQUEST_CODE_FOR_PREMISSIONS){
237
- if (checkPermissions()) {
238
- startCamera();
239
- } else {
240
- Log.i(TAG, "[onRequestPermissionsResult] Failed to get permissions");
241
- this.finish();
242
- }
243
- }
244
- }
245
- }
246
56
  ```
247
57
 
248
58
  ### 試したこと

3

コードの追記

2022/12/07 14:37

投稿

naki_a
naki_a

スコア6

test CHANGED
File without changes
test CHANGED
@@ -29,7 +29,111 @@
29
29
  ### 該当のソースコード
30
30
 
31
31
  ```Java
32
+ package com.android.example.samplecameraxandopencv;
33
+
34
+ import static org.opencv.core.CvType.CV_8U;
35
+ import static org.opencv.core.CvType.CV_8UC3;
36
+ import static org.opencv.core.CvType.CV_8UC4;
37
+
38
+ import androidx.annotation.NonNull;
39
+ import androidx.appcompat.app.AppCompatActivity;
40
+ import androidx.camera.core.Camera;
41
+ import androidx.camera.core.CameraSelector;
42
+ import androidx.camera.core.ImageAnalysis;
43
+ import androidx.camera.core.ImageProxy;
44
+ import androidx.camera.core.Preview;
45
+ import androidx.camera.lifecycle.ProcessCameraProvider;
46
+ import androidx.camera.view.PreviewView;
47
+ import androidx.core.app.ActivityCompat;
48
+ import androidx.core.content.ContextCompat;
49
+ import androidx.lifecycle.LifecycleOwner;
50
+
51
+ import android.Manifest;
52
+ import android.app.Activity;
53
+ import android.content.Context;
54
+ import android.content.pm.PackageManager;
55
+ import android.graphics.Bitmap;
56
+ import android.os.Bundle;
57
+ import android.util.Log;
58
+ import android.view.Surface;
59
+ import android.widget.ImageView;
60
+
61
+ import com.google.common.util.concurrent.ListenableFuture;
62
+
63
+ import org.opencv.android.OpenCVLoader;
64
+ import org.opencv.android.Utils;
65
+ import org.opencv.core.Core;
66
+ import org.opencv.core.CvType;
67
+ import org.opencv.core.Mat;
68
+ import org.opencv.core.Point;
69
+ import org.opencv.core.Rect;
70
+ import org.opencv.core.Scalar;
71
+ import org.opencv.core.Size;
72
+ import org.opencv.imgproc.Imgproc;
73
+
74
+ import java.nio.ByteBuffer;
75
+ import java.util.concurrent.ExecutorService;
76
+ import java.util.concurrent.Executors;
77
+
78
+ public class MainActivity extends AppCompatActivity {
79
+
80
+ /*** Fixed values - 固定値 ***/
81
+ private static final String TAG = "MyApp";
82
+ private int REQUEST_CODE_FOR_PREMISSIONS = 1234;
83
+ private final String[] REQUIRED_PERMISSIONS = new String[]{"android.permission.CAMERA", "android.permission.WRITE_EXTERNAL_STORAGE"};
84
+
85
+ /*** Views ***/
86
+ private PreviewView previewView;
87
+ private ImageView imageView;
88
+ /*** For CameraX ***/
89
+ private Camera camera = null;
90
+ private Preview preview = null;
91
+ private ImageAnalysis imageAnalysis = null;
92
+ private ExecutorService cameraExecutor = Executors.newSingleThreadExecutor();
93
+
94
+ static {
95
+ System.loadLibrary("opencv_java4"); // 追加
96
+ }
97
+
98
+ @Override
99
+ protected void onCreate(Bundle savedInstanceState) {
100
+ super.onCreate(savedInstanceState);
101
+ setContentView(R.layout.activity_main);
102
+
103
+ previewView = findViewById(R.id.previewView);
104
+ imageView = findViewById(R.id.imageView);
105
+
106
+ if(checkPermissions()){
107
+ startCamera();
108
+ } else {
109
+ ActivityCompat.requestPermissions(this,REQUIRED_PERMISSIONS,REQUEST_CODE_FOR_PREMISSIONS);
110
+ }
111
+ }
112
+
113
+ private void startCamera(){
114
+ final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
115
+ Context context = this;
116
+ cameraProviderFuture.addListener(new Runnable() {
117
+ @Override
118
+ public void run() {
119
+ try {
120
+ ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
121
+ preview = new Preview.Builder().build();
122
+ imageAnalysis = new ImageAnalysis.Builder().build();
123
+ imageAnalysis.setAnalyzer(cameraExecutor, new MyImageAnalyzer());
124
+ CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
125
+
126
+ cameraProvider.unbindAll();
127
+ camera = cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector,preview, imageAnalysis);
128
+ preview.setSurfaceProvider(previewView.createSurfaceProvider(camera.getCameraInfo()));
129
+ } catch (Exception e){
130
+ Log.e(TAG, "[StartCamera] Use case binding failed", e);
131
+ }
132
+ }
133
+ },ContextCompat.getMainExecutor(this));
134
+ }
135
+
32
- private class MyImageAnalyzer implements ImageAnalysis.Analyzer {
136
+ private class MyImageAnalyzer implements ImageAnalysis.Analyzer {
33
137
  private Mat matPrevious = null;
34
138
 
35
139
  @Override
@@ -39,6 +143,12 @@
39
143
  /* 画像を回転 */
40
144
  Mat fixMat = fixMatRotation(matOrg);
41
145
 
146
+ /***
147
+ Log.i(TAG, "[analyze] width = " + image.getWidth() + ", height = " + image.getHeight() + "Rotation = " + previewView.getDisplay().getRotation());
148
+ Log.i(TAG, "[analyze] mat width = " + fixMat.cols() + ", mat height = " + fixMat.rows());
149
+ type = 16, depth = 0, channels = 3, isContinuous = true
150
+ Log.i(TAG, "type = " + fixMat.type() + ", depth = " + fixMat.depth() + ", channels = " + fixMat.channels() + ", isContinuous = " + fixMat.isContinuous());
151
+ ***/
42
152
  Size sizeMat = fixMat.size();
43
153
  Mat mat = fixMat.clone();
44
154
 
@@ -51,22 +161,88 @@
51
161
  mat.put(i,j,data);
52
162
  }
53
163
  }
54
-      //試したこと
164
+
55
-       Imgproc.cvtColor(mat,mat,Imgproc.COLOR_RGB2GRAY);
56
- Imgproc.cvtColor(mat,mat,Imgproc.COLOR_GRAY2BGR);
57
-
58
- /* mat -> bitmap */
165
+ /* Convert cv::mat to bitmap for drawing */
59
166
  Bitmap bitmap = Bitmap.createBitmap(mat.cols(), mat.rows(),Bitmap.Config.ARGB_8888);
60
167
  Utils.matToBitmap(mat, bitmap);
61
168
 
169
+ /* Display the result onto ImageView */
62
170
  runOnUiThread(new Runnable() {
63
171
  @Override
64
172
  public void run() {
65
173
  imageView.setImageBitmap(bitmap);
66
174
  }
67
175
  });
176
+
177
+ /* Close the image otherwise, this function is not called next time */
68
178
  image.close();
69
179
  }
180
+
181
+
182
+ //NV21画像をOpenCVで使えるようにする(mat)
183
+ private Mat getMatFromImage(ImageProxy image) {
184
+ /* https://stackoverflow.com/questions/30510928/convert-android-camera2-api-yuv-420-888-to-rgb */
185
+ ByteBuffer yBuffer = image.getPlanes()[0].getBuffer();
186
+ ByteBuffer uBuffer = image.getPlanes()[1].getBuffer();
187
+ ByteBuffer vBuffer = image.getPlanes()[2].getBuffer();
188
+ int ySize = yBuffer.remaining();
189
+ int uSize = uBuffer.remaining();
190
+ int vSize = vBuffer.remaining();
191
+ byte[] nv21 = new byte[ySize + uSize + vSize];
192
+ yBuffer.get(nv21, 0, ySize);
193
+ vBuffer.get(nv21, ySize, vSize);
194
+ uBuffer.get(nv21, ySize + vSize, uSize);
195
+ Mat yuv = new Mat(image.getHeight() + image.getHeight() / 2, image.getWidth(), CvType.CV_8UC1);
196
+ yuv.put(0, 0, nv21);
197
+ Mat mat = new Mat();
198
+ Imgproc.cvtColor(yuv, mat, Imgproc.COLOR_YUV2RGB_NV21, 3);
199
+ return mat;
200
+ }
201
+
202
+ private Mat fixMatRotation(Mat matOrg) {
203
+ Mat mat;
204
+ switch (previewView.getDisplay().getRotation()){
205
+ default:
206
+ case Surface.ROTATION_0:
207
+ mat = new Mat(matOrg.cols(), matOrg.rows(), matOrg.type());
208
+ Core.transpose(matOrg, mat);
209
+ Core.flip(mat, mat, 1);
210
+ break;
211
+ case Surface.ROTATION_90:
212
+ mat = matOrg;
213
+ break;
214
+ case Surface.ROTATION_270:
215
+ mat = matOrg;
216
+ Core.flip(mat, mat, -1);
217
+ break;
218
+ }
219
+ return mat;
220
+ }
221
+ }
222
+
223
+ private boolean checkPermissions(){
224
+ for(String permission : REQUIRED_PERMISSIONS){
225
+ if(ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED){
226
+ return false;
227
+ }
228
+ }
229
+ return true;
230
+ }
231
+
232
+ @Override
233
+ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
234
+ // super.onRequestPermissionsResult(requestCode, permissions, grantResults);
235
+ super.onRequestPermissionsResult(requestCode, permissions, grantResults);
236
+ if (requestCode == REQUEST_CODE_FOR_PREMISSIONS){
237
+ if (checkPermissions()) {
238
+ startCamera();
239
+ } else {
240
+ Log.i(TAG, "[onRequestPermissionsResult] Failed to get permissions");
241
+ this.finish();
242
+ }
243
+ }
244
+ }
245
+ }
70
246
  ```
71
247
 
72
248
  ### 試したこと

2

誤字

2022/12/07 03:37

投稿

naki_a
naki_a

スコア6

test CHANGED
File without changes
test CHANGED
@@ -70,7 +70,8 @@
70
70
  ```
71
71
 
72
72
  ### 試したこと
73
- fitmatの要素を取得したところ、type = 16, depth = 0, channels = 3, isContinuous = trueが返ってきました。
73
+ fixmatの要素を取得したところ、type = 16, depth = 0, channels = 3, isContinuous = trueが返ってきました。
74
+ ビット深度はCV_8U, チャンネル数が3のカラー画像です。
74
75
 
75
76
  グレースケール画像では正常に表示されるので、
76
77
  NV21→RGB(Mat型)→RGB2GRAY→GRA2RGB(BGRも試しました)→Bitmapに変換

1

ソースコードの追加

2022/12/07 03:10

投稿

naki_a
naki_a

スコア6

test CHANGED
@@ -1 +1 @@
1
- MatからBitMapに変換した画像が正常に表示されない
1
+ [Java]MatからBitMapに変換した画像が正常に表示されない
test CHANGED
@@ -51,10 +51,11 @@
51
51
  mat.put(i,j,data);
52
52
  }
53
53
  }
54
-      //試したこと
54
+      //試したこと
55
+       Imgproc.cvtColor(mat,mat,Imgproc.COLOR_RGB2GRAY);
56
+ Imgproc.cvtColor(mat,mat,Imgproc.COLOR_GRAY2BGR);
55
57
 
56
58
  /* mat -> bitmap */
57
- //試したこと①
58
59
  Bitmap bitmap = Bitmap.createBitmap(mat.cols(), mat.rows(),Bitmap.Config.ARGB_8888);
59
60
  Utils.matToBitmap(mat, bitmap);
60
61
 
@@ -71,12 +72,6 @@
71
72
  ### 試したこと
72
73
  fitmatの要素を取得したところ、type = 16, depth = 0, channels = 3, isContinuous = trueが返ってきました。
73
74
 
74
- 試したこと①
75
- createBitmapの設定が間違えているのかと思い、ARGB_8888からいろいろな設定に変更しました。
76
- その結果、ARGB_8888とRGB_565は実行できるものの色は画像の通り赤と緑でおかしくなり、
77
- ALPHA_8,RGBA_1010102,RGBA_F16ではそもそもアプリが起動せず落ちてしまいました。
78
-
79
- 試したこと②
80
75
  グレースケール画像では正常に表示されるので、
81
76
  NV21→RGB(Mat型)→RGB2GRAY→GRA2RGB(BGRも試しました)→Bitmapに変換
82
77
  という流れでカラーにしようと思ったのですが表示されるのはグレースケール画像でカラーに戻りませんでした。