Problems with Imgproc.pyrMeanShiftFiltering

826 views
Skip to first unread message

Andrew Canny

unread,
Aug 17, 2011, 1:03:44 PM8/17/11
to android-opencv
Hello everyone.
I'm trying to make the mean shift segmentation, but the method seems
Imgproc.pyrMeanShiftFiltering not work very well. There I put my code.

Main activity:
---------------------------
package app.android;

import android.app.Activity;
import android.os.Bundle;

public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
}



MyView.java:
----------------------------
package app.android;

import android.content.Context;
import android.view.View;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

import org.opencv.core.Mat;
import org.opencv.core.CvType;
import org.opencv.android.Utils;
import org.opencv.imgproc.Imgproc;


public class MyView extends View {
private Bitmap image_src_Bmp;
private Bitmap image_dst_Bmp;


public MyView(Context context) {
super(context);

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inScaled = true;
opts.inSampleSize = 1;
image_src_Bmp =
BitmapFactory.decodeResource(context.getResources(),
R.drawable.fruits, opts);
}

//the method that draws the image
@Override protected void onDraw(Canvas canvas) {
int widthView = getWidth();
int heightView = getHeight();
Bitmap imageScaled_Bmp = Bitmap.createScaledBitmap(image_src_Bmp,
widthView, heightView, true);
Bitmap imageScaledNew_Bmp =
imageScaled_Bmp.copy(Config.ARGB_8888, true);


Mat image_src_Mat = Utils.bitmapToMat(imageScaledNew_Bmp);

//Segment the image
Mat imagesrcNew_Mat = new Mat(heightView, widthView,
CvType.CV_8UC3);
Mat image_dst_Mat = new Mat(heightView, widthView,
CvType.CV_8UC3);
Imgproc.cvtColor(image_src_Mat, imagesrcNew_Mat,
Imgproc.COLOR_RGBA2RGB);
Imgproc.pyrMeanShiftFiltering(imagesrcNew_Mat, image_dst_Mat, 10,
20, 2);


image_dst_Bmp = Bitmap.createBitmap(widthView, heightView,
Config.ARGB_8888);
Utils.matToBitmap(image_dst_Mat, image_dst_Bmp);

//draw the image on the canvas
canvas.drawBitmap(image_dst_Bmp, 0, 0, null);
}

}


If you test the code with known image fruits.jpeg (the one referred to
in reference opencv) you will find that the result is not the same.
Also, instead of obtaining a color image we get a grayscale replicated
and it takes some time before it is produced.
There does not seem to go something in my code?
Can anyone help me?
Thanks!

Andrey Kamaev

unread,
Aug 17, 2011, 2:34:34 PM8/17/11
to android...@googlegroups.com
Hi,

The following code works for me:

Mat fruits = null;
try {
    fruits = Utils.loadResource(context, R.drawable.fruits, Highgui.CV_LOAD_IMAGE_COLOR);
} catch (IOException e) {
    return;
}
Mat resized = new Mat();
Imgproc.resize(fruits, resized, new Size(getWidth(), getHeight()));
Mat dst = new Mat();
Imgproc.pyrMeanShiftFiltering(resized, dst, 10, 20, 2);
Mat img2show = new Mat();
Imgproc.cvtColor(dst, img2show, Imgproc.COLOR_BGR2RGBA);
Bitmap bmp = Bitmap.createBitmap(img2show.width(), img2show.height(), Config.ARGB_8888);
if (!Utils.matToBitmap(img2show, bmp))
    return;
canvas.drawBitmap(bmp, 0, 0, null);

/Andrey

Andrew Canny

unread,
Aug 18, 2011, 1:49:16 PM8/18/11
to android-opencv
Thanks, now it works. However, before returning the processed image
takes some time, even minutes.
I need something much faster (order of seconds).
Specifically I need to do a segmentation based on color images taken
by camera phone. The segmented image should have no more than 8 colors
(or around that number) and can be any type of image (landscapes,
interiors, rooms, etc.).
Any suggestions? OpenCV can help me or not?
What solution do you propose? (I also accept solutions other than
opencv)
Thanks a lot in advance.

Kirill Kornyakov

unread,
Aug 23, 2011, 4:19:43 AM8/23/11
to android...@googlegroups.com
Mean shift is slow even on a PC, so you should probably post your question to the major group, may be somebody met a similar issue.

Hoà Lê Thanh

unread,
Sep 25, 2011, 1:35:29 AM9/25/11
to android-opencv

Hi, could you give me a help?
I saw you use Imgproc.resize, but in the native library Opencv 2.3.1,
i didn't find it.
Can you show me where you get these library?
Thanks you very much!

Kirill Kornyakov

unread,
Sep 26, 2011, 3:41:56 AM9/26/11
to android...@googlegroups.com
Not sure that I get your question right, but here is native resize function: http://opencv.itseez.com/trunk/modules/imgproc/doc/geometric_transformations.html#resize
Reply all
Reply to author
Forward
0 new messages