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!