I'm looking for the most optimal way of extracting single color on background and replace it with some picture on the fly in JavaCV. I wrote very simple method reading pixel by pixel and replacing it if the color is green for example. This is very slow and even if I divided it into few threads it works dramatically slow. This must be done on live so I have to find out some other method. Perhaps I will have to add some watermark as well after background replacing, so the application must be quite fast.
Does anybody can help me finding right way of doing it ? I spend all day long trying to find other way of doing it, but.. all examples I have found doesn't work or maybe I do not know how to adopt them to my needs.
This guy (http://www.youtube.com/watch?v=WOEuE3D88b0) wrote that he is reading pixel by pixel - no way, or maybe he had very fast multi-core workstation. On my MacBook Pro 2.5 i5 it just doesn't work :(
I have used your example and I think that I have mixed it totally. I can see mask of moving me ( white on black background) and if I use CvCopy I can see background appearing at the places where white dots are. Unfortunatelly his is not requested result, but I think I must be very close to it ;) I have to leave me and replace all static objects. Could you please examine my code ?
private static final IplImage back = cvLoadImage("/Users/user/app/eclipse/JavaCV/resources/1.png", CV_LOAD_IMAGE_COLOR);
private static BackgroundSubtractorMOG2 bgs = new BackgroundSubtractorMOG2(30,16,false);
public static IplImage replacePixels2(IplImage img){
IplImage frame = cvCloneImage(img);
//bgs.getBackgroundImage(frame);
IplImage image = IplImage.create(frame.width(), frame.height(), IPL_DEPTH_8U,1);
bgs.apply(frame, image, -1);
IplROI roi = new IplROI();
roi.xOffset(0);
roi.yOffset(0);
roi.width(frame.width());
roi.height(frame.height());
IplImage backImageWithRoi = frame.roi(roi);
cvCopy(back, backImageWithRoi, image );
return backImageWithRoi;
}
I also tried grabCut but this is extremely slow and can't be used in changing stream on live :(
Best Regards Jan
public static IplImage bgTest(IplImage img){ IplImage frame = cvCloneImage(img); CvRect rect = new CvRect(0, 0, 200, 200); IplImage result = IplImage.create(cvGetSize(frame), IPL_DEPTH_8U, 1); CvMat a = new CvMat(null); CvMat b = new CvMat(null); grabCut(frame, result, rect, a, b, 3, GC_INIT_WITH_RECT); cvCmpS(result, GC_PR_FGD, result, CV_CMP_EQ); return result; }
– johnnyGor Aug 17 '13 at 21:37