So I picked up OpenCV 3 days ago. Total newbie. I downloaded code online to implement Lazy Snapping, an image cut-out tool which is quite similar to Grabcut. I thought it was a C++ project but most of the code is written in C style and is complete till the mask creation part. I thought of extending it to apply the mask on the original image and cutting out the foreground and pasting it into a different image.
I couldn't find much help on C. So I saved the mask and read it using Mat (C++). Here's a little code snippet:
cvSaveImage("Mask.jpg",mask);
cvSaveImage("Object_Marking_Output.jpg",showImg);
cvSaveImage("Original.jpg",original);
Mat masky = imread("Lazy Snapping/mask.jpg", 1);
Mat showImg1 = imread("Lazy Snapping/Original.jpg", 1);
Mat crop(showImg1.rows, showImg1.cols, CV_8UC3);
//Though the mask appears black and white I kept getting errors
//This convert code helped it go away. I could recreate the error if necessary
cvtColor(masky,masky,CV_RGB2GRAY);
//I resize it because the mask created is smaller
cv::resize(masky, masky, showImg1.size());
showImg1.copyTo(crop, masky);
//bitwise_and(showImg1, cv::Scalar(255,255,255), crop, masky);
// normalize so imwrite(...)/imshow(...) shows the mask correctly!
normalize(masky.clone(), masky, 0.0, 255.0, CV_MINMAX, CV_8UC1);
// show the images
imshow("Mask used to apply on image", masky);
imshow("Lazy snapped output", crop);
However my output looks very strange. I am trying to remove the towel on the head treating everything else as background. The mask created is perfect and outlines the towel only. However when applying it on the input image, I get these extra scatter bits of the background on the output image (its not letting me post images directly. So here's a dropbox link):
https://www.dropbox.com/sh/z4u22n9yyfhcnpx/AAA8QxU9tkLGgEEyS_2QFuG2a
I have no idea why this is happening because from the tutorials and answers I have been reading on forums, it looks like the right way to go. copyTo and bitwise_and are giving the same output. Any help is much appreciated. Thanks in advance!
those 'holes' are already in your mask img (if you have some imgviewer with a pipette tool, you can check yourself, the resp.parts are not completely black, but like 0x020202, so the masking fails in that areas)
so,
threshold(masky,masky, 120,255, 0);
your image, so it is perfectly 'binary'
PS.: avoid saving stuff like that as jpg. your problem looks like it is a compression/interpolation artefact