From 52f5009a4ca7ac5f876205f3cbce1e7e5e631d88 Mon Sep 17 00:00:00 2001 From: Andrey Pavlenko Date: Fri, 27 Apr 2012 14:40:25 +0000 Subject: [PATCH] adding Histogram and Posterize, removing Blur from 'image manipulation' Java sample --- .../ImageManipulationsActivity.java | 29 +++--- .../ImageManipulationsView.java | 98 +++++++++++++++++-- .../imagemanipulations/SampleCvViewBase.java | 2 +- 3 files changed, 107 insertions(+), 22 deletions(-) diff --git a/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java b/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java index 68d4b7fe80..e5fe800ed3 100644 --- a/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java +++ b/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java @@ -8,23 +8,25 @@ import android.view.MenuItem; import android.view.Window; public class ImageManipulationsActivity extends Activity { - private static final String TAG = "Sample::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_HIST = 1; + public static final int VIEW_MODE_CANNY = 2; + public static final int VIEW_MODE_SEPIA = 3; + public static final int VIEW_MODE_SOBEL = 4; public static final int VIEW_MODE_ZOOM = 5; public static final int VIEW_MODE_PIXELIZE = 6; + public static final int VIEW_MODE_POSTERIZE = 7; private MenuItem mItemPreviewRGBA; + private MenuItem mItemPreviewHist; private MenuItem mItemPreviewCanny; private MenuItem mItemPreviewSepia; private MenuItem mItemPreviewSobel; - private MenuItem mItemPreviewBlur; private MenuItem mItemPreviewZoom; private MenuItem mItemPreviewPixelize; + private MenuItem mItemPreviewPosterize; public static int viewMode = VIEW_MODE_RGBA; @@ -44,13 +46,14 @@ public class ImageManipulationsActivity extends Activity { @Override public boolean onCreateOptionsMenu(Menu menu) { Log.i(TAG, "onCreateOptionsMenu"); - mItemPreviewRGBA = menu.add("Preview RGBA"); + mItemPreviewRGBA = menu.add("Preview RGBA"); + mItemPreviewHist = menu.add("Histograms"); mItemPreviewCanny = menu.add("Canny"); mItemPreviewSepia = menu.add("Sepia"); mItemPreviewSobel = menu.add("Sobel"); - mItemPreviewBlur = menu.add("Blur"); - mItemPreviewZoom = menu.add("Zoom"); - mItemPreviewPixelize = menu.add("Pixelize"); + mItemPreviewZoom = menu.add("Zoom"); + mItemPreviewPixelize = menu.add("Pixelize"); + mItemPreviewPosterize = menu.add("Posterize"); return true; } @@ -59,18 +62,20 @@ public class ImageManipulationsActivity extends Activity { Log.i(TAG, "Menu Item selected " + item); if (item == mItemPreviewRGBA) viewMode = VIEW_MODE_RGBA; + if (item == mItemPreviewHist) + viewMode = VIEW_MODE_HIST; 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; else if (item == mItemPreviewPixelize) viewMode = VIEW_MODE_PIXELIZE; + else if (item == mItemPreviewPosterize) + viewMode = VIEW_MODE_POSTERIZE; return true; } } diff --git a/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsView.java b/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsView.java index f979f1403d..cc44c552f3 100644 --- a/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsView.java +++ b/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsView.java @@ -1,8 +1,12 @@ 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; @@ -18,11 +22,19 @@ import android.view.SurfaceHolder; class ImageManipulationsView extends SampleCvViewBase { private Size mSize0; + private Size mSizeRgba; private Size mSizeRgbaInner; private Mat mRgba; private Mat mGray; private Mat mIntermediateMat; + private Mat mHist, mMat0; + private MatOfInt mChannels[], mHistSize; + private int mHistSizeNum; + private MatOfFloat mRanges; + private Scalar mColorsRGB[], mColorsHue[], mWhilte; + private Point mP1, mP2; + float mBuff[]; private Mat mRgbaInnerWindow; private Mat mGrayInnerWindow; @@ -52,6 +64,24 @@ class ImageManipulationsView extends SampleCvViewBase { 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(); } } @@ -59,8 +89,10 @@ class ImageManipulationsView extends SampleCvViewBase { if (mRgba.empty()) return; - int rows = mRgba.rows(); - int cols = mRgba.cols(); + mSizeRgba = mRgba.size(); + + int rows = (int) mSizeRgba.height; + int cols = (int) mSizeRgba.width; int left = cols / 8; int top = rows / 8; @@ -93,6 +125,49 @@ class ImageManipulationsView extends SampleCvViewBase { 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) + 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