本文将从图像的预处理部分进行叙述,包括图片的读入,模板匹配,灰度化,中值滤波四个部分。
图片读入
为保证后续对图片的处理方便,这里的初步设计是希望在主程序入口对程序进行操作,故提前定义如窗口名,文件名等信息(图片的索引是在与cpp文件的同文件夹下),代码如下:
// Define Window Name
char window_name[] = "Demo";
// Define File Path
char src_image_path[] = "11.jpg";
char template_path[] = "05.jpg";
// Create a named window with the name of the file
cvNamedWindow(window_name);
// Define Variables
Mat src_img, template_img, midd_img, midd_line_img,gray_img, dst_img;
// Load the image from the given file name
src_img = imread(src_image_path, 1);
template_img = imread(template_path, 1);
namedWindow(window_name, CV_WINDOW_NORMAL);
moveWindow(window_name, 100, 100);
imshow(window_name, src_img);
waitKey(0);
由于后续将使用模板匹配的方法,故在定义时就将模板预先读入进来。
模板匹配
模板匹