Added image-manipulations android sample, other samples are updated; added Mat.size method to java API.
parent
3119af1c72
commit
72541721a1
18 changed files with 463 additions and 58 deletions
@ -0,0 +1,8 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<classpath> |
||||
<classpathentry kind="src" path="src"/> |
||||
<classpathentry kind="src" path="gen"/> |
||||
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/> |
||||
<classpathentry kind="src" path="OpenCV-2.3.1_src"/> |
||||
<classpathentry kind="output" path="bin"/> |
||||
</classpath> |
@ -0,0 +1,40 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<projectDescription> |
||||
<name>image-manipulations</name> |
||||
<comment></comment> |
||||
<projects> |
||||
</projects> |
||||
<buildSpec> |
||||
<buildCommand> |
||||
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name> |
||||
<arguments> |
||||
</arguments> |
||||
</buildCommand> |
||||
<buildCommand> |
||||
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name> |
||||
<arguments> |
||||
</arguments> |
||||
</buildCommand> |
||||
<buildCommand> |
||||
<name>org.eclipse.jdt.core.javabuilder</name> |
||||
<arguments> |
||||
</arguments> |
||||
</buildCommand> |
||||
<buildCommand> |
||||
<name>com.android.ide.eclipse.adt.ApkBuilder</name> |
||||
<arguments> |
||||
</arguments> |
||||
</buildCommand> |
||||
</buildSpec> |
||||
<natures> |
||||
<nature>com.android.ide.eclipse.adt.AndroidNature</nature> |
||||
<nature>org.eclipse.jdt.core.javanature</nature> |
||||
</natures> |
||||
<linkedResources> |
||||
<link> |
||||
<name>OpenCV-2.3.1_src</name> |
||||
<type>2</type> |
||||
<locationURI>_android_OpenCV_2_3_1_df28900a/src</locationURI> |
||||
</link> |
||||
</linkedResources> |
||||
</projectDescription> |
@ -0,0 +1,5 @@ |
||||
#Wed Jun 29 04:36:40 MSD 2011 |
||||
eclipse.preferences.version=1 |
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 |
||||
org.eclipse.jdt.core.compiler.compliance=1.5 |
||||
org.eclipse.jdt.core.compiler.source=1.5 |
@ -0,0 +1,31 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" |
||||
package="org.opencv.samples.imagemanipulations" |
||||
android:versionCode="1" |
||||
android:versionName="1.0"> |
||||
|
||||
<supports-screens android:resizeable="true" |
||||
android:smallScreens="true" |
||||
android:normalScreens="true" |
||||
android:largeScreens="true" |
||||
android:anyDensity="true" /> |
||||
|
||||
<application android:label="@string/app_name" android:icon="@drawable/icon"> |
||||
<activity android:name="ImageManipulationsActivity" |
||||
android:label="@string/app_name" |
||||
android:screenOrientation="landscape" |
||||
android:configChanges="keyboardHidden|orientation"> |
||||
<intent-filter> |
||||
<action android:name="android.intent.action.MAIN" /> |
||||
<category android:name="android.intent.category.LAUNCHER" /> |
||||
</intent-filter> |
||||
</activity> |
||||
</application> |
||||
|
||||
<uses-sdk android:minSdkVersion="8" /> |
||||
|
||||
<uses-permission android:name="android.permission.CAMERA"/> |
||||
<uses-feature android:name="android.hardware.camera" /> |
||||
<uses-feature android:name="android.hardware.camera.autofocus" /> |
||||
|
||||
</manifest> |
@ -0,0 +1,3 @@ |
||||
android.library.reference.1=../../../android/build |
||||
# Project target. |
||||
target=android-8 |
After Width: | Height: | Size: 5.6 KiB |
@ -0,0 +1,4 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<resources> |
||||
<string name="app_name">image-manipulations</string> |
||||
</resources> |
@ -0,0 +1,71 @@ |
||||
package org.opencv.samples.imagemanipulations; |
||||
|
||||
import android.app.Activity; |
||||
import android.os.Bundle; |
||||
import android.util.Log; |
||||
import android.view.Menu; |
||||
import android.view.MenuItem; |
||||
import android.view.Window; |
||||
|
||||
public class ImageManipulationsActivity extends Activity { |
||||
private static final String TAG = "Sample::Activity"; |
||||
|
||||
public static final int VIEW_MODE_RGBA = 0; |
||||
public static final int VIEW_MODE_CANNY = 1; |
||||
public static final int VIEW_MODE_SEPIA = 2; |
||||
public static final int VIEW_MODE_SOBEL = 3; |
||||
public static final int VIEW_MODE_BLUR = 4; |
||||
public static final int VIEW_MODE_ZOOM = 5; |
||||
|
||||
private MenuItem mItemPreviewRGBA; |
||||
private MenuItem mItemPreviewCanny; |
||||
private MenuItem mItemPreviewSepia; |
||||
private MenuItem mItemPreviewSobel; |
||||
private MenuItem mItemPreviewBlur; |
||||
private MenuItem mItemPreviewZoom; |
||||
|
||||
public static int viewMode = VIEW_MODE_RGBA; |
||||
|
||||
public ImageManipulationsActivity() { |
||||
Log.i(TAG, "Instantiated new " + this.getClass()); |
||||
} |
||||
|
||||
/** Called when the activity is first created. */ |
||||
@Override |
||||
public void onCreate(Bundle savedInstanceState) { |
||||
Log.i(TAG, "onCreate"); |
||||
super.onCreate(savedInstanceState); |
||||
requestWindowFeature(Window.FEATURE_NO_TITLE); |
||||
setContentView(new ImageManipulationsView(this)); |
||||
} |
||||
|
||||
@Override |
||||
public boolean onCreateOptionsMenu(Menu menu) { |
||||
Log.i(TAG, "onCreateOptionsMenu"); |
||||
mItemPreviewRGBA = menu.add("Preview RGBA"); |
||||
mItemPreviewCanny = menu.add("Canny"); |
||||
mItemPreviewSepia = menu.add("Sepia"); |
||||
mItemPreviewSobel = menu.add("Sobel"); |
||||
mItemPreviewBlur = menu.add("Blur"); |
||||
mItemPreviewZoom = menu.add("Zoom"); |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public boolean onOptionsItemSelected(MenuItem item) { |
||||
Log.i(TAG, "Menu Item selected " + item); |
||||
if (item == mItemPreviewRGBA) |
||||
viewMode = VIEW_MODE_RGBA; |
||||
else if (item == mItemPreviewCanny) |
||||
viewMode = VIEW_MODE_CANNY; |
||||
else if (item == mItemPreviewSepia) |
||||
viewMode = VIEW_MODE_SEPIA; |
||||
else if (item == mItemPreviewSobel) |
||||
viewMode = VIEW_MODE_SOBEL; |
||||
else if (item == mItemPreviewBlur) |
||||
viewMode = VIEW_MODE_BLUR; |
||||
else if (item == mItemPreviewZoom) |
||||
viewMode = VIEW_MODE_ZOOM; |
||||
return true; |
||||
} |
||||
} |
@ -0,0 +1,154 @@ |
||||
package org.opencv.samples.imagemanipulations; |
||||
|
||||
import org.opencv.*; |
||||
|
||||
import android.content.Context; |
||||
import android.graphics.Bitmap; |
||||
import android.view.SurfaceHolder; |
||||
|
||||
class ImageManipulationsView extends SampleCvViewBase { |
||||
private Mat mRgba; |
||||
private Mat mGray; |
||||
private Mat mIntermediateMat; |
||||
|
||||
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); |
||||
} |
||||
|
||||
@Override |
||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) { |
||||
super.surfaceChanged(_holder, format, width, height); |
||||
|
||||
synchronized (this) { |
||||
// initialize Mats before usage
|
||||
mGray = new Mat(); |
||||
mRgba = new Mat(); |
||||
mIntermediateMat = new Mat(); |
||||
} |
||||
} |
||||
|
||||
private void CreateAuxiliaryMats() { |
||||
if (mRgba.empty()) |
||||
return; |
||||
|
||||
int rows = mRgba.rows(); |
||||
int cols = mRgba.cols(); |
||||
|
||||
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); |
||||
|
||||
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_CANNY: |
||||
capture.retrieve(mRgba, highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); |
||||
capture.retrieve(mGray, highgui.CV_CAP_ANDROID_GREY_FRAME); |
||||
|
||||
if (mRgbaInnerWindow == null || mGrayInnerWindow == null) |
||||
CreateAuxiliaryMats(); |
||||
|
||||
imgproc.Canny(mGrayInnerWindow, mGrayInnerWindow, 80, 90); |
||||
imgproc.cvtColor(mGrayInnerWindow, mRgbaInnerWindow, imgproc.CV_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) |
||||
CreateAuxiliaryMats(); |
||||
|
||||
imgproc.Sobel(mGrayInnerWindow, mIntermediateMat, CvType.CV_8U, 1, 1); |
||||
core.convertScaleAbs(mIntermediateMat, mIntermediateMat, 10); |
||||
imgproc.cvtColor(mIntermediateMat, mRgbaInnerWindow, imgproc.CV_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_BLUR: |
||||
capture.retrieve(mRgba, highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); |
||||
if (mBlurWindow == null) |
||||
CreateAuxiliaryMats(); |
||||
imgproc.blur(mBlurWindow, mBlurWindow, new Size(15, 15)); |
||||
break; |
||||
|
||||
case ImageManipulationsActivity.VIEW_MODE_ZOOM: |
||||
capture.retrieve(mRgba, highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); |
||||
if (mZoomCorner == null || mZoomWindow == null) |
||||
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; |
||||
} |
||||
|
||||
Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888); |
||||
|
||||
if (android.MatToBitmap(mRgba, bmp)) |
||||
return bmp; |
||||
|
||||
bmp.recycle(); |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public void run() { |
||||
super.run(); |
||||
|
||||
synchronized (this) { |
||||
// Explicitly deallocate Mats
|
||||
if (mRgba != null) |
||||
mRgba.dispose(); |
||||
if (mGray != null) |
||||
mGray.dispose(); |
||||
if (mIntermediateMat != null) |
||||
mIntermediateMat.dispose(); |
||||
|
||||
mRgba = null; |
||||
mGray = null; |
||||
mIntermediateMat = null; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,108 @@ |
||||
package org.opencv.samples.imagemanipulations; |
||||
|
||||
import java.util.List; |
||||
|
||||
import org.opencv.*; |
||||
|
||||
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 = "Sample::SurfaceView"; |
||||
|
||||
private SurfaceHolder mHolder; |
||||
private VideoCapture mCamera; |
||||
|
||||
public SampleCvViewBase(Context context) { |
||||
super(context); |
||||
mHolder = getHolder(); |
||||
mHolder.addCallback(this); |
||||
Log.i(TAG, "Instantiated new " + this.getClass()); |
||||
} |
||||
|
||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) { |
||||
Log.i(TAG, "surfaceCreated"); |
||||
synchronized (this) { |
||||
if (mCamera != null && mCamera.isOpened()) { |
||||
Log.i(TAG, "before mCamera.getSupportedPreviewSizes()"); |
||||
List<Size> sizes = mCamera.getSupportedPreviewSizes(); |
||||
Log.i(TAG, "after 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 surfaceCreated(SurfaceHolder holder) { |
||||
Log.i(TAG, "surfaceCreated"); |
||||
mCamera = new VideoCapture(highgui.CV_CAP_ANDROID); |
||||
if (mCamera.isOpened()) { |
||||
(new Thread(this)).start(); |
||||
} else { |
||||
mCamera.release(); |
||||
mCamera = null; |
||||
Log.e(TAG, "Failed to open native camera"); |
||||
} |
||||
} |
||||
|
||||
public void surfaceDestroyed(SurfaceHolder holder) { |
||||
Log.i(TAG, "surfaceDestroyed"); |
||||
if (mCamera != null) { |
||||
synchronized (this) { |
||||
mCamera.release(); |
||||
mCamera = null; |
||||
} |
||||
} |
||||
} |
||||
|
||||
protected abstract Bitmap processFrame(VideoCapture capture); |
||||
|
||||
public void run() { |
||||
Log.i(TAG, "Starting processing thread"); |
||||
while (true) { |
||||
Bitmap bmp = null; |
||||
|
||||
synchronized (this) { |
||||
if (mCamera == null) |
||||
break; |
||||
|
||||
if (!mCamera.grab()) { |
||||
Log.e(TAG, "mCamera.grab() failed"); |
||||
break; |
||||
} |
||||
|
||||
bmp = processFrame(mCamera); |
||||
} |
||||
|
||||
if (bmp != null) { |
||||
Canvas canvas = mHolder.lockCanvas(); |
||||
if (canvas != null) { |
||||
canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2, (canvas.getHeight() - bmp.getHeight()) / 2, null); |
||||
mHolder.unlockCanvasAndPost(canvas); |
||||
} |
||||
bmp.recycle(); |
||||
} |
||||
} |
||||
|
||||
Log.i(TAG, "Finishing processing thread"); |
||||
} |
||||
} |
Loading…
Reference in new issue