2

Firstly, if you don't know, i should tell what is ndwi. Ndwi stands for normalized difference water index. It is a graphical indicator for water and the value range is [-1 1]. Ndwi is defined as follows:

(Green - NIR) / (Green + NIR)

I am middle of a simple coastline extraction tool based on opencv. I have accomplished it in MATLAB and the result is shown like this:

MATLAB NDWI

However, opencv version of the result is look like binarized:

OPENCV NDWI

When i debugged the program, i see that minimum value in the ndwi matrix is zero and this is wrong because it should be -0.8057. The code which is responsible for ndwi calculation (opencv version) as follows:

Mat ndwi = (greenRoi - nirRoi) / (greenRoi + nirRoi);
double min;
double max;
minMaxIdx(ndwi, &min, &max);
Mat adjNDWI;
convertScaleAbs(ndwi, adjNDWI, 255 / max);

What is the problem in here and how can i achieve to calculate the right ndwi values?

Note:

greenRoi and nirRoi are created in this way:

Rect rectangle = boundingRect(Mat(testCorners)); //vector<Point2f> testCorners(4);

Mat testImgGreen = imread((LPCSTR)testImgGreenPath, 0);
Mat testImgNir = imread((LPCSTR)testImgNirPath, 0);

Mat greenRoi(testImgGreen, rectangle);
Mat nirRoi(testImgNir, rectangle);
4

2 回答 2

2

您需要显式创建一个浮点 cv::Mat

cv::Mat image(cols,rows,CV_32FC1)或者CV_64FC1如果你需要双打

于 2013-05-28T02:51:11.650 回答
1

greenRoi、nirRoi 和 ndwi 的元素都将是 uchar 的(Mat 将是 CV_8UC1)。

假设 greenRoi = 10,nirRoi = 40。

你的答案不是 (10 - 40)/(10+40) = -0.6。答案必须是肯定的(因为它没有符号)并且不能是分数。根据我的计算器,这将给出 0。

@Martin Beckett 是正确的,将 testImgGreen 和 testImgNir 转换为浮点类型的矩阵,它将起作用。你需要:

testImgGreen.convertTo(testImgGreen, CV_32F);
testImgNir.convertTo(testImgNir , CV_32F);

Mat greenRoi(testImgGreen, rectangle);
Mat nirRoi(testImgNir, rectangle);
Mat ndwi = (greenRoi - nirRoi) / (greenRoi + nirRoi);
于 2013-05-28T04:42:58.017 回答