refining image

25 views
Skip to first unread message

Larienas

unread,
Mar 10, 2021, 7:31:21 AM3/10/21
to javacv
Hi Samuel,

  I have an image extracted using MaskRCNN on the left handside. Is it possible with openCV/JavaCV to convert to image on right handside?

mask.pngHi Samuel,

With Regards,
Lariena

  

Samuel Audet

unread,
Mar 11, 2021, 12:43:24 AM3/11/21
to jav...@googlegroups.com, Larienas
That looks like the kind of job for something like GrabCut:
https://docs.opencv.org/master/d8/d83/tutorial_py_grabcut.html
https://docs.opencv.org/master/d8/d34/samples_2cpp_2grabcut_8cpp-example.html
The C++ sample code shouldn't be too hard to translate to Java, but
please let me know if you encounter any issues!

On 3/10/21 9:31 PM, Larienas wrote:
> Hi Samuel,
>
>   I have an image extracted using MaskRCNN on the left handside. Is it
> possible with openCV/JavaCV to convert to image on right handside?
>
>
> With Regards,
> Lariena

larien...@gmail.com

unread,
Mar 12, 2021, 3:30:06 PM3/12/21
to Samuel Audet, jav...@googlegroups.com
Hi Samuel,

Please see my code below

public static void main(String[] args) throws IOException {

String file = "C:\\Users\\OpenCV\\1.jpg";
Mat image = load(new File(file), IMREAD_COLOR);

String maskFile = "C:\\Users\\OpenCV\\1_mask.jpg";
Mat maskimage = load(new File(maskFile), IMREAD_GRAYSCALE);

show(toMat8U(image), "original");

int iterCount = 5;
int mode = GC_INIT_WITH_MASK;

// Need to allocate arrays for temporary data
Mat bgdModel = new Mat();
Mat fgdModel = new Mat();

// GrabCut segmentation
grabCut(image, maskimage, new Rect(), bgdModel, fgdModel, iterCount, mode);

show(toMat8U(image), "Result foreground mask");
}

I am getting error as
Exception in thread "main" java.lang.RuntimeException: OpenCV(4.1.2) C:\projects\javacpp-presets\opencv\cppbuild\windows-x86_64\opencv-4.1.2\modules\imgproc\src\grabcut.cpp:344: error: (-5:Bad argument) mask element value must be equal GC_BGD or GC_FGD or GC_PR_BGD or GC_PR_FGD in function 'checkMask'

at org.bytedeco.opencv.global.opencv_imgproc.grabCut(Native Method)
at com. GrabCut.main(Ex5GrabCut.java:46)

I have also attached both the image and mask I got from Mask RCNN

With Regards,
Lariena
1.jpg
1_mask.jpg

Larienas Sujith

unread,
Mar 13, 2021, 4:09:50 AM3/13/21
to Samuel Audet, javacv
Hi Samuel,

  I was wondering is it possible to make the black part of the images in my previous mail 1.jpg and 1_mask.jpg transparent?

With Regards,
Lariena

Samuel Audet

unread,
Mar 13, 2021, 4:24:33 AM3/13/21
to Larienas Sujith, javacv
OpenCV functions don't support transparency, but we can consider the channels however we want, yes. You'll need to follow the requirements of the algorithm for the mask though. We can't use any arbitrary values!

larien...@gmail.com

unread,
Mar 13, 2021, 4:31:51 AM3/13/21
to Samuel Audet, javacv

Hi Samuel,

 

  Thanks for your reply 😊. When you say “we can consider channels however we want” that mean it is possible to do it or not?

 

With Regards,

Lariena

larien...@gmail.com

unread,
Mar 13, 2021, 12:54:09 PM3/13/21
to Samuel Audet, javacv

Hi Samuel,

 

  Playing with channels solved the issue. Thanks for your guidance.

 

With Regards,

Lariena

 

From: Samuel Audet <samuel...@gmail.com>
Sent: samedi 13 mars 2021 10:24
To: Larienas Sujith <larien...@gmail.com>
Cc: javacv <jav...@googlegroups.com>

Reply all
Reply to author
Forward
0 new messages