What is the meaning of following code line in opencv?

我是研究僧i 提交于 2020-01-12 03:39:09

问题


What does this code line means and how can I convert this code into javacv?

gray = Scalar::all(255);

This is whole code which related to this code line.

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);

Please can some one explain about this ?


回答1:


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




回答2:


The highlighted code line sets gray to 255. It is one of the methods avaliable in OpenCV to set a matrix to a value.

Other ways to do it are:

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.

But I think the question is why this source line after findfContours...

According to the docs, findContours modifies the image it is working on (it extracts a contour, deteles it, then proceed to the next one, until there are no more contours). The result is a garbage image (probably black).

So, the set-to-255 line clears it up for some other use.

The Mat::setTo() method should also be available in JavaCV, so you should not have problems converting it to Java



来源:https://stackoverflow.com/questions/11426354/what-is-the-meaning-of-following-code-line-in-opencv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!