2

I'm trying to remove the background from an image that has either one coin or one bill.

I try this code:

BackgroundSubtractorMOG BGS = new BackgroundSubtractorMOG(); 
BGS.apply(src, dest,0.1);

the result was an black image.

How can I do that in java using opencv?

The image will be captured from the camera and so it could have background ,I want to remove the background ,, the image could be like that :

enter image description here

Anonymous
  • 119
  • 2
  • 12
  • Can you show what you have tried? – ooXei1sh Sep 12 '15 at 13:03
  • You **can't** apply background subtraction to a single image, unless you have a reference background image. – Miki Sep 12 '15 at 13:09
  • How can I get reference background image ? – Anonymous Sep 12 '15 at 13:17
  • What kind of image have you? What do you want to segment? Please provide some images – Miki Sep 12 '15 at 13:23
  • I'm trying to remove background from an image that has either one coin or one bill. – Anonymous Sep 12 '15 at 13:28
  • I can't tell you much without seeing some reference images. – Miki Sep 12 '15 at 13:33
  • I provide an image in the question. – Anonymous Sep 12 '15 at 13:42
  • Thanks, much clearer now. But I need a clarification. Did you just made up this image copy&pasting the bill on an uniform green background, or you actually have bills on such uniform background? – Miki Sep 12 '15 at 13:47
  • Yes I assume that the background will be some thing like that its not completely uniform it could be any color. – Anonymous Sep 12 '15 at 13:49
  • And so it's a completely different problem :D. If you don't provide some real images, you'll get answers that won't work for you in real cases. – Miki Sep 12 '15 at 13:53
  • Why ? I think the concept will be the same . – Anonymous Sep 12 '15 at 13:54
  • Nope. Uniform background with a single color is trivial. Uniform background with different color is simple. Non uniform background is difficult. Background different for every image is almost impossible. Also, if your bill is always perfectly rectangular and clean then it's easy. If it's folded, dirty, etc, it'll be harder. – Miki Sep 12 '15 at 14:05
  • OK assume that Uniform background with different color , How this can be done ? – Anonymous Sep 12 '15 at 14:19

1 Answers1

3

This is a simple procedure to segment the bill. Basically, since background is uniform, you simply compute the bounding box of the edges.

This code is in C++, but it should be easily ported to Java. Note that your jpeg image has many compression artifacts. If you have a non-compressed image (png) the result would be better.

#include <opencv2\opencv.hpp>
using namespace cv;
using namespace std;

int main() 
{
    Mat3b img = imread("path_to_image");

    Mat1b gray;
    cvtColor(img, gray, COLOR_BGR2GRAY);

    Mat1b edges;
    Canny(gray, edges, 400, 100);

    vector<Point> points;
    findNonZero(edges, points);

    Rect box = boundingRect(points);

    Mat3b bill(img(box));

    return 0;
}

The bill image:

enter image description here

Miki
  • 40,887
  • 13
  • 123
  • 202
  • How to convert this line to java Mat3b bill(img(box)); – Anonymous Sep 12 '15 at 17:07
  • @Walaa http://docs.opencv.org/java/org/opencv/core/Mat.html#Mat(org.opencv.core.Mat, org.opencv.core.Rect) it should be something like: `Mat bill(img, box);` sorry but Java is not my thing.. :D – Miki Sep 12 '15 at 17:11