#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
/*
形态学操作:提取水平线或垂线
1、输入彩色图像;
2、转化为灰度图像
3、转化为二值图像
4、定义结构元素
5、形态学操作 开操作(先腐蚀,后膨胀)可去掉小的对象
*/
int main()
{
//1、输入彩色图像;
Mat srcImg = imread("C:/Users/admin/Desktop/test222.png");
if (!srcImg.data)
{
printf("没有找到!!!");
return -1;
}
//2、转化为灰度图像
Mat grayImg;
cvtColor(srcImg, grayImg, CV_BGR2GRAY);
namedWindow("grayImg windows", CV_WINDOW_AUTOSIZE);
imshow("grayImg windows", grayImg);
//3、转化为二值图像 灰度图像 才能转化二值图像
Mat binImg;
adaptiveThreshold(~grayImg, binImg, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
namedWindow("binImg windows", CV_WINDOW_AUTOSIZE);
imshow("binImg windows", binImg);
//4、定义结构元素
Mat hline = getStructuringElement(MORPH_RECT, Size(srcImg.cols/16, 1), Point(-1, -1));//水平结构
Mat vline = getStructuringElement(MORPH_RECT, Size(1, srcImg.rows/16), Point(-1, -1));//垂直结构
Mat rectStruct = getStructuringElement(MORPH_RECT, Size(3,3), Point(-1, -1));
//5、形态学操作 开操作(先腐蚀,后膨胀)可去掉小的对象
Mat outImg;
//morphologyEx(binImg, outImg, MORPH_OPEN, rectStruct);//开操作(先腐蚀后膨胀)
Mat tmpImg;
erode(binImg, tmpImg, rectStruct, Point(-1, -1));//腐蚀
dilate(tmpImg, outImg, rectStruct, Point(-1, -1));
bitwise_not(outImg, outImg);//反色
//blur(outImg, outImg, Size(3, 3), Point(-1, -1));//均值滤波
namedWindow("outImg windows", CV_WINDOW_AUTOSIZE);
imshow("outImg windows", outImg);
namedWindow("srcImg windows",CV_WINDOW_AUTOSIZE);
imshow("srcImg windows",srcImg);
waitKey(0);
return 0;
}
2、高斯金字塔
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
//图像金字塔
//1、输入彩色图像;
Mat srcImg = imread("C:/Users/admin/Desktop/1.JPG");
if (!srcImg.data)
{
printf("没有找到!!!");
return -1;
}
//上采样
Mat s_upImg;
pyrUp(srcImg, s_upImg, Size(srcImg.cols * 2, srcImg.rows * 2));
imshow("s_upImg window", s_upImg);
//降采样
Mat s_downImg;
pyrDown(srcImg, s_downImg, Size(srcImg.cols / 2, srcImg.rows / 2));
imshow("s_downImg window", s_downImg);
//高斯不同
Mat gray_srcImg, g1, g2,dog_img;
//转化为灰度图像
cvtColor(srcImg, gray_srcImg, CV_BGR2GRAY);
//进行高斯模糊 高斯不同
GaussianBlur(gray_srcImg, g1, Size(5, 5), 0, 0);
GaussianBlur(g1, g2, Size(5, 5), 0);
subtract(g1, g2, dog_img,Mat());
normalize(dog_img, dog_img, 255, 0, NORM_MINMAX);//归一化显示
imshow("dog_img window", dog_img);
namedWindow("srcImg windows", CV_WINDOW_AUTOSIZE);
imshow("srcImg windows", srcImg);
waitKey(0);
return 0;
}
3、基本阈值操作
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
Mat srcImg, gray_Img, dstImg;
char OUT_PUT_WINDOW[] = "out_put window";
int threshold_value = 127;
int threshold_max = 255;
int type_value = 2;
int max_value = 4;
void CallBack_Dome(int, void*);
int main()
{
srcImg = imread("C:/Users/admin/Desktop/1.JPG");
if (!srcImg.data)
{
printf("没有找到!!!");
return -1;
}
namedWindow(OUT_PUT_WINDOW, CV_WINDOW_AUTOSIZE);
createTrackbar("Threshold value:", OUT_PUT_WINDOW, &threshold_value, threshold_max, CallBack_Dome);
createTrackbar("type value:", OUT_PUT_WINDOW, &type_value, max_value, CallBack_Dome);
CallBack_Dome(0,0);
namedWindow("srcImg windows", CV_WINDOW_AUTOSIZE);
imshow("srcImg windows", srcImg);
waitKey(0);
return 0;
}
void CallBack_Dome(int, void*)
{
cvtColor(srcImg, gray_Img, CV_BGR2GRAY);
//threshold(gray_Img, dstImg, threshold_value, threshold_max, type_value);//THRESH_BINARY 二值化 THRESH_BINARY_INV 反向二值化
threshold(gray_Img, dstImg, 0, 255, THRESH_TRUNC|type_value);//自动计算阈值THRESH_OTSU
imshow(OUT_PUT_WINDOW, dstImg);
}