植被指数
比值被指数
归一化植被指数
土壤调节植被指数
修正的土壤调节植被指数
三波段梯度差植被指数
水体指数
归一化水体指数
MNDWI
AWEI
EWI
NWI
建筑指数
DBI
NDBI
IBI
归一化差值裸地与建筑用地指数
裸土指数
代码
#include
#include
#include
using namespace std;
using namespace cv;
void RVI(const Mat &nir, const Mat &r,Mat &rvi)
{
divide(nir, r, rvi,1,CV_32F);
}
void NDVI(const Mat &nir, const Mat &r, Mat &ndvi)
{
divide(nir - r, nir + r, ndvi, 1, CV_32F);
}
void SAVI(const Mat &nir, const Mat &r, double L,Mat &savi)
{
divide((nir - r)*(1 + L), nir + r + L, savi, 1, CV_32F);
}
void MSAVI(const Mat &nir,const Mat &r,Mat &msavi)
{
Mat tmp1, tmp2,tmp3;
pow(2 * nir + 1, 2, tmp1);
tmp2 = tmp1 - 8 * (nir - r);
tmp2.convertTo(tmp2, CV_32F);
pow(tmp2, 0.5, tmp3);
subtract(2 * nir + 1, tmp3, msavi, noArray(), CV_32F);
msavi *= 0.5;
return;
}
void NDWI(const Mat &nir, const Mat &g, Mat & ndwi)
{
divide(g - nir, g + nir, ndwi, 1, CV_32F);
}
void MNDWI(const Mat &swir, const Mat &g, Mat &mndwi)
{
divide(g - swir, g + swir, mndwi, 1, CV_32F);
}
void DBI(const Mat &mir, const Mat & nir, Mat & dbi)
{
dbi = mir - nir;
}
void NDBI(const Mat &swir, const Mat &nir, Mat & ndbi)
{
divide(swir - nir, swir + nir, ndbi, 1, CV_32F);
}
void OTSUSegmentation(const Mat & input, Mat & output, double &value)
{
double minv, maxv;
minMaxLoc(input, &minv, &maxv);
Mat tmp;
divide(input - minv, maxv - minv, tmp, 1, CV_32F);
tmp = tmp * 255;
tmp.convertTo(tmp, CV_8U);
value = threshold(tmp, output, 128, 255, ThresholdTypes::THRESH_BINARY | ThresholdTypes::THRESH_OTSU);
value /= 255;
value = value * (maxv - minv) + minv;
}
int main()
{
Mat nir = imread("./tifs/tm4.tif", ImreadModes::IMREAD_GRAYSCALE);
Mat r = imread("./tifs/tm3.tif", ImreadModes::IMREAD_GRAYSCALE);
Mat swir = imread("./tifs/tm5.tif", ImreadModes::IMREAD_GRAYSCALE);
Mat b= imread("./tifs/tm1.tif", ImreadModes::IMREAD_GRAYSCALE);
Mat g=imread("./tifs/tm2.tif", ImreadModes::IMREAD_GRAYSCALE);
Mat mir= imread("./tifs/tm7.tif", ImreadModes::IMREAD_GRAYSCALE);
Mat rvi;
RVI(nir, r, rvi);
imwrite("./tifs/rvi.tif", rvi);
Mat rviArea;
double rviV;
OTSUSegmentation(rvi, rviArea, rviV);
imwrite("./tifs/rviArea.tif", rviArea);
cout << "rvi阈值:" << rviV << endl;
Mat ndvi;
NDVI(nir, r, ndvi);
imwrite("./tifs/ndvi.tif", ndvi);
Mat ndviArea;
double ndviV;
OTSUSegmentation(ndvi, ndviArea, ndviV);
imwrite("./tifs/ndviArea.tif", ndviArea);
cout << "ndvi阈值:" << ndviV << endl;
Mat savi;
SAVI(nir, r, 0.5,savi);
imwrite("./tifs/savi.tif", savi);
Mat saviArea;
double saviV;
OTSUSegmentation(savi,saviArea,saviV);
imwrite("./tifs/saviArea.tif", saviArea);
cout << "savi阈值:" << saviV << endl;
Mat msavi;
MSAVI(nir, r,msavi);
imwrite("./tifs/msavi.tif", msavi);
Mat msaviArea;
double msaviV;
OTSUSegmentation(msavi, msaviArea, msaviV);
imwrite("./tifs/msaviArea.tif", msaviArea);
cout << "msavi阈值:" << msaviV << endl;
Mat ndwi;
NDWI(nir, g,ndwi);
imwrite("./tifs/ndwi.tif", ndwi);
Mat ndwiArea;
double ndwiV;
OTSUSegmentation(ndwi, ndwiArea, ndwiV);
imwrite("./tifs/ndwiArea.tif",ndwiArea);
cout << "ndwi阈值:" << ndwiV << endl;
Mat mndwi;
MNDWI(swir, g, mndwi);
imwrite("./tifs/mndwi.tif", mndwi);
Mat mndwiArea;
double mndwiV;
OTSUSegmentation(mndwi, mndwiArea, mndwiV);
imwrite("./tifs/mndwiArea.tif", mndwiArea);
cout << "mndwi阈值:" << mndwiV << endl;
Mat dbi;
DBI(mir, nir, dbi);
imwrite("./tifs/dbi.tif", dbi);
Mat dbiArea;
double dbiV;
OTSUSegmentation(dbi, dbiArea, dbiV);
imwrite("./tifs/dbiArea.tif", dbiArea);
cout << "dbi阈值:" << dbiV << endl;
Mat ndbi;
NDBI(swir, nir, ndbi);
imwrite("./tifs/ndbi.tif", ndbi);
Mat ndbiArea;
double ndbiV;
OTSUSegmentation(ndbi, ndbiArea, ndbiV);
imwrite("./tifs/ndbiArea.tif", ndbiArea);
cout << "dbi阈值:" << ndbiV << endl;
system("pause");
return 0;
}
结果
ndvi
nvdi阈值分割
rvi
rvi阈值分割
savi
savi阈值分割
msavi
msavi阈值分割
ndwi
ndwi阈值分割
mndwi
mndwi阈值分割
dbi
dbi阈值分割
ndbi
ndbi阈值分割
分割阈值输出