#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <highgui.h>
using namespace std;
using namespace cv;
Mat src, erosion_dst, dilation_dst;
int erosion_elem = 0;
int erosion_size = 0;
int dilation_elem = 0;
int dilation_size = 0;
int const max_elem = 2;
int const max_kernel_size = 21;
void Erosion( int, void* );
void Dilation( int, void* );
int main(int argc, char* argv[])
{
src = imread("lena.jpg");
if ( !src.data ) { return 0;}
char* name1 = "Erosion Demo";
char* name2 = "Dilation Demo";
namedWindow( name1, CV_WINDOW_AUTOSIZE );
namedWindow( name2, CV_WINDOW_AUTOSIZE );
cvMoveWindow( name2, src.cols, 0 ); //设定窗口的位置,貌似没什么影响
//创建腐蚀进度条
createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", name1, &erosion_elem, max_elem, Erosion ); //内核选择进度条,有三种选择
createTrackbar( "Kernel size:\n 2n +1", name1 ,&erosion_size, max_kernel_size, Erosion ); //窗口大小进度条选择
//创建膨胀进度条
createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", name2, &dilation_elem, max_elem, Dilation );
createTrackbar( "Kernel size:\n 2n +1", name2, &dilation_size, max_kernel_size, Dilation );
//默认开始
Erosion( 0, 0 );
Dilation( 0, 0 );
waitKey(0);
return 0;
}
//腐蚀函数
void Erosion( int, void* ){
int erosion_type;
if ( erosion_elem == 0 )
{
erosion_type = MORPH_RECT;
}
else if ( erosion_elem == 1 )
{
erosion_type = MORPH_CROSS;
}
else if ( erosion_elem == 2 )
{
erosion_type = MORPH_ELLIPSE;
}
Mat element = getStructuringElement( erosion_type, Size( 2*erosion_size + 1, 2*erosion_size+1 ), Point( erosion_size, erosion_size ) );
erode( src, erosion_dst, element);
imshow( "Erosion Demo" , erosion_dst ); //不明白这里换成name1就不行,不知道为什么,不都是main函数中定义的东西么?
}
//膨胀函数
void Dilation( int, void* ){
int dilation_type;
if ( dilation_elem ==0 )
{
dilation_type = MORPH_RECT;
}
else if ( dilation_elem == 1 )
{
dilation_type = MORPH_CROSS;
}
else if ( dilation_elem == 2 )
{
dilation_type = MORPH_ELLIPSE;
}
Mat element = getStructuringElement( dilation_type, Size( 2*dilation_size + 1, 2*dilation_size+1 ), Point( dilation_size, dilation_size ) );
dilate( src, dilation_dst, element);
imshow( "Dilation Demo", dilation_dst );
}
图像的腐蚀和膨胀------学习记录(9)
最新推荐文章于 2023-11-20 06:39:06 发布