Mat matBinary, matGray, matOut;
//***********读图 show图***********
Mat inMat = imread("pic.bmp", -1); //读进原图
//Mat inMat = imread("pic.bmp", 0) //将原图转为单通道灰度图在读进
//Mat inMat = imread("pic.bmp", 1) //将原图转为三通道RGB图在读进
imshow("winname", inMat); //show出inMat
//***********腐蚀 膨胀***********
Mat element = getStructuringElement(MORPH_RECT, Size(11,11));
dilate(matBinary, matBinary, element); //膨胀
erode(matBinary, matBinary, element);//腐蚀
//***********二值化***********
threshold(matGray, matBinary, 0, 255, THRESH_BINARY | THRESH_OTSU); //大津法,自动计算二值化阈值,能够有效的区分出前景和背景
//threshold(matGray, matBinary, 177, 255, THRESH_BINARY); //灰度值在170-255间的值置为255
//adaptiveThreshold(inMat, outMat, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 93, 7);//自适应二值化。通过图像的局部特征自适应的设定阈值,做出二值化处理(倒数第二个参数)
//***********卷积************
Mat kernel = (Mat_<float>(3,3) << -1,-1,-1,2,3,2,-1,-1,-1); //边缘图
filter2D(matGray, matOut, -1, kernel);
//***********找轮廓************
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours( matBinary, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE );//必须用二值化的图片
for (int i = 0; i < contours.size(); i++)
{
Rect rect = boundingRect(contours[i]); //找出轮廓最小水平矩形 rect包含矩形左上角坐标,和矩形的长,宽,面积
//RotatedRect rotateRect = minAreaRect(contours[i]); //找出轮廓最小矩形 rotateRect包含 矩形四个角点:P[0]为四个点中y坐标最大的点,顺时针,矩形角度:角度在[0,-90]之间
//Point2f p[4];
//rotateRect.points(p);
//Point2f center; float radius;
//minEnclosingCircle(contours[i], center, radius);//找出轮廓最小外接圆圆心和半径
}
//***********拼接两张图************
vconcat(inMat1, inMat2, saveMat); //横向拼接,两张图的高度一样
hconcat(inMat1, inMat2, saveMat); //竖向拼接,两张图的宽度需要一样
//***********模板匹配************
Mat matResult;
matchTemplate(matCheck, matGolden, matResult, cv::TM_CCOEFF_NORMED);
//通过minMaxLoc定位最佳匹配位置
double minValue = -1, maxValue = 1;
cv::Point minLocation, maxLocation;//相似区域左上角坐标
cv::Point matchLocation;
minMaxLoc(matResult, &minValue, &maxValue, &minLocation, &maxLocation, cv::Mat());
qDebug()<< "匹配度:" << maxValue;
Rect rect(maxLocation.x, maxLocation.y, matGolden.cols, matGolden.rows);//相似度最高区域
cv::cvtColor(matCheck, checkRoi, cv::COLOR_GRAY2BGR); //转为3通道图
cv::rectangle(matCheck, rect, cv::Scalar(0, 0, 255),1, cv::LINE_8,0);//框出最相似区域
putText(matCheck, text.toStdString(), maxLocation, cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 255, 0), 1, 8, false);//指定位置写文字
//***********画线************
line(matCheck, p[0], p[1], Scalar(255,255,255), 1);//用白线连接p[0] p[1]
//***********更改rgb图的单个像素点值************
Point pos;
matCheck.at<Vec3b>(pos)[0] = 225; //B
matCheck.at<Vec3b>(pos)[1] = 225; //G
matCheck.at<Vec3b>(pos)[2] = 225; //R
//***********更改灰度图的单个像素点值************
matGray.at<uchar>(i, j) = 255; // i图片的行,j图片的列 --->取图片的像素点和图片上某一点的坐标的表示方法不同 Point p(j, i)表示坐标。i, j搞反,容易发生越界,导致crash。
//***********求rgb图片的平均R G B 值
bool MainWindow::GetAverageColor(Mat &matSrc,Scalar averaScalar)
{
if(SourceMat.empty()||SourceMat.channels()<3)
{
return false;
}
Mat ImgRGB;
ImgRGB = matSrc.clone();
averaScalar=cv::mean(ImgRGB); //平均RGB值
Mat matRes;
inRange(matSrc, Scalar(averaScalar[0]-30, averaScalar[1]-30, averaScalar[2]-30), Scalar(averaScalar[0]+30, averaScalar[1]+30, averaScalar[2]+30), matRes); //筛选出rgb值在平均值正负30范围内的点
return true;
}
//***************卷积核********************
void adjustBrightnessContrast(InputArray src, OutputArray dst)
{
Mat input = src.getMat();
dst.create(src.size(), src.type());
Mat output = dst.getMat();
Mat imageEnhance;
//Mat kernel = (Mat_<float>(3, 3) << -2, -1, 0, -1, 1, 1, 0, 1, 2);//浮雕
//Mat kernel = (Mat_<float>(3, 3) << 0, -1, 0, 0, 5, 0, 0, -1, 0);//亮度增强
Mat kernel = (Mat_<float>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);//锐化
//Mat kernel = (Mat_<float>(3, 3) << 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4);//邻近平均滤波,模糊处理
//Mat kernel = (Mat_<float>(3, 3) << -1,-1,-1,-1,9,-1,-1,-1,-1);//超锐化
//Mat kernel = (Mat_<float>(3, 3) << -1, -1, -1, -1, 8.5, -1, -1, -1, -1);//亮度减弱+锐化
//Mat kernel = (Mat_<float>(3, 3) << 0, -1, 0, -1, 4.5, -1, 0, -1, 0);//亮度减弱
//Mat kernel = (Mat_<float>(3, 3) << -1,-1,-1,2,3,2,-1,-1,-1);//边缘图
//Mat kernel = (Mat_<float>(7, 7) << -1,-1,-1,-1,-1,-1,-1,
// -1,-1,-1,-1,-1,-1,-1,
// 1.5, 1.5, 1.5, 1.5, 1.5,1.5,1.5,
// 1.5, 1.5, 1.5, 1.5, 1.5,1.5,1.5,
// 1.5, 1.5, 1.5, 1.5, 1.5,1.5,1.5,
// -1, -1, -1, -1, -1,-1,-1,
// -1,-1,-1,-1,-1,-1,-1);
/*Mat kernel = (Mat_<float>(5, 5) << -9,-9,-9,-9,-9,
9,9,9,9,9,
-1,2,3,2,-1,
9,9,9,9,9,
-9,-9,-9,-9,-9);*/
//Mat kernel = (Mat_<float>(3, 3) << -1,-1,-1,-1,9,-1,-1,-1,-1);
Mat sss;
medianBlur(src,sss,7);
filter2D(sss, imageEnhance, CV_8UC3, kernel);
imageEnhance.copyTo(output);
}