/
MainActivity.java
519 lines (406 loc) · 15.9 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
package org.opencv.tutorials.imgproc;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.Toast;
public class MainActivity extends Activity implements CvCameraViewListener2, OnTouchListener {
private static final String TAG = "OCVSample::Activity";
private CameraBridgeViewBase mOpenCvCameraView;
private boolean mIsJavaCamera = true;
public static final int VIEW_MODE_A = 0;
public static final int VIEW_MODE_B = 1;
public static final int VIEW_MODE_C = 2;
public static final int VIEW_MODE_D = 3;
public static final int VIEW_MODE_E = 4;
public static final int VIEW_MODE_F = 5;
public static final int VIEW_MODE_G = 6;
public static final int VIEW_MODE_H = 7;
public static final int VIEW_MODE_I = 8;
public static final int VIEW_MODE_J = 9;
public static int viewMode = VIEW_MODE_A;
private MenuItem mItemSwitchCamera = null;
private MenuItem mItemPreviewA;
private MenuItem mItemPreviewB;
private MenuItem mItemPreviewC;
private MenuItem mItemPreviewD;
private MenuItem mItemPreviewE;
private MenuItem mItemPreviewF;
private MenuItem mItemPreviewG;
private MenuItem mItemPreviewH;
private MenuItem mItemPreviewI;
private MenuItem mItemPreviewJ;
private Mat mIntermediateMat;
private Mat mGray;
private Bitmap mBitmap;
private Mat mRgba;
private Mat mHSV;
private int thresh = 100;
private int max_thresh = 255;
/// Global Variables
int GLOBAL_ITER = 1;
int DELAY_CAPTION = 1500;
int DELAY_BLUR = 100;
int MAX_KERNEL_LENGTH = 31;
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
mOpenCvCameraView.enableView();
mOpenCvCameraView.setOnTouchListener(MainActivity.this);
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
public MainActivity() {
Log.i(TAG, "Instantiated new " + this.getClass());
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "called onCreate");
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.tutorial1_surface_view);
if (mIsJavaCamera)
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_java_surface_view);
else
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_native_surface_view);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
}
@Override
public void onPause()
{
super.onPause();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
@Override
public void onResume()
{
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_8, this, mLoaderCallback);
}
public void onDestroy() {
super.onDestroy();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.i(TAG, "called onCreateOptionsMenu");
mItemSwitchCamera = menu.add("Toggle Native/Java camera");
mItemPreviewA = menu.add("blur");
mItemPreviewB = menu.add("gaussian");
mItemPreviewC = menu.add("median");
mItemPreviewD = menu.add("bilateral");
mItemPreviewE = menu.add("erosion");
mItemPreviewF = menu.add("dilation");
mItemPreviewG = menu.add("morph");
mItemPreviewH = menu.add("findContours");
mItemPreviewI = menu.add("rotation");
mItemPreviewJ = menu.add("warpAffine");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
String toastMesage = new String();
Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);
if (item == mItemSwitchCamera) {
mOpenCvCameraView.setVisibility(SurfaceView.GONE);
mIsJavaCamera = !mIsJavaCamera;
if (mIsJavaCamera) {
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_java_surface_view);
toastMesage = "Java Camera";
} else {
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_native_surface_view);
toastMesage = "Native Camera";
}
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
mOpenCvCameraView.enableView();
Toast toast = Toast.makeText(this, toastMesage, Toast.LENGTH_LONG);
toast.show();
} else if (item == mItemPreviewA){
viewMode = VIEW_MODE_A;
} else if (item == mItemPreviewB){
viewMode = VIEW_MODE_B;
} else if (item == mItemPreviewC){
viewMode = VIEW_MODE_C;
} else if (item == mItemPreviewD){
viewMode = VIEW_MODE_D;
} else if (item == mItemPreviewE){
viewMode = VIEW_MODE_E;
} else if (item == mItemPreviewF){
viewMode = VIEW_MODE_F;
} else if (item == mItemPreviewG){
viewMode = VIEW_MODE_G;
} else if (item == mItemPreviewH){
viewMode = VIEW_MODE_H;
} else if (item == mItemPreviewI){
viewMode = VIEW_MODE_I;
} else if (item == mItemPreviewJ){
viewMode = VIEW_MODE_J;
}
return true;
}
public void onCameraViewStarted(int width, int height) {
mRgba = new Mat(height, width, CvType.CV_8UC3);
mHSV = new Mat();
mIntermediateMat = new Mat(height, width, CvType.CV_8UC3);
mGray = new Mat(height, width, CvType.CV_8UC1);
mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
}
public void onCameraViewStopped() {
// Explicitly deallocate Mats
if (mIntermediateMat != null)
mIntermediateMat.release();
mIntermediateMat = null;
if(mHSV!= null)
mHSV.release();
mHSV = null;
if(mGray!= null)
mGray.release();
mGray = null;
if (mIntermediateMat != null)
mIntermediateMat.release();
mIntermediateMat = null;
if (mBitmap != null) {
mBitmap.recycle();
}
}
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
switch (MainActivity.viewMode) {
case MainActivity.VIEW_MODE_A:
homogeneousBlurTutorial();
break;
case MainActivity.VIEW_MODE_B:
gaussianBlurTutorial();
break;
case MainActivity.VIEW_MODE_C:
medianBlurTutorial();
break;
case MainActivity.VIEW_MODE_D:
bilateralFilterTutorial();
break;
case MainActivity.VIEW_MODE_E:
erosionTutorial(0);
break;
case MainActivity.VIEW_MODE_F:
dilationTutorial(0);
break;
case MainActivity.VIEW_MODE_G:
morphologyOperations(Imgproc.MORPH_GRADIENT, Imgproc.MORPH_RECT);
break;
case MainActivity.VIEW_MODE_H:
findContoursTutorial();
break;
case MainActivity.VIEW_MODE_I:
rotationTutorial();
break;
case MainActivity.VIEW_MODE_J:
rotationAffineTutorial();
break;
}
return mRgba;
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// Fill and get the mask
//Point seed = UtilsOpenCV.getImageCoordinates(getWindowManager(), mRgba, arg1.getX(), arg1.getY());
//Log.d(TAG, "seed: "+seed +" type: "+CvType.typeToString(mRgba.type()));
return true;
}
private void homogeneousBlurTutorial(){
Mat ROI = getProcessingROI(mRgba);
/// Applying Homogeneous blur
if( GLOBAL_ITER < MAX_KERNEL_LENGTH ){
Imgproc.blur( ROI, ROI, new Size( GLOBAL_ITER, GLOBAL_ITER ), new Point(-1,-1) );
GLOBAL_ITER = GLOBAL_ITER + 2;
} else {
GLOBAL_ITER = 1;
}
}
private void gaussianBlurTutorial(){
Mat ROI = getProcessingROI(mRgba);
if( GLOBAL_ITER < MAX_KERNEL_LENGTH){
Imgproc.GaussianBlur( ROI, ROI, new Size( GLOBAL_ITER, GLOBAL_ITER ), 0, 0 );
GLOBAL_ITER = GLOBAL_ITER + 2;
} else {
GLOBAL_ITER = 1;
}
}
private void medianBlurTutorial(){
Mat ROI = getProcessingROI(mRgba);
if( GLOBAL_ITER < MAX_KERNEL_LENGTH){
Imgproc.medianBlur( ROI, ROI, GLOBAL_ITER );
GLOBAL_ITER = GLOBAL_ITER + 2;
} else {
GLOBAL_ITER = 1;
}
}
private void bilateralFilterTutorial(){
// bilateralFilter expects 3 channels, mRgba originaly has 4.
Imgproc.cvtColor(mRgba, mRgba, Imgproc.COLOR_RGBA2RGB);
Mat ROI = getProcessingROI(mRgba);
Mat outputROI = getProcessingROI(mIntermediateMat);
if( GLOBAL_ITER < MAX_KERNEL_LENGTH){
Imgproc.bilateralFilter( ROI, outputROI, GLOBAL_ITER, GLOBAL_ITER*2, GLOBAL_ITER/2);
GLOBAL_ITER = GLOBAL_ITER + 2;
} else {
GLOBAL_ITER = 1;
}
Imgproc.cvtColor(outputROI, ROI, Imgproc.COLOR_RGB2RGBA);
}
private void erosionTutorial(int erosion_elem){
int erosion_type = Imgproc.MORPH_RECT;
if( erosion_elem == 0 ){ erosion_type = Imgproc.MORPH_RECT; }
else if( erosion_elem == 1 ){ erosion_type = Imgproc.MORPH_CROSS; }
else if( erosion_elem == 2) { erosion_type = Imgproc.MORPH_ELLIPSE; }
int erosion_size = 1;
Mat element = Imgproc.getStructuringElement( erosion_type,
new Size( 2*erosion_size + 1, 2*erosion_size+1 ),
new Point( erosion_size, erosion_size ) );
Mat ROI = getProcessingROI(mRgba);
Mat grayROI = getProcessingROI(mGray);
Imgproc.cvtColor(ROI, grayROI, Imgproc.COLOR_RGBA2GRAY);
// Morphology operations expect a binary image:
Imgproc.threshold(grayROI, grayROI, 100, 255, Imgproc.THRESH_OTSU);
// Apply the erosion operation
Imgproc.erode( grayROI, grayROI, element );
Imgproc.cvtColor(grayROI, ROI, Imgproc.COLOR_GRAY2RGBA);
}
private void dilationTutorial(int dilation_elem){
int dilation_type = Imgproc.MORPH_RECT;
if( dilation_elem == 0 ){ dilation_type = Imgproc.MORPH_RECT; }
else if( dilation_elem == 1 ){ dilation_type = Imgproc.MORPH_CROSS; }
else if( dilation_elem == 2) { dilation_type = Imgproc.MORPH_ELLIPSE; }
int dilation_size = 1;
Mat element = Imgproc.getStructuringElement( dilation_type,
new Size( 2*dilation_size + 1, 2*dilation_size+1 ),
new Point( dilation_size, dilation_size ) );
Mat ROI = getProcessingROI(mRgba);
Mat grayROI = getProcessingROI(mGray);
Imgproc.cvtColor(ROI, grayROI, Imgproc.COLOR_RGBA2GRAY);
// Morphology operations expect a binary image:
Imgproc.threshold(grayROI, grayROI, 100, 255, Imgproc.THRESH_OTSU);
// Apply the dilation operation
Imgproc.dilate( grayROI, grayROI, element );
Imgproc.cvtColor(grayROI, ROI, Imgproc.COLOR_GRAY2RGBA);
}
private void morphologyOperations( int morph_operator, int morph_elem ){
// Since MORPH_X : 2,3,4,5 and 6
//int operation = morph_operator + 2;
int morph_size = 2;
Mat element = Imgproc.getStructuringElement( morph_elem,
new Size( 2*morph_size + 1, 2*morph_size+1 ),
new Point( morph_size, morph_size ) );
Mat ROI = getProcessingROI(mRgba);
Mat grayROI = getProcessingROI(mGray);
Imgproc.cvtColor(ROI, grayROI, Imgproc.COLOR_RGBA2GRAY);
// Morphology operations expect a binary image:
Imgproc.threshold(grayROI, grayROI, 100, 255, Imgproc.THRESH_OTSU);
/// Apply the specified morphology operation
Imgproc.morphologyEx( grayROI, grayROI, morph_operator, element );
Imgproc.cvtColor(grayROI, ROI, Imgproc.COLOR_GRAY2RGBA);
}
private void findContoursTutorial(){
Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGBA2GRAY);
Imgproc.blur(mGray, mGray, new Size(3, 3));
Mat canny_output = new Mat();
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat hierarchy = new Mat();
/// Detect edges using canny
Imgproc.Canny( mGray, canny_output, thresh, thresh*2, 3, false);
/// Find contours
Imgproc.findContours( canny_output, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0) );
/// Draw contours
mIntermediateMat = Mat.zeros( canny_output.size(), CvType.CV_8UC3 );
for(int i = 0; i<contours.size(); i++) {
Scalar color = new Scalar( getRandomUniformInt(0, 255), getRandomUniformInt(0,255), getRandomUniformInt(0,255) );
Imgproc.drawContours( mIntermediateMat, contours, i, color, 2, 8, hierarchy, 0, new Point() );
}
mRgba = mIntermediateMat;
}
/**
* Image is first resized-to-fit the dst Mat and then rotated.
*
*/
private void rotationTutorial(){
double ratio = mRgba.height() / (double) mRgba.width();
int rotatedHeight = mRgba.height();
int rotatedWidth = (int) Math.round(mRgba.height() * ratio);
Imgproc.resize(mRgba, mIntermediateMat, new Size(rotatedHeight, rotatedWidth));
Core.flip(mIntermediateMat.t(), mIntermediateMat, 0);
Mat ROI = mRgba.submat(0, mIntermediateMat.rows(), 0, mIntermediateMat.cols());
mIntermediateMat.copyTo(ROI);
}
/**
* Image is rotated - cropped-to-fit dst Mat.
*
*/
private void rotationAffineTutorial(){
// assuming source image's with and height are a pair value:
int centerX = Math.round(mRgba.width()/2);
int centerY = Math.round(mRgba.height()/2);
Point center = new Point(centerY,centerX);
double angle = 90;
double scale = 1.0;
double ratio = mRgba.height() / (double) mRgba.width();
int rotatedHeight = (int) Math.round(mRgba.height());
int rotatedWidth = (int) Math.round(mRgba.height() * ratio);
Mat mapMatrix = Imgproc.getRotationMatrix2D(center, angle, scale);
Size rotatedSize = new Size(rotatedWidth, rotatedHeight);
mIntermediateMat = new Mat(rotatedSize, mRgba.type());
Imgproc.warpAffine(mRgba, mIntermediateMat, mapMatrix, mIntermediateMat.size(), Imgproc.INTER_LINEAR);
Mat ROI = mRgba.submat(0, mIntermediateMat.rows(), 0, mIntermediateMat.cols());
mIntermediateMat.copyTo(ROI);
}
private Mat getProcessingROI(Mat src){
Size size = src.size();
int rows = (int) size.height;
int cols = (int) size.width;
int left = cols / 8;
int top = rows / 8;
int width = cols * 3 / 4;
int height = rows * 3 / 4;
return src.submat(top, top + height, left, left + width);
}
private int getRandomUniformInt(int min, int max) {
Random r1 = new Random();
return r1.nextInt() * (max - min) + min;
}
}