如何使用OpenCV从Java中的静态图像中删除背景?
我正在尝试从具有一枚硬币或一张钞票的图像中删除背景.
I'm trying to remove the background from an image that has either one coin or one bill.
我尝试以下代码:
BackgroundSubtractorMOG BGS = new BackgroundSubtractorMOG();
BGS.apply(src, dest,0.1);
结果是黑色图像.
如何在Java中使用opencv做到这一点?
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 :
这是分割帐单的简单过程.基本上,由于背景是均匀的,因此您只需计算边缘的边界框即可.
This is a simple procedure to segment the bill. Basically, since background is uniform, you simply compute the bounding box of the edges.
此代码是C ++,但应易于移植到Java.请注意,您的jpeg图像具有许多压缩伪像.如果您有未压缩的图像(png),效果会更好.
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;
}
bill
图片: