Image manipulations sample ported on new framework.
This commit is contained in:
parent
898320a0f6
commit
a5ee07d340
@ -0,0 +1,11 @@
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" >
|
||||
|
||||
<org.opencv.framework.OpenCvJavaCameraView
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
android:id="@+id/image_manipulations_activity_surface_view" />
|
||||
|
||||
</LinearLayout>
|
@ -1,12 +1,23 @@
|
||||
package org.opencv.samples.imagemanipulations;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.opencv.android.BaseLoaderCallback;
|
||||
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.MatOfFloat;
|
||||
import org.opencv.core.MatOfInt;
|
||||
import org.opencv.core.Point;
|
||||
import org.opencv.core.Scalar;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.framework.OpenCvJavaCameraView;
|
||||
import org.opencv.framework.OpenCvCameraBridgeViewBase.CvCameraViewListener;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
@ -14,7 +25,7 @@ import android.view.MenuItem;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
public class ImageManipulationsActivity extends Activity {
|
||||
public class ImageManipulationsActivity extends Activity implements CvCameraViewListener {
|
||||
private static final String TAG = "OCVSample::Activity";
|
||||
|
||||
public static final int VIEW_MODE_RGBA = 0;
|
||||
@ -34,50 +45,44 @@ public class ImageManipulationsActivity extends Activity {
|
||||
private MenuItem mItemPreviewZoom;
|
||||
private MenuItem mItemPreviewPixelize;
|
||||
private MenuItem mItemPreviewPosterize;
|
||||
private ImageManipulationsView mView;
|
||||
private OpenCvJavaCameraView mOpenCvCameraView;
|
||||
|
||||
private Size mSize0;
|
||||
private Size mSizeRgba;
|
||||
private Size mSizeRgbaInner;
|
||||
|
||||
private Mat mRgba;
|
||||
private Mat mGray;
|
||||
private Mat mIntermediateMat;
|
||||
private Mat mHist;
|
||||
private Mat mMat0;
|
||||
private MatOfInt mChannels[];
|
||||
private MatOfInt mHistSize;
|
||||
private int mHistSizeNum;
|
||||
private MatOfFloat mRanges;
|
||||
private Scalar mColorsRGB[];
|
||||
private Scalar mColorsHue[];
|
||||
private Scalar mWhilte;
|
||||
private Point mP1;
|
||||
private Point mP2;
|
||||
private float mBuff[];
|
||||
private Mat mRgbaInnerWindow;
|
||||
private Mat mGrayInnerWindow;
|
||||
private Mat mBlurWindow;
|
||||
private Mat mZoomWindow;
|
||||
private Mat mZoomCorner;
|
||||
private Mat mSepiaKernel;
|
||||
|
||||
public static int viewMode = VIEW_MODE_RGBA;
|
||||
|
||||
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
|
||||
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
|
||||
@Override
|
||||
public void onManagerConnected(int status) {
|
||||
switch (status) {
|
||||
case LoaderCallbackInterface.SUCCESS:
|
||||
{
|
||||
Log.i(TAG, "OpenCV loaded successfully");
|
||||
// Create and set View
|
||||
mView = new ImageManipulationsView(mAppContext);
|
||||
setContentView(mView);
|
||||
|
||||
// Check native OpenCV camera
|
||||
if( !mView.openCamera() ) {
|
||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||
ad.setCancelable(false); // This blocks the 'BACK' button
|
||||
ad.setMessage("Fatal error: can't open camera!");
|
||||
ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
} break;
|
||||
|
||||
/** OpenCV loader cannot start Google Play **/
|
||||
case LoaderCallbackInterface.MARKET_ERROR:
|
||||
{
|
||||
Log.d(TAG, "Google Play service is not accessible!");
|
||||
AlertDialog MarketErrorMessage = new AlertDialog.Builder(mAppContext).create();
|
||||
MarketErrorMessage.setTitle("OpenCV Manager");
|
||||
MarketErrorMessage.setMessage("Google Play service is not accessible!\nTry to install the 'OpenCV Manager' and the appropriate 'OpenCV binary pack' APKs from OpenCV SDK manually via 'adb install' command.");
|
||||
MarketErrorMessage.setCancelable(false); // This blocks the 'BACK' button
|
||||
MarketErrorMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
finish();
|
||||
}
|
||||
});
|
||||
MarketErrorMessage.show();
|
||||
mOpenCvCameraView.enableView();
|
||||
} break;
|
||||
default:
|
||||
{
|
||||
@ -91,25 +96,6 @@ public class ImageManipulationsActivity extends Activity {
|
||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
Log.i(TAG, "called onPause");
|
||||
if (null != mView)
|
||||
mView.releaseCamera();
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
Log.i(TAG, "called onResume");
|
||||
super.onResume();
|
||||
|
||||
Log.i(TAG, "Trying to load OpenCV library");
|
||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack)) {
|
||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||
}
|
||||
}
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
@ -117,6 +103,30 @@ public class ImageManipulationsActivity extends Activity {
|
||||
super.onCreate(savedInstanceState);
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
|
||||
setContentView(R.layout.image_manipulations_surface_view);
|
||||
|
||||
mOpenCvCameraView = (OpenCvJavaCameraView)findViewById(R.id.image_manipulations_activity_surface_view);
|
||||
mOpenCvCameraView.setCvCameraViewListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause()
|
||||
{
|
||||
mOpenCvCameraView.disableView();
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume()
|
||||
{
|
||||
super.onResume();
|
||||
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mLoaderCallback);
|
||||
}
|
||||
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
mOpenCvCameraView.disableView();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -154,4 +164,202 @@ public class ImageManipulationsActivity extends Activity {
|
||||
viewMode = VIEW_MODE_POSTERIZE;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void onCameraViewStarted(int width, int height) {
|
||||
mGray = new Mat();
|
||||
mRgba = new Mat();
|
||||
mIntermediateMat = new Mat();
|
||||
mSize0 = new Size();
|
||||
mHist = new Mat();
|
||||
mChannels = new MatOfInt[] { new MatOfInt(0), new MatOfInt(1), new MatOfInt(2) };
|
||||
mHistSizeNum = 25;
|
||||
mBuff = new float[mHistSizeNum];
|
||||
mHistSize = new MatOfInt(mHistSizeNum);
|
||||
mRanges = new MatOfFloat(0f, 256f);
|
||||
mMat0 = new Mat();
|
||||
mColorsRGB = new Scalar[] { new Scalar(200, 0, 0, 255), new Scalar(0, 200, 0, 255), new Scalar(0, 0, 200, 255) };
|
||||
mColorsHue = new Scalar[] {
|
||||
new Scalar(255, 0, 0, 255), new Scalar(255, 60, 0, 255), new Scalar(255, 120, 0, 255), new Scalar(255, 180, 0, 255), new Scalar(255, 240, 0, 255),
|
||||
new Scalar(215, 213, 0, 255), new Scalar(150, 255, 0, 255), new Scalar(85, 255, 0, 255), new Scalar(20, 255, 0, 255), new Scalar(0, 255, 30, 255),
|
||||
new Scalar(0, 255, 85, 255), new Scalar(0, 255, 150, 255), new Scalar(0, 255, 215, 255), new Scalar(0, 234, 255, 255), new Scalar(0, 170, 255, 255),
|
||||
new Scalar(0, 120, 255, 255), new Scalar(0, 60, 255, 255), new Scalar(0, 0, 255, 255), new Scalar(64, 0, 255, 255), new Scalar(120, 0, 255, 255),
|
||||
new Scalar(180, 0, 255, 255), new Scalar(255, 0, 255, 255), new Scalar(255, 0, 215, 255), new Scalar(255, 0, 85, 255), new Scalar(255, 0, 0, 255)
|
||||
};
|
||||
mWhilte = Scalar.all(255);
|
||||
mP1 = new Point();
|
||||
mP2 = new Point();
|
||||
|
||||
// Fill sepia kernel
|
||||
mSepiaKernel = new Mat(4, 4, CvType.CV_32F);
|
||||
mSepiaKernel.put(0, 0, /* R */0.189f, 0.769f, 0.393f, 0f);
|
||||
mSepiaKernel.put(1, 0, /* G */0.168f, 0.686f, 0.349f, 0f);
|
||||
mSepiaKernel.put(2, 0, /* B */0.131f, 0.534f, 0.272f, 0f);
|
||||
mSepiaKernel.put(3, 0, /* A */0.000f, 0.000f, 0.000f, 1f);
|
||||
}
|
||||
|
||||
private void CreateAuxiliaryMats() {
|
||||
if (mRgba.empty())
|
||||
return;
|
||||
|
||||
mSizeRgba = mRgba.size();
|
||||
|
||||
int rows = (int) mSizeRgba.height;
|
||||
int cols = (int) mSizeRgba.width;
|
||||
|
||||
int left = cols / 8;
|
||||
int top = rows / 8;
|
||||
|
||||
int width = cols * 3 / 4;
|
||||
int height = rows * 3 / 4;
|
||||
|
||||
if (mRgbaInnerWindow == null)
|
||||
mRgbaInnerWindow = mRgba.submat(top, top + height, left, left + width);
|
||||
mSizeRgbaInner = mRgbaInnerWindow.size();
|
||||
|
||||
if (mGrayInnerWindow == null && !mGray.empty())
|
||||
mGrayInnerWindow = mGray.submat(top, top + height, left, left + width);
|
||||
|
||||
if (mBlurWindow == null)
|
||||
mBlurWindow = mRgba.submat(0, rows, cols / 3, cols * 2 / 3);
|
||||
|
||||
if (mZoomCorner == null)
|
||||
mZoomCorner = mRgba.submat(0, rows / 2 - rows / 10, 0, cols / 2 - cols / 10);
|
||||
|
||||
if (mZoomWindow == null)
|
||||
mZoomWindow = mRgba.submat(rows / 2 - 9 * rows / 100, rows / 2 + 9 * rows / 100, cols / 2 - 9 * cols / 100, cols / 2 + 9 * cols / 100);
|
||||
}
|
||||
|
||||
public void onCameraViewStopped() {
|
||||
// Explicitly deallocate Mats
|
||||
if (mZoomWindow != null)
|
||||
mZoomWindow.release();
|
||||
if (mZoomCorner != null)
|
||||
mZoomCorner.release();
|
||||
if (mBlurWindow != null)
|
||||
mBlurWindow.release();
|
||||
if (mGrayInnerWindow != null)
|
||||
mGrayInnerWindow.release();
|
||||
if (mRgbaInnerWindow != null)
|
||||
mRgbaInnerWindow.release();
|
||||
if (mRgba != null)
|
||||
mRgba.release();
|
||||
if (mGray != null)
|
||||
mGray.release();
|
||||
if (mIntermediateMat != null)
|
||||
mIntermediateMat.release();
|
||||
|
||||
mRgba = null;
|
||||
mGray = null;
|
||||
mIntermediateMat = null;
|
||||
mRgbaInnerWindow = null;
|
||||
mGrayInnerWindow = null;
|
||||
mBlurWindow = null;
|
||||
mZoomCorner = null;
|
||||
mZoomWindow = null;
|
||||
}
|
||||
|
||||
public Mat onCameraFrame(Mat inputFrame) {
|
||||
|
||||
switch (ImageManipulationsActivity.viewMode) {
|
||||
case ImageManipulationsActivity.VIEW_MODE_RGBA:
|
||||
break;
|
||||
|
||||
case ImageManipulationsActivity.VIEW_MODE_HIST:
|
||||
if ((mSizeRgba == null) || (mRgba.cols() != mSizeRgba.width) || (mRgba.height() != mSizeRgba.height))
|
||||
CreateAuxiliaryMats();
|
||||
int thikness = (int) (mSizeRgba.width / (mHistSizeNum + 10) / 5);
|
||||
if(thikness > 5) thikness = 5;
|
||||
int offset = (int) ((mSizeRgba.width - (5*mHistSizeNum + 4*10)*thikness)/2);
|
||||
// RGB
|
||||
for(int c=0; c<3; c++) {
|
||||
Imgproc.calcHist(Arrays.asList(mRgba), mChannels[c], mMat0, mHist, mHistSize, mRanges);
|
||||
Core.normalize(mHist, mHist, mSizeRgba.height/2, 0, Core.NORM_INF);
|
||||
mHist.get(0, 0, mBuff);
|
||||
for(int h=0; h<mHistSizeNum; h++) {
|
||||
mP1.x = mP2.x = offset + (c * (mHistSizeNum + 10) + h) * thikness;
|
||||
mP1.y = mSizeRgba.height-1;
|
||||
mP2.y = mP1.y - 2 - (int)mBuff[h];
|
||||
Core.line(mRgba, mP1, mP2, mColorsRGB[c], thikness);
|
||||
}
|
||||
}
|
||||
// Value and Hue
|
||||
Imgproc.cvtColor(mRgba, mIntermediateMat, Imgproc.COLOR_RGB2HSV_FULL);
|
||||
// Value
|
||||
Imgproc.calcHist(Arrays.asList(mIntermediateMat), mChannels[2], mMat0, mHist, mHistSize, mRanges);
|
||||
Core.normalize(mHist, mHist, mSizeRgba.height/2, 0, Core.NORM_INF);
|
||||
mHist.get(0, 0, mBuff);
|
||||
for(int h=0; h<mHistSizeNum; h++) {
|
||||
mP1.x = mP2.x = offset + (3 * (mHistSizeNum + 10) + h) * thikness;
|
||||
mP1.y = mSizeRgba.height-1;
|
||||
mP2.y = mP1.y - 2 - (int)mBuff[h];
|
||||
Core.line(mRgba, mP1, mP2, mWhilte, thikness);
|
||||
}
|
||||
// Hue
|
||||
Imgproc.calcHist(Arrays.asList(mIntermediateMat), mChannels[0], mMat0, mHist, mHistSize, mRanges);
|
||||
Core.normalize(mHist, mHist, mSizeRgba.height/2, 0, Core.NORM_INF);
|
||||
mHist.get(0, 0, mBuff);
|
||||
for(int h=0; h<mHistSizeNum; h++) {
|
||||
mP1.x = mP2.x = offset + (4 * (mHistSizeNum + 10) + h) * thikness;
|
||||
mP1.y = mSizeRgba.height-1;
|
||||
mP2.y = mP1.y - 2 - (int)mBuff[h];
|
||||
Core.line(mRgba, mP1, mP2, mColorsHue[h], thikness);
|
||||
}
|
||||
break;
|
||||
|
||||
case ImageManipulationsActivity.VIEW_MODE_CANNY:
|
||||
if ((mRgbaInnerWindow == null) || (mGrayInnerWindow == null) || (mRgba.cols() != mSizeRgba.width) || (mRgba.height() != mSizeRgba.height))
|
||||
CreateAuxiliaryMats();
|
||||
Imgproc.Canny(mRgbaInnerWindow, mIntermediateMat, 80, 90);
|
||||
Imgproc.cvtColor(mIntermediateMat, mRgbaInnerWindow, Imgproc.COLOR_GRAY2BGRA, 4);
|
||||
break;
|
||||
|
||||
case ImageManipulationsActivity.VIEW_MODE_SOBEL:
|
||||
Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGBA2GRAY);
|
||||
|
||||
if ((mRgbaInnerWindow == null) || (mGrayInnerWindow == null) || (mRgba.cols() != mSizeRgba.width) || (mRgba.height() != mSizeRgba.height))
|
||||
CreateAuxiliaryMats();
|
||||
|
||||
Imgproc.Sobel(mGrayInnerWindow, mIntermediateMat, CvType.CV_8U, 1, 1);
|
||||
Core.convertScaleAbs(mIntermediateMat, mIntermediateMat, 10, 0);
|
||||
Imgproc.cvtColor(mIntermediateMat, mRgbaInnerWindow, Imgproc.COLOR_GRAY2BGRA, 4);
|
||||
break;
|
||||
|
||||
case ImageManipulationsActivity.VIEW_MODE_SEPIA:
|
||||
Core.transform(mRgba, mRgba, mSepiaKernel);
|
||||
break;
|
||||
|
||||
case ImageManipulationsActivity.VIEW_MODE_ZOOM:
|
||||
if ((mZoomCorner == null) || (mZoomWindow == null) || (mRgba.cols() != mSizeRgba.width) || (mRgba.height() != mSizeRgba.height))
|
||||
CreateAuxiliaryMats();
|
||||
Imgproc.resize(mZoomWindow, mZoomCorner, mZoomCorner.size());
|
||||
|
||||
Size wsize = mZoomWindow.size();
|
||||
Core.rectangle(mZoomWindow, new Point(1, 1), new Point(wsize.width - 2, wsize.height - 2), new Scalar(255, 0, 0, 255), 2);
|
||||
break;
|
||||
|
||||
case ImageManipulationsActivity.VIEW_MODE_PIXELIZE:
|
||||
if ((mRgbaInnerWindow == null) || (mRgba.cols() != mSizeRgba.width) || (mRgba.height() != mSizeRgba.height))
|
||||
CreateAuxiliaryMats();
|
||||
Imgproc.resize(mRgbaInnerWindow, mIntermediateMat, mSize0, 0.1, 0.1, Imgproc.INTER_NEAREST);
|
||||
Imgproc.resize(mIntermediateMat, mRgbaInnerWindow, mSizeRgbaInner, 0., 0., Imgproc.INTER_NEAREST);
|
||||
break;
|
||||
|
||||
case ImageManipulationsActivity.VIEW_MODE_POSTERIZE:
|
||||
if ((mRgbaInnerWindow == null) || (mRgba.cols() != mSizeRgba.width) || (mRgba.height() != mSizeRgba.height))
|
||||
CreateAuxiliaryMats();
|
||||
/*
|
||||
Imgproc.cvtColor(mRgbaInnerWindow, mIntermediateMat, Imgproc.COLOR_RGBA2RGB);
|
||||
Imgproc.pyrMeanShiftFiltering(mIntermediateMat, mIntermediateMat, 5, 50);
|
||||
Imgproc.cvtColor(mIntermediateMat, mRgbaInnerWindow, Imgproc.COLOR_RGB2RGBA);
|
||||
*/
|
||||
|
||||
Imgproc.Canny(mRgbaInnerWindow, mIntermediateMat, 80, 90);
|
||||
mRgbaInnerWindow.setTo(new Scalar(0, 0, 0, 255), mIntermediateMat);
|
||||
Core.convertScaleAbs(mRgbaInnerWindow, mIntermediateMat, 1./16, 0);
|
||||
Core.convertScaleAbs(mIntermediateMat, mRgbaInnerWindow, 16, 0);
|
||||
break;
|
||||
}
|
||||
|
||||
return mRgba;
|
||||
}
|
||||
}
|
||||
|
@ -1,283 +0,0 @@
|
||||
package org.opencv.samples.imagemanipulations;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.opencv.android.Utils;
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.MatOfFloat;
|
||||
import org.opencv.core.MatOfInt;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.core.Point;
|
||||
import org.opencv.core.Scalar;
|
||||
import org.opencv.core.CvType;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
import org.opencv.highgui.Highgui;
|
||||
import org.opencv.highgui.VideoCapture;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.util.Log;
|
||||
import android.view.SurfaceHolder;
|
||||
|
||||
class ImageManipulationsView extends SampleCvViewBase {
|
||||
private static final String TAG = "OCVSample::View";
|
||||
private Size mSize0;
|
||||
private Size mSizeRgba;
|
||||
private Size mSizeRgbaInner;
|
||||
|
||||
private Mat mRgba;
|
||||
private Mat mGray;
|
||||
private Mat mIntermediateMat;
|
||||
private Mat mHist;
|
||||
private Mat mMat0;
|
||||
private MatOfInt mChannels[];
|
||||
private MatOfInt mHistSize;
|
||||
private int mHistSizeNum;
|
||||
private MatOfFloat mRanges;
|
||||
private Scalar mColorsRGB[];
|
||||
private Scalar mColorsHue[];
|
||||
private Scalar mWhilte;
|
||||
private Point mP1;
|
||||
private Point mP2;
|
||||
private float mBuff[];
|
||||
private Mat mRgbaInnerWindow;
|
||||
private Mat mGrayInnerWindow;
|
||||
private Mat mBlurWindow;
|
||||
private Mat mZoomWindow;
|
||||
private Mat mZoomCorner;
|
||||
private Mat mSepiaKernel;
|
||||
|
||||
public ImageManipulationsView(Context context) {
|
||||
super(context);
|
||||
|
||||
mSepiaKernel = new Mat(4, 4, CvType.CV_32F);
|
||||
mSepiaKernel.put(0, 0, /* R */0.189f, 0.769f, 0.393f, 0f);
|
||||
mSepiaKernel.put(1, 0, /* G */0.168f, 0.686f, 0.349f, 0f);
|
||||
mSepiaKernel.put(2, 0, /* B */0.131f, 0.534f, 0.272f, 0f);
|
||||
mSepiaKernel.put(3, 0, /* A */0.000f, 0.000f, 0.000f, 1f);
|
||||
|
||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
Log.i(TAG, "called surfaceCreated");
|
||||
synchronized (this) {
|
||||
// initialize Mats before usage
|
||||
mGray = new Mat();
|
||||
mRgba = new Mat();
|
||||
mIntermediateMat = new Mat();
|
||||
mSize0 = new Size();
|
||||
mHist = new Mat();
|
||||
mChannels = new MatOfInt[] { new MatOfInt(0), new MatOfInt(1), new MatOfInt(2) };
|
||||
mHistSizeNum = 25;
|
||||
mBuff = new float[mHistSizeNum];
|
||||
mHistSize = new MatOfInt(mHistSizeNum);
|
||||
mRanges = new MatOfFloat(0f, 256f);
|
||||
mMat0 = new Mat();
|
||||
mColorsRGB = new Scalar[] { new Scalar(200, 0, 0, 255), new Scalar(0, 200, 0, 255), new Scalar(0, 0, 200, 255) };
|
||||
mColorsHue = new Scalar[] {
|
||||
new Scalar(255, 0, 0, 255), new Scalar(255, 60, 0, 255), new Scalar(255, 120, 0, 255), new Scalar(255, 180, 0, 255), new Scalar(255, 240, 0, 255),
|
||||
new Scalar(215, 213, 0, 255), new Scalar(150, 255, 0, 255), new Scalar(85, 255, 0, 255), new Scalar(20, 255, 0, 255), new Scalar(0, 255, 30, 255),
|
||||
new Scalar(0, 255, 85, 255), new Scalar(0, 255, 150, 255), new Scalar(0, 255, 215, 255), new Scalar(0, 234, 255, 255), new Scalar(0, 170, 255, 255),
|
||||
new Scalar(0, 120, 255, 255), new Scalar(0, 60, 255, 255), new Scalar(0, 0, 255, 255), new Scalar(64, 0, 255, 255), new Scalar(120, 0, 255, 255),
|
||||
new Scalar(180, 0, 255, 255), new Scalar(255, 0, 255, 255), new Scalar(255, 0, 215, 255), new Scalar(255, 0, 85, 255), new Scalar(255, 0, 0, 255)
|
||||
};
|
||||
mWhilte = Scalar.all(255);
|
||||
mP1 = new Point();
|
||||
mP2 = new Point();
|
||||
}
|
||||
|
||||
super.surfaceCreated(holder);
|
||||
}
|
||||
|
||||
private void CreateAuxiliaryMats() {
|
||||
if (mRgba.empty())
|
||||
return;
|
||||
|
||||
mSizeRgba = mRgba.size();
|
||||
|
||||
int rows = (int) mSizeRgba.height;
|
||||
int cols = (int) mSizeRgba.width;
|
||||
|
||||
int left = cols / 8;
|
||||
int top = rows / 8;
|
||||
|
||||
int width = cols * 3 / 4;
|
||||
int height = rows * 3 / 4;
|
||||
|
||||
if (mRgbaInnerWindow == null)
|
||||
mRgbaInnerWindow = mRgba.submat(top, top + height, left, left + width);
|
||||
mSizeRgbaInner = mRgbaInnerWindow.size();
|
||||
|
||||
if (mGrayInnerWindow == null && !mGray.empty())
|
||||
mGrayInnerWindow = mGray.submat(top, top + height, left, left + width);
|
||||
|
||||
if (mBlurWindow == null)
|
||||
mBlurWindow = mRgba.submat(0, rows, cols / 3, cols * 2 / 3);
|
||||
|
||||
if (mZoomCorner == null)
|
||||
mZoomCorner = mRgba.submat(0, rows / 2 - rows / 10, 0, cols / 2 - cols / 10);
|
||||
|
||||
if (mZoomWindow == null)
|
||||
mZoomWindow = mRgba.submat(rows / 2 - 9 * rows / 100, rows / 2 + 9 * rows / 100, cols / 2 - 9 * cols / 100, cols / 2 + 9 * cols / 100);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Bitmap processFrame(VideoCapture capture) {
|
||||
switch (ImageManipulationsActivity.viewMode) {
|
||||
|
||||
case ImageManipulationsActivity.VIEW_MODE_RGBA:
|
||||
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
|
||||
break;
|
||||
|
||||
case ImageManipulationsActivity.VIEW_MODE_HIST:
|
||||
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
|
||||
if ((mSizeRgba == null) || (mRgba.cols() != mSizeRgba.width) || (mRgba.height() != mSizeRgba.height))
|
||||
CreateAuxiliaryMats();
|
||||
int thikness = (int) (mSizeRgba.width / (mHistSizeNum + 10) / 5);
|
||||
if(thikness > 5) thikness = 5;
|
||||
int offset = (int) ((mSizeRgba.width - (5*mHistSizeNum + 4*10)*thikness)/2);
|
||||
// RGB
|
||||
for(int c=0; c<3; c++) {
|
||||
Imgproc.calcHist(Arrays.asList(mRgba), mChannels[c], mMat0, mHist, mHistSize, mRanges);
|
||||
Core.normalize(mHist, mHist, mSizeRgba.height/2, 0, Core.NORM_INF);
|
||||
mHist.get(0, 0, mBuff);
|
||||
for(int h=0; h<mHistSizeNum; h++) {
|
||||
mP1.x = mP2.x = offset + (c * (mHistSizeNum + 10) + h) * thikness;
|
||||
mP1.y = mSizeRgba.height-1;
|
||||
mP2.y = mP1.y - 2 - (int)mBuff[h];
|
||||
Core.line(mRgba, mP1, mP2, mColorsRGB[c], thikness);
|
||||
}
|
||||
}
|
||||
// Value and Hue
|
||||
Imgproc.cvtColor(mRgba, mIntermediateMat, Imgproc.COLOR_RGB2HSV_FULL);
|
||||
// Value
|
||||
Imgproc.calcHist(Arrays.asList(mIntermediateMat), mChannels[2], mMat0, mHist, mHistSize, mRanges);
|
||||
Core.normalize(mHist, mHist, mSizeRgba.height/2, 0, Core.NORM_INF);
|
||||
mHist.get(0, 0, mBuff);
|
||||
for(int h=0; h<mHistSizeNum; h++) {
|
||||
mP1.x = mP2.x = offset + (3 * (mHistSizeNum + 10) + h) * thikness;
|
||||
mP1.y = mSizeRgba.height-1;
|
||||
mP2.y = mP1.y - 2 - (int)mBuff[h];
|
||||
Core.line(mRgba, mP1, mP2, mWhilte, thikness);
|
||||
}
|
||||
// Hue
|
||||
Imgproc.calcHist(Arrays.asList(mIntermediateMat), mChannels[0], mMat0, mHist, mHistSize, mRanges);
|
||||
Core.normalize(mHist, mHist, mSizeRgba.height/2, 0, Core.NORM_INF);
|
||||
mHist.get(0, 0, mBuff);
|
||||
for(int h=0; h<mHistSizeNum; h++) {
|
||||
mP1.x = mP2.x = offset + (4 * (mHistSizeNum + 10) + h) * thikness;
|
||||
mP1.y = mSizeRgba.height-1;
|
||||
mP2.y = mP1.y - 2 - (int)mBuff[h];
|
||||
Core.line(mRgba, mP1, mP2, mColorsHue[h], thikness);
|
||||
}
|
||||
break;
|
||||
|
||||
case ImageManipulationsActivity.VIEW_MODE_CANNY:
|
||||
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
|
||||
|
||||
if ((mRgbaInnerWindow == null) || (mGrayInnerWindow == null) || (mRgba.cols() != mSizeRgba.width) || (mRgba.height() != mSizeRgba.height))
|
||||
CreateAuxiliaryMats();
|
||||
Imgproc.Canny(mRgbaInnerWindow, mIntermediateMat, 80, 90);
|
||||
Imgproc.cvtColor(mIntermediateMat, mRgbaInnerWindow, Imgproc.COLOR_GRAY2BGRA, 4);
|
||||
break;
|
||||
|
||||
case ImageManipulationsActivity.VIEW_MODE_SOBEL:
|
||||
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
|
||||
capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME);
|
||||
|
||||
if ((mRgbaInnerWindow == null) || (mGrayInnerWindow == null) || (mRgba.cols() != mSizeRgba.width) || (mRgba.height() != mSizeRgba.height))
|
||||
CreateAuxiliaryMats();
|
||||
|
||||
Imgproc.Sobel(mGrayInnerWindow, mIntermediateMat, CvType.CV_8U, 1, 1);
|
||||
Core.convertScaleAbs(mIntermediateMat, mIntermediateMat, 10, 0);
|
||||
Imgproc.cvtColor(mIntermediateMat, mRgbaInnerWindow, Imgproc.COLOR_GRAY2BGRA, 4);
|
||||
break;
|
||||
|
||||
case ImageManipulationsActivity.VIEW_MODE_SEPIA:
|
||||
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
|
||||
Core.transform(mRgba, mRgba, mSepiaKernel);
|
||||
break;
|
||||
|
||||
case ImageManipulationsActivity.VIEW_MODE_ZOOM:
|
||||
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
|
||||
if ((mZoomCorner == null) || (mZoomWindow == null) || (mRgba.cols() != mSizeRgba.width) || (mRgba.height() != mSizeRgba.height))
|
||||
CreateAuxiliaryMats();
|
||||
Imgproc.resize(mZoomWindow, mZoomCorner, mZoomCorner.size());
|
||||
|
||||
Size wsize = mZoomWindow.size();
|
||||
Core.rectangle(mZoomWindow, new Point(1, 1), new Point(wsize.width - 2, wsize.height - 2), new Scalar(255, 0, 0, 255), 2);
|
||||
break;
|
||||
|
||||
case ImageManipulationsActivity.VIEW_MODE_PIXELIZE:
|
||||
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
|
||||
if ((mRgbaInnerWindow == null) || (mRgba.cols() != mSizeRgba.width) || (mRgba.height() != mSizeRgba.height))
|
||||
CreateAuxiliaryMats();
|
||||
Imgproc.resize(mRgbaInnerWindow, mIntermediateMat, mSize0, 0.1, 0.1, Imgproc.INTER_NEAREST);
|
||||
Imgproc.resize(mIntermediateMat, mRgbaInnerWindow, mSizeRgbaInner, 0., 0., Imgproc.INTER_NEAREST);
|
||||
break;
|
||||
|
||||
case ImageManipulationsActivity.VIEW_MODE_POSTERIZE:
|
||||
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
|
||||
if ((mRgbaInnerWindow == null) || (mRgba.cols() != mSizeRgba.width) || (mRgba.height() != mSizeRgba.height))
|
||||
CreateAuxiliaryMats();
|
||||
/*
|
||||
Imgproc.cvtColor(mRgbaInnerWindow, mIntermediateMat, Imgproc.COLOR_RGBA2RGB);
|
||||
Imgproc.pyrMeanShiftFiltering(mIntermediateMat, mIntermediateMat, 5, 50);
|
||||
Imgproc.cvtColor(mIntermediateMat, mRgbaInnerWindow, Imgproc.COLOR_RGB2RGBA);
|
||||
*/
|
||||
Imgproc.Canny(mRgbaInnerWindow, mIntermediateMat, 80, 90);
|
||||
mRgbaInnerWindow.setTo(new Scalar(0, 0, 0, 255), mIntermediateMat);
|
||||
Core.convertScaleAbs(mRgbaInnerWindow, mIntermediateMat, 1./16, 0);
|
||||
Core.convertScaleAbs(mIntermediateMat, mRgbaInnerWindow, 16, 0);
|
||||
break;
|
||||
}
|
||||
|
||||
Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);
|
||||
|
||||
try {
|
||||
Utils.matToBitmap(mRgba, bmp);
|
||||
return bmp;
|
||||
} catch(Exception e) {
|
||||
Log.e(TAG, "Utils.matToBitmap() throws an exception: " + e.getMessage());
|
||||
bmp.recycle();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
super.run();
|
||||
|
||||
synchronized (this) {
|
||||
// Explicitly deallocate Mats
|
||||
if (mZoomWindow != null)
|
||||
mZoomWindow.release();
|
||||
if (mZoomCorner != null)
|
||||
mZoomCorner.release();
|
||||
if (mBlurWindow != null)
|
||||
mBlurWindow.release();
|
||||
if (mGrayInnerWindow != null)
|
||||
mGrayInnerWindow.release();
|
||||
if (mRgbaInnerWindow != null)
|
||||
mRgbaInnerWindow.release();
|
||||
if (mRgba != null)
|
||||
mRgba.release();
|
||||
if (mGray != null)
|
||||
mGray.release();
|
||||
if (mIntermediateMat != null)
|
||||
mIntermediateMat.release();
|
||||
|
||||
mRgba = null;
|
||||
mGray = null;
|
||||
mIntermediateMat = null;
|
||||
mRgbaInnerWindow = null;
|
||||
mGrayInnerWindow = null;
|
||||
mBlurWindow = null;
|
||||
mZoomCorner = null;
|
||||
mZoomWindow = null;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,124 +0,0 @@
|
||||
package org.opencv.samples.imagemanipulations;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.highgui.VideoCapture;
|
||||
import org.opencv.highgui.Highgui;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.util.Log;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
|
||||
public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
||||
private static final String TAG = "OCVSample::BaseView";
|
||||
|
||||
private SurfaceHolder mHolder;
|
||||
private VideoCapture mCamera;
|
||||
private FpsMeter mFps;
|
||||
|
||||
public SampleCvViewBase(Context context) {
|
||||
super(context);
|
||||
mHolder = getHolder();
|
||||
mHolder.addCallback(this);
|
||||
mFps = new FpsMeter();
|
||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||
}
|
||||
|
||||
public synchronized boolean openCamera() {
|
||||
Log.i(TAG, "Opening Camera");
|
||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
||||
if (!mCamera.isOpened()) {
|
||||
releaseCamera();
|
||||
Log.e(TAG, "Can't open native camera");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public synchronized void releaseCamera() {
|
||||
Log.i(TAG, "Releasing Camera");
|
||||
if (mCamera != null) {
|
||||
mCamera.release();
|
||||
mCamera = null;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void setupCamera(int width, int height) {
|
||||
if (mCamera != null && mCamera.isOpened()) {
|
||||
Log.i(TAG, "Setup Camera - " + width + "x" + height);
|
||||
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
||||
int mFrameWidth = width;
|
||||
int mFrameHeight = height;
|
||||
|
||||
// selecting optimal camera preview size
|
||||
{
|
||||
double minDiff = Double.MAX_VALUE;
|
||||
for (Size size : sizes) {
|
||||
if (Math.abs(size.height - height) < minDiff) {
|
||||
mFrameWidth = (int) size.width;
|
||||
mFrameHeight = (int) size.height;
|
||||
minDiff = Math.abs(size.height - height);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mCamera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, mFrameWidth);
|
||||
mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, mFrameHeight);
|
||||
}
|
||||
}
|
||||
|
||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||
Log.i(TAG, "called surfaceChanged");
|
||||
setupCamera(width, height);
|
||||
}
|
||||
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
Log.i(TAG, "called surfaceCreated");
|
||||
(new Thread(this)).start();
|
||||
}
|
||||
|
||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||
Log.i(TAG, "called surfaceDestroyed");
|
||||
}
|
||||
|
||||
protected abstract Bitmap processFrame(VideoCapture capture);
|
||||
|
||||
public void run() {
|
||||
Log.i(TAG, "Started processing thread");
|
||||
mFps.init();
|
||||
|
||||
while (true) {
|
||||
Bitmap bmp = null;
|
||||
|
||||
synchronized (this) {
|
||||
if (mCamera == null)
|
||||
break;
|
||||
|
||||
if (!mCamera.grab()) {
|
||||
Log.e(TAG, "mCamera.grab() failed");
|
||||
break;
|
||||
}
|
||||
|
||||
bmp = processFrame(mCamera);
|
||||
|
||||
mFps.measure();
|
||||
}
|
||||
|
||||
if (bmp != null) {
|
||||
Canvas canvas = mHolder.lockCanvas();
|
||||
if (canvas != null) {
|
||||
canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
|
||||
canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2, (canvas.getHeight() - bmp.getHeight()), null);
|
||||
mFps.draw(canvas, (canvas.getWidth() - bmp.getWidth()) / 2, 0);
|
||||
mHolder.unlockCanvasAndPost(canvas);
|
||||
}
|
||||
bmp.recycle();
|
||||
}
|
||||
}
|
||||
Log.i(TAG, "Finished processing thread");
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user