突出图像中特定灰度范围的亮度通常是重要的,其应用包括增强特征;
灰度级分层有多种方法,基本方法有两种
代码实现:
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
Mat grayLayered(Mat srcImage)
{
Mat resultImage = srcImage.clone();
int nRows = resultImage.rows;
int nCols = resultImage.cols;
if (resultImage.isContinuous())
{
nCols = nRows*nCols;
nRows = 1;
}
uchar *pDataMat;
int controlMin = 150;
int controlMax = 200;
for (int j = 0; j < nRows; j++)
{
pDataMat = resultImage.ptr<uchar>(j);
for (int i = 0; i < nCols; i++)
{
// //第一种方法:二值映射
// if (pDataMat[i] > controlMin)
// pDataMat[i] = 255;
// else
// pDataMat[i] = 0;
//第二种方法:区域映射
if (pDataMat[i] > controlMin && pDataMat[i] < controlMax)
pDataMat[i] = controlMax;
}
}
return resultImage;
}
int main()
{
Mat srcImage = imread("456.tif");
if (!srcImage.data)
return -1;
Mat srcGray;
cvtColor(srcImage, srcGray, CV_BGR2GRAY);
imshow("srcGray", srcGray);
Mat resultImage = grayLayered(srcGray);
imshow("resultImage", resultImage);
waitKey(0);
return 0;
}
处理结果: