Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: image segmentation on android

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default image segmentation on android

    I am trying run this code that I found on the internet:

    package com.opencv.grabcut.android;

    import java.io.IOException;

    import org.opencv.android.Utils;
    import org.opencv.core.Core;
    import org.opencv.core.CvType;
    import org.opencv.core.Mat;
    import org.opencv.core.Point;
    import org.opencv.core.Rect;
    import org.opencv.core.Scalar;
    import org.opencv.highgui.Highgui;
    import org.opencv.imgproc.Imgproc;

    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    import android.widget.ImageView;
    import android.widget.Toast;


    public class Grabcut extends Activity implements OnTouchListener {
    ImageView imageView;
    Bitmap bitmap;
    Canvas canvas;
    Scalar color = new Scalar(255, 0, 0, 255);
    Point tl, br;
    int counter;
    Bitmap bitmapResult, bitmapBackground;
    Mat dst = new Mat();
    final String pathToImage = "/mnt/sdcard/gcut.png";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grabcut_main);
    imageView = (ImageView) this.findViewById(R.id.imageView);

    bitmap = BitmapFactory.decodeFile(pathToImage);
    Toast msg = Toast.makeText(Grabcut.this, "Press top left and bottom right of the foreground image", Toast.LENGTH_LONG);

    msg.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);

    msg.show();
    bitmapResult = bitmap.copy(bitmap.getConfig(), true);
    canvas = new Canvas(bitmapResult);

    imageView.setImageBitmap(bitmapResult);
    imageView.setOnTouchListener(this);

    tl = new Point();
    br = new Point();
    counter = 0;
    }

    //@Override
    public boolean onTouch(View v, MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
    if (counter == 0) {
    tl.x = event.getX();
    tl.y = event.getY();
    counter++;
    } else if (counter == 1) {
    br.x = event.getX();
    br.y = event.getY();
    counter++;

    Mat img = new Mat();
    img = Highgui.imread(pathToImage);
    Mat background = new Mat();
    try {
    background = Utils.loadResource(getApplicationContext(),
    R.drawable.wall);
    } catch (IOException e) {

    e.printStackTrace();
    }

    backgroundSubtracting(img, background);
    Highgui.imwrite("/mnt/sdcard/GRABCUT/rect.png", dst);
    Bitmap jpg = BitmapFactory
    .decodeFile("/mnt/sdcard/GRABCUT/rect.png");

    imageView.setScaleType(ImageView.ScaleType.CENTER_ INSIDE);
    imageView.setAdjustViewBounds(true);
    imageView.setPadding(2, 2, 2, 2);
    imageView.setImageBitmap(jpg);
    imageView.invalidate();
    }
    }

    return true;
    }

    private void backgroundSubtracting(Mat img, Mat background) {
    Mat firstMask = new Mat();
    Mat bgModel = new Mat();
    Mat fgModel = new Mat();
    Mat mask;
    Mat source = new Mat(1, 1, CvType.CV_8U, new Scalar(3.0));
    dst = new Mat();
    Rect rect = new Rect(tl, br);

    Imgproc.grabCut(img, firstMask, rect, bgModel, fgModel, 1, 0 /* GC_INIT_WITH_RECT */);
    Core.compare(firstMask, source/* GC_PR_FGD */, firstMask, Core.CMP_EQ);

    Mat foreground = new Mat(img.size(), CvType.CV_8UC3, new Scalar(255,
    255, 255));
    img.copyTo(foreground, firstMask);

    Core.rectangle(img, tl, br, color);

    Mat tmp = new Mat();
    Imgproc.resize(background, tmp, img.size());
    background = tmp;
    mask = new Mat(foreground.size(), CvType.CV_8UC1, new Scalar(255, 255, 255));

    Imgproc.cvtColor(foreground, mask, 6/* COLOR_BGR2GRAY */);
    Imgproc.threshold(mask, mask, 254, 255, 1 /* THRESH_BINARY_INV */);

    Mat vals = new Mat(1, 1, CvType.CV_8UC3, new Scalar(0.0));
    background.copyTo(dst);

    background.setTo(vals, mask);

    Core.add(background, foreground, dst, mask);

    firstMask.release();
    source.release();
    bgModel.release();
    fgModel.release();
    vals.release();
    }

    }


    I've manage to fix some errors of this program, but there is still one error that I can't fix:

    the error is "wall cannot be resolved or is not a field"

    The error always points on R.drawable.wall inside the the public boolean onTouch()

    please help how can i fix this error...

    thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: image segmentation on android

    Thread moved.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    the error is "wall cannot be resolved or is not a field"
    Where is wall defined? There should be a definition in the drawable folder.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Grabcut Image Segmentation Algorithm
    By messier32 in forum Algorithms & Recursion
    Replies: 2
    Last Post: September 13th, 2014, 02:32 PM
  2. How can I make image to move on java for android aplication?
    By Mikael in forum Android Development
    Replies: 5
    Last Post: May 6th, 2014, 05:08 PM
  3. android image processing ( cropping)
    By bangun in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 21st, 2013, 06:36 AM
  4. Android image processing
    By bangun in forum Android Development
    Replies: 0
    Last Post: September 16th, 2013, 10:31 PM