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:
However, opencv version of the result is look like binarized:
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);