原來的版本(不夠簡潔優化):(1條消息) c++實現ENVI1%Liner、2%Liner等拉伸_躊躇向前的博客-CSDN博客https://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<float>(int(counts*ratio));
float cutmax = mimg.at<float>(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)