OpenCV各种遥感指数计算

植被指数

比值被指数

image-20220617204953830

归一化植被指数

image-20220617205042234

土壤调节植被指数

image-20220617205115279

修正的土壤调节植被指数

image-20220617205145774

三波段梯度差植被指数

image-20220617205226671

水体指数

归一化水体指数

image-20220617205251070

MNDWI

image-20220617205338418

AWEI

image-20220617205407669

EWI

image-20220617205456435

NWI

image-20220617205521025

image-20220617205543943

建筑指数

DBI

image-20220617205608699

NDBI

image-20220617205624712

IBI

image-20220617205641635

归一化差值裸地与建筑用地指数

image-20220617205700744

裸土指数

image-20220617205732991

代码

#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
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);
}

//OTSU 阈值分割
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

image-20220617205923079

nvdi阈值分割

image-20220617210001802

rvi

image-20220617210059140

rvi阈值分割

image-20220617210111647

savi

image-20220617210138516

savi阈值分割

image-20220617210152720

msavi

image-20220617210228290

msavi阈值分割

image-20220617210238378

ndwi

image-20220617210309385

ndwi阈值分割

image-20220617210318853

mndwi

image-20220617210357411

mndwi阈值分割

image-20220617210415959

dbi

image-20220617210447495

dbi阈值分割

image-20220617210458285

ndbi

image-20220617210523627

ndbi阈值分割

image-20220617210532815

分割阈值输出

image-20220617210555522

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值