5

这段代码行是什么意思,如何将这段代码转换为 javacv?

gray = Scalar::all(255);

这是与此代码行相关的整个代码。

Mat src = imread("in.jpg"), gray;

cvtColor(src, gray, CV_BGR2GRAY);
threshold(gray, gray, 230, 255, THRESH_BINARY_INV);
vector<Vec4i> hierarchy;
vector<vector<Point> > contours;
findContours(gray, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

gray = Scalar::all(255);

请问有人可以解释一下吗?

4

2 回答 2

2

突出显示的代码行将灰色设置为 255。这是 OpenCV 中可用的将矩阵设置为值的方法之一。

其他方法是:

gray.setTo(255); // prior to 2.3.1 it was a buggy on multichannel images
gray = 255; // prior to 2.3.1 it was a buggy on multichannel images

gray.setTo(Scalar::all(255)); // it works regardless the OpenCV version.

但我认为问题是为什么 findfContours 之后的这条源代码行......

根据文档, findContours 修改了它正在处理的图像(它提取轮廓,删除它,然后继续下一个,直到没有更多的轮廓)。结果是一个垃圾图像(可能是黑色的)。

因此,设置为 255 的行将其清除为其他用途。

Mat::setTo()方法也应该在 JavaCV 中可用,因此将其转换为 Java 应该没有问题

于 2012-07-11T06:03:29.193 回答
2

As I mentioned in my comment, it is used to set the gray image to white.

What is its benefit? It can be said only if we know what is this code for or see the full code.

Regarding Java, OpenCV has now some android samples, in which you can find Java codes.

You can check them. I saw a similar function there : mWhilte = Scalar.all(255);

Also check the JavaCV samples : http://code.google.com/p/javacv/wiki/OpenCV2_Cookbook_Examples_Chapter_2

于 2012-07-11T06:27:02.497 回答