简单了解二值图像的形态学操作(并没有用二值图像)
了解一个基础API,返回指定形状和尺寸的结构元素,这里用于指示膨胀和腐蚀用到的核。
CV_EXPORTS_W Mat getStructuringElement(int shape, Size ksize, Point anchor = Point(-1,-1));
高值扩散,用指定结构遍历图像,计算邻域内最高值赋予当前像素。
CV_EXPORTS_W void dilate( InputArray src, OutputArray dst, InputArray kernel,
Point anchor = Point(-1,-1), int iterations = 1,
int borderType = BORDER_CONSTANT,
const Scalar& borderValue = morphologyDefaultBorderValue() );
src = imread("girl6.jpg");
if (!src.data)
{
printf("can not loading data!\n");
return -1;
}
Mat element = getStructuringElement(MORPH_RECT, Size(2 * StructElement_Size + 1, 2 * StructElement_Size + 1), Point(StructElement_Size, StructElement_Size));
dilate(src, dst_dilate, element);
imshow("dst_dilate", dst_dilate);
CV_EXPORTS_W void erode( InputArray src, OutputArray dst, InputArray kernel,
Point anchor = Point(-1,-1), int iterations = 1,
int borderType = BORDER_CONSTANT,
const Scalar& borderValue = morphologyDefaultBorderValue() );
src = imread("girl6.jpg");
if (!src.data)
{
printf("can not loading data!\n");
return -1;
}
Mat element = getStructuringElement(MORPH_RECT, Size(2 * StructElement_Size + 1, 2 * StructElement_Size + 1), Point(StructElement_Size, StructElement_Size));
erode(src, dst_erode, element);
imshow("dst_erode", dst_erode);
#include
#include
using namespace std;
using namespace cv;
Mat src, dst;
int mo = 0;
int StructElement_Size = 3;
void Process();
void setmo(int, void*);
void setStructElement_Size(int, void*);
int main() {
src = imread("girl6.jpg");
if (!src.data)
{
printf("can not loading data!\n");
return -1;
}
namedWindow("Input", WINDOW_AUTOSIZE);
namedWindow("Output", WINDOW_AUTOSIZE);
createTrackbar("erode/dilate","Output",&mo,1,setmo,NULL);
createTrackbar("StructElement Size", "Output", &StructElement_Size, 10, setStructElement_Size, NULL);
imshow("Input", src);
waitKey(0);
return -1;
}
void Process() {
Mat element = getStructuringElement(MORPH_RECT, Size(2 * StructElement_Size + 1, 2 * StructElement_Size + 1), Point(StructElement_Size, StructElement_Size));
if (mo == 1)
{
dilate(src, dst, element);//标记为1 膨胀
}
else
{
erode(src, dst, element);//否则腐蚀
}
imshow("Output",dst);
return;
}
void setmo(int, void*) {
Process();
return;
}
void setStructElement_Size(int, void*) {
Process();
return;
}