Grabcut example

101 views
Skip to first unread message

Larienas

unread,
Apr 5, 2020, 9:23:50 PM4/5/20
to javacv
Hello,


  I have done one to one translation from scala example to java as show below

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.WindowConstants;

import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacpp.annotation.ByVal;
import org.bytedeco.javacpp.indexer.FloatIndexer;
import org.bytedeco.javacpp.indexer.IntIndexer;
import org.bytedeco.javacpp.indexer.UByteIndexer;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter.ToMat;
import org.bytedeco.opencv.global.opencv_imgproc;
import org.bytedeco.opencv.opencv_core.*;
import org.bytedeco.opencv.opencv_imgproc.*;
import static org.bytedeco.opencv.global.opencv_core.*;
import static org.bytedeco.opencv.global.opencv_imgcodecs.*;
import static org.bytedeco.opencv.global.opencv_imgproc.*;

public class GrabcutExample {

public static void main(String[] args) {
String file = "group.jpg";
Mat src = OpenCVUtilsJava.loadAndShowOrExit(new File(file), IMREAD_COLOR);
Rect rect = new Rect(10, 100, 380, 180);
Mat result = new Mat();
int iterCount = 5;
int mode = GC_INIT_WITH_RECT;
Mat bgdModel = new Mat();
Mat fgdModel = new Mat();
grabCut(src, result, rect, bgdModel, fgdModel, iterCount, mode);
threshold(result, result, GC_PR_FGD - 0.5, GC_PR_FGD + 0.5, THRESH_BINARY);
display(result, "Result foreground mask");
}

static void display(Mat image, String caption) {
// Create image window named "My Image".
final CanvasFrame canvas = new CanvasFrame(caption, 1.0);

// Request closing of the application when the image window is closed.
canvas.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

// Convert from OpenCV Mat to Java Buffered image for display
final OpenCVFrameConverter converter = new OpenCVFrameConverter.ToMat();
// Show image on window.
canvas.showImage(converter.convert(image));
}

}

The result I am getting is just a black screen. Could anyone help me to get it right. I have also attached "group.jpg" pic if anyone tries to execute the code

group.jpg















With Regards,
Larienas

Samuel Audet

unread,
Apr 6, 2020, 12:33:56 AM4/6/20
to jav...@googlegroups.com, Larienas, Jarek Sacha
On 4/6/20 10:23 AM, Larienas wrote:
>   I am using grabcut as shown in the example
> https://github.com/bytedeco/javacv-examples/blob/master/OpenCV_Cookbook/src/main/scala/opencv_cookbook/chapter05/Ex5GrabCut.scala
>
>   I have done one to one translation from scala example to java as show below

I would recommend to take a look at the original sample in C++ here:
https://github.com/opencv/opencv/blob/master/samples/cpp/grabcut.cpp

If it's still not clear what you're missing, please ask again! Thanks

Samuel

larien...@gmail.com

unread,
Apr 7, 2020, 11:38:20 AM4/7/20
to Samuel Audet, jav...@googlegroups.com, Jarek Sacha
Hello Samuel,

I am very new to Open CV and I am not an expert in C/C++. I know Java that’s why I using JAVACV, otherwise I would have used OpenCV in C++ itself.

I really don’t know what I should look for in the original code to make my Java code work.

When I went through the original code it is also calling " grabCut" function.

As you are the expert it would be very much helpful to get a working example of grabcut

With Regards,
Lariena

Samuel Audet

unread,
Apr 10, 2020, 7:22:31 PM4/10/20
to larien...@gmail.com, jav...@googlegroups.com, Jarek Sacha
From the Scala example, it just looks like you forgot the call to
toMat8U(). Try adding that and it should work!

larien...@gmail.com

unread,
Apr 11, 2020, 2:09:48 AM4/11/20
to Samuel Audet, jav...@googlegroups.com, Jarek Sacha
Hello Samuel,

It is working 😊 Thank you so much. Much appreciated

With Regards,
Larienas
Reply all
Reply to author
Forward
0 new messages