加强版第十二章 开闭操作
开操作=腐蚀+膨胀
开操作---删除小的干扰块
morphologyEx(binary,dst,op(相关形态学操作),kernel,Point(-1,-1),1(连续开操作))
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat src = imread("C:/newword/image/14.png");
if (src.empty()) {
printf("no");
return -1;
}
Mat srv, srb,tc;
cvtColor(src, srv, COLOR_BGR2GRAY);
//threshold(srv, srb, 0, 255, THRESH_BINARY || THRESH_OTSU);
imshow("skb", srv);
Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1));
morphologyEx(srv, tc, MORPH_OPEN, kernel, Point(-1, -1), 1);
//erode(srv, srv, kernel);
imshow("skb1", tc);
dilate(srv, srv, kernel);
imshow("skb2", srv);
waitKey(0);
return 0;
}
原有结构保持不变,删除小的干扰点
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat src = imread("C:/newword/image/15.png");
if (src.empty()) {
printf("no");
return -1;
}
Mat srv, srb,tc;
cvtColor(src, srv, COLOR_BGR2GRAY);
threshold(srv, srb, 0, 255, THRESH_BINARY_INV(背景转为黑色) | THRESH_OTSU);
imshow("skb", srb);
Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(5, 5), Point(-1, -1));
morphologyEx(srb, tc, MORPH_OPEN, kernel, Point(-1, -1), 1);
//erode(srv, srv, kernel);
imshow("skb1", tc);
dilate(srv, srv, kernel);
imshow("skb2", srv);
waitKey(0);
return 0;
}
更加突出
闭操作=膨胀加腐蚀
闭操作-------填充闭合区域
morphologyEx(binary,dst,op(相关形态学操作),kernel,Point(-1,-1),1(连续开操作))
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat src = imread("C:/newword/image/14.png");
if (src.empty()) {
printf("no");
return -1;
}
Mat srv, srb,tc;
cvtColor(src, srv, COLOR_BGR2GRAY);
//threshold(srv, srb, 0, 255, THRESH_BINARY || THRESH_OTSU);
imshow("skb", srv);
Mat kernel = getStructuringElement(MORPH_RECT, Size(13, 13), Point(-1, -1));
morphologyEx(srv, tc, MORPH_CLOSE, kernel, Point(-1, -1), 1);
//erode(srv, srv, kernel);
imshow("skb1", tc);
dilate(srv, srv, kernel);
imshow("skb2", srv);
waitKey(0);
return 0;
}