c++&python实现遥感影像1、2、5...%线性拉伸

原来的版本(不够简洁优化):(1条消息) c++实现ENVI1%Liner、2%Liner等拉伸_踌躇向前的博客-CSDN博客icon-default.png?t=M5H6https://blog.csdn.net/qq_41824159/article/details/105469986?spm=1001.2014.3001.5501

 c++版本:

cv::Mat LinerStrech(cv::Mat img, float ratio)
{
	cv::Mat mimg = img.clone();
    mimg.convertTo(mimg, CV_32FC1);
	int rows = img.rows;
	int cols = img.cols;
	int counts = rows * cols;
	mimg = mimg.reshape(1, counts);
	cv::sort(mimg, mimg, cv::SORT_EVERY_COLUMN + cv::SORT_ASCENDING);
	float cutmin = mimg.at(int(counts*ratio));
	float cutmax = mimg.at(int(counts*(1-ratio)));
	cv::Mat nimg = 255.0 * (img - cutmin) / (cutmax - cutmin);
	nimg.convertTo(nimg, CV_8UC1);
	return nimg;
}

python版本:

def Liner2persent(data, ratio):
    mdata = data.copy()
    rows, cols = data.shape[0:2]
    counts = rows*cols
    mdata = mdata.reshape(counts, 1)
    tdata = np.sort(mdata, axis=0)
    cutmin = tdata[int(counts*ratio), 0]
    cutmax = tdata[int(counts*(1-ratio)), 0]
    cutmax = tdata[int(counts*(1-ratio)), 0]
    ndata = 255.0*(data.astype(np.float32) - cutmin)/float(cutmax-cutmin)
    ndata[data < cutmin] = 0
    ndata[data > cutmax] = 255
    return ndata.astype(np.uint8)

你可能感兴趣的:(图像处理,图像处理,ENVI遥感影像处理,c++,python)