图片处理之模式匹配(match_template)
来源:互联网 发布:ubuntu 16.04和优麒麟 编辑:程序博客网 时间:2024/06/02 06:40
以一张脸部皮肤切片中的毛孔作为研究对象,使用模式匹配寻找到该切片中所有的毛孔
1.选取切片,并找到匹配对象
from skimage.feature import match_template
img_gray = cv2.cvtColor(img_o,cv2.COLOR_BGR2GRAY)img_kernel = img_gray[255:295,460:500]plt.subplot(131),plt.imshow(img_o)plt.title('original'),plt.xticks([]),plt.yticks([])plt.subplot(132),plt.imshow(img_gray,'gray')plt.title('gray'),plt.xticks([]),plt.yticks([])plt.subplot(133),plt.imshow(img_kernel,'gray')plt.title('template'),plt.xticks([]),plt.yticks([])plt.show()
如上图所示,左边图为皮肤切片原图,中间为灰度图,右边为选中的匹配对象
2.match_template
result = match_template(img_gray, img_kernel, pad_input=True, mode='constant', constant_values=0)plt.subplot(121),plt.imshow(img_gray,'gray')plt.title('img_gray')plt.subplot(122),plt.imshow(result,'gray')plt.title('match_template'),plt.xticks([]),plt.yticks([])plt.show()
匹配结果如上图右所示,其中亮度较高的部分即我们所寻找的部分
接下来我们要让这张图看起来,更加明显,所以进行二值化操作,并降噪
mask = np.ones_like(result)mask[result > 0.4] = 255mask[result < 0.4] = 0plt.subplot(121),plt.imshow(mask,'gray'),plt.title('threshold')# openkernel = np.ones((2,2),np.uint8)th_mor = cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernel, iterations = 3)plt.subplot(122),plt.imshow(th_mor,'gray'),plt.title('open'),plt.xticks([]),plt.yticks([])plt.show()
如此我们已经得到毛孔的轮廓图,不过这样看是不是太抽象了,ok,那么我们就把它画到原图上看一下
3.draw contours
# 寻找轮廓 _, contour_total, _ = cv2.findContours(th_mor, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)# 绘制筛选后的轮廓img_ground = img_o.copy()cv2.drawContours(img_ground,new_contours,-1,(0,255,255),2)fig = plt.figure(figsize=(12, 9))axes = np.zeros((3, 3), dtype=np.object)axes[0, 0] = plt.subplot(231),plt.imshow(img_o),plt.title('original image')axes[0, 1] = plt.subplot(232),plt.imshow(img_ground),plt.title('contours')plt.show()
至此模式匹配的简单应用就算结束了。准确率什么的仅仅凭借我们肉眼观察是无法断定的,在此我们可以人工在原图上标注出毛孔的轮廓,然后与模式匹配结果进行比较,下面贴上作者尝试标注的图与模式匹配结果的对比(人工标注比较随意)
其中第二部分,模式匹配方法应用及二值化操作过程中,参数设置决定匹配结果。
另外,在各种方法使用过程中,发现LBP效果也挺好。
补充:上述第二部分方法是采用skimage的match_template,最近因为工作需要,又做了一份opencv的方法,具体如下:
import cv2img_match = cv2.matchTemplate(img_gray, img_kernel, cv2.TM_CCOEFF_NORMED)# 给匹配图补黑边,解决cv2匹配过程中边缘忽略问题img_match = cv2.copyMakeBorder(img_match,0,img_kernel.shape[0]-1,0,img_kernel.shape[1]-1,cv2.BORDER_CONSTANT,value=(0, 0, 0))
经检测发现,两者效果接近,skimage效果稍优。
本人邮箱:craftsman.mote@gmail.com,欢迎讨论。
阅读全文
2 0
- 图片处理之模式匹配(match_template)
- 图像处理之------匹配
- 模式匹配和文本处理
- 模式匹配之(BF KMP算法)
- Erlang基础之模式匹配(1)
- Erlang基础之模式匹配(2)
- scala 之模式匹配(八)
- 模式匹配之路
- 模式匹配之路
- vim 之 模式匹配
- KMP之模式匹配
- 模式匹配之路
- Scala之模式匹配
- Scala 之 模式匹配
- Scala之模式匹配
- 模式匹配之KMP
- 前端开发之使用base64模式处理图片
- 串处理(朴素模式匹配/KMP模式匹配)
- Js与BootStrap实现简易计算器
- 剑指offer—数组中出现次数超过一半的数字
- 浅析开源数据库MySQL架构
- The user specified as a definer ('root'@'%') does not exist
- Java关键字transient和volatile
- 图片处理之模式匹配(match_template)
- 二、Spring Cloud系列:Zipkin客户端配置
- Go语言:同个包下不同文件不能互相调用函数
- 吐槽
- HDOJ HDU 1106 排序
- MongoDB入门学习一
- 多态
- 模拟实现strcpy,strcmp,strstr,strncpy,strncmp,strcat,strlen函数
- 高效的多维空间点索引算法 — Geohash 和 Google S2