本文依旧延续斯坦福的课程,讲解下如何用形态学操作来辨识钥匙。给定一把钥匙,如何从一串钥匙中匹配该钥匙,每把钥匙尾部都不同,但两幅图像中钥匙尺寸都相同。虽然还没想到它的经济价值,但是还是比较有趣的。
介绍内容:
1. 题目
2. 解题思路
3. Matlab代码及实验结果
4. 击中不击中原理及opencv实现
老外原题:
DuplicateKey Detection
In a large set of keys, we would like to use image processing to automatically detectif two keys are duplicates of each other. Two keys can be considered duplicateseven if they have different bows, as long as their blades are identical.
Design and implement an image processing algorithm to automatically determinewhich key (if any) in hw3_keys_set.png is a duplicate of the key in hw3_key.png.
题目大意就是根据Blade找匹配的钥匙【不要管bow】
解题思路:
1. 二值化钥匙串的图片key_set
2. 二值化钥匙目标图片key,并对二值图像进行剪切,只保留blade部分【这算开挂!】
3. 对二值化目标图片的前景部分【白色】,使用1*3结构基元腐蚀
4. 对于二值化目标图像的背景部分【黑色】,分别使用1*7结构基元和1*5结构基元膨胀,然后计算他们的差分图像【3,4两步其实是计算hit-miss变换的两种模板】
5. 对钥匙串图片进行hit-miss操作【算法步骤后面介绍】
6. 对5的结果,用目标图片进行膨胀。
7. 展示结果,显示钥匙串的二值化图,通过阈值改变匹配的blade部分灰度值。
Matlab源码:
clc; clear all;
% Load and binarize test image
imgTest = im2double(imread('hw3_keys_set.png'));
imgTestGray = rgb2gray(imgTest);
imgTestBin = double(imgTestGray < 0.95);
% figure(1); clf;
% imshow(imgTestBin);
% Load and binarize template image
imgTemplate = im2double(imread('hw3_key.png'));
imgTemplateGray = rgb2gray(imgTemplate);
imgTemplateBin = double(imgTemplateGray < 0.95);
% figure(2); clf;
% imshow(imgTemplateBin);
% Prepare structuring elements
imgTemplateBin = imgTemplateBin(93:end,:);
SE1 = imerode(imgTemplateBin, ones(1,3));
SE2 = imdilate(imgTemplateBin, ones(1,7)) - imdilate(imgTemplateBin, ones(1,5));
% SE2 = imdilate(imgTemplateBin, ones(1,5));
figure(3); clf;
imshow(SE1);
figure(4); clf;
imshow(SE2);
% Perform hit-miss filtering
imgHitMiss = bwhitmiss(imgTestBin, SE1, SE2);
imgHitMissDilate = imdilate(imgHitMiss, imgTemplateBin);
figure(5); clf;
alpha = 0.3;
imshow(max(imgHitMissDilate, alpha*imgTestBin));
实验效果: