图像二值化
//二值化
cvtColor(image,image_gray,COLOR_BGR2GRAY);
threshold(image_gray,image_thre,125,255,THRESH_BINARY);
imshow("仓老师",image);
imshow("仓老师黑白",image_thre);
腐蚀
//内核,可选矩形MORPH_RECT、交叉形MORPH_CROSS、椭圆MORPH_ELLIPSE
Mat element=getStructuringElement(MORPH_ELLIPSE,Size(5,5),Point(-1,-1));
//腐蚀
Mat image_erode;
erode(image_thre,image_erode,element);
imshow("腐蚀",image_erode);
//膨胀
Mat image_dilate;
dilate(image_thre,image_dilate,element);
imshow("膨胀",image_dilate);
开操作
//开
Mat image_open;
morphologyEx(image_thre,image_open,MORPH_OPEN,element);
imshow("开",image_open);
//闭
Mat image_close;
morphologyEx(image_thre,image_close,MORPH_CLOSE,element);
imshow("闭",image_close);
/*
形态学操作初步
腐蚀、膨胀、开操作、闭操作
*/
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
int main(int argc, char* argv[]){
Mat image=imread("仓老师.jpg");
Mat image_gray,image_thre;
//二值化
cvtColor(image,image_gray,COLOR_BGR2GRAY);
threshold(image_gray,image_thre,125,255,THRESH_BINARY);
imshow("仓老师",image_thre);
//内核,可选矩形MORPH_RECT、交叉形MORPH_CROSS、椭圆MORPH_ELLIPSE
Mat element=getStructuringElement(MORPH_ELLIPSE,Size(5,5),Point(-1,-1));
//腐蚀
Mat image_erode;
erode(image_thre,image_erode,element);
imshow("腐蚀",image_erode);
//膨胀
Mat image_dilate;
dilate(image_thre,image_dilate,element);
imshow("膨胀",image_dilate);
//开
Mat image_open;
morphologyEx(image_thre,image_open,MORPH_OPEN,element);
imshow("开",image_open);
//闭
Mat image_close;
morphologyEx(image_thre,image_close,MORPH_CLOSE,element);
imshow("闭",image_close);
waitKey(0);
return 0;
}