1. # -*- coding: utf-8 -*-
  2. #2018-2-19 14:30:30
    #Author:Fourmi_gsj
  3. import cv2
  4. import numpy as np
  5. import pylab as pl
  6. from PIL import Image
  7. import skimage.io as io
  8. from skimage import data_dir,data,filters,color,morphology
  9. import matplotlib.pyplot as plt
  10. from math import exp,floor
  11. import os
  12. #"/home/fourmi/桌面/fourmi/DRIVE/test/images"
  13. PICTURE_PATH="/home/fourmi/桌面/fourmi/DRIVE/training/images"
  14. #"/home/fourmi/桌面/fourmi/DRIVE/training/1st_manual"
  15. PICTURE_PATH0 = "/home/fourmi/桌面/fourmi/DRIVE/test/1st_manual"
  16. "*********************第一部分特征提取(图像处理)***************************************"
  17. def load_image():
  18.  
  19. cv2.imshow("original",img)
  20. gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
  21. return img,gray
  22.  
  23. def get_Image():#循环读取对应文件的图片
  24. for i in range(21,41):
  25. path = PICTURE_PATH+"/"+str(i)+"_"+"training"+".tif"
  26. path0 = PICTURE_PATH0+"/"+str(i)+"_"+"manual1"+".gif"
  27. #Gabor(path)
  28. #img = cv2.imread(path0)
  29. img = ImageToMatrix(path0)
  30. gray = img
  31. #gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  32. #SIFT(img,gray)
  33. #SURF(img,gray)
  34. #cv2.waitKey(0)
  35. return path,path0,img,gray
  36.  
  37. def Gabor(img):
  38. #using Gabor
  39. real,imag = filters.gabor(img,frequency = 1)
  40. return real
  41. """
  42. plt.figure('GABOR')
  43. plt.subplot(121)
  44. plt.imshow(img0)
  45. plt.subplot(122)
  46. plt.imshow(real,plt.cm.gray)
  47. #plt.figure('the imag')
  48. #plt.imshow(imag,plt.cm.gray)
  49. plt.show()
  50. """
  51.  
  52. def Hessian(img):
  53. #using Hessian
  54. # img0 = io.imread(path)
  55.  
  56. real = filters.hessian(img, scale_range=(1, 10), scale_step=0.05, beta1=0.04, beta2=0.04)
  57. return real
  58. """
  59. plt.figure('Hessian')
  60. plt.imshow(real)
  61. plt.show()
  62. """
  63.  
  64. def SIFT(img,gray):
  65. #using SIFT
  66. sift = cv2.xfeatures2d.SIFT_create()
  67. keypoints, descriptor = sift.detectAndCompute(gray,None)
  68.  
  69. cv2.drawKeypoints(image = img,
  70. outImage = img,
  71. keypoints = keypoints,
  72. flags = cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS,
  73. color = (51,163,236))
  74. cv2.imshow("SIFT",img)
  75. cv2.waitKey(0)
  76.  
  77. def SURF(img,gray):
  78. #using SURF
  79. surf = cv2.xfeatures2d.SURF_create()
  80. keypoints, descriptor = surf.detectAndCompute(gray,None)
  81.  
  82. cv2.drawKeypoints(image = img,
  83. outImage = img,
  84. keypoints = keypoints,
  85. flags = cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS,
  86. color = (51,163,236))
  87. real = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  88. cv2.imshow("SURF",real)
  89. cv2.waitKey(0)
  90. return real
  91.  
  92. def Morphology(img):
  93. #using Morphology
  94. kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3, 3))
  95. eroded = cv2.erode(img,kernel)
  96. dilated = cv2.dilate(img,kernel)
  97.  
  98. #NpKernel = np.uint8(np.ones((3,3)))
  99. #Nperoded = cv2.erode(img,NpKernel)
  100. #cv2.imshow("Eroded by NumPy kernel",Nperoded);
  101.  
  102. kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5, 5))
  103. closed = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
  104. opened = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
  105. cv2.imshow("Close Image",closed);
  106. cv2.imshow("Open Image", opened);
  107. return eroded ,opened,closed,dilated
  108.  
  109. def GaussianBlurSize(GaussianBlur_size):
  110. #using GaussianBlurSize
  111. global KSIZE
  112. KSIZE = GaussianBlur_size * 2 +3
  113. print KSIZE, SIGMA
  114. dst = cv2.GaussianBlur(gray, (KSIZE,KSIZE), SIGMA, KSIZE)
  115. cv2.imshow(window_name,dst)
  116.  
  117. def GaussianBlurSigma(GaussianBlur_sigma):
  118. #using GaussianBlurSigma
  119. global SIGMA
  120. SIGMA = GaussianBlur_sigma/10.0
  121. print KSIZE, SIGMA
  122. dst = cv2.GaussianBlur(gray, (KSIZE,KSIZE), SIGMA, KSIZE)
  123. cv2.imshow(window_name,dst)
  124.  
  125. def window_gaussian():
  126. SIGMA = 1
  127. KSIZE = 15
  128. GaussianBlur_size = 1
  129. GaussianBlur_sigma = 15
  130. max_value = 300
  131. max_type = 6
  132. window_name = "GaussianBlurS Demo"
  133. trackbar_size = "Size*2+3"
  134. trackbar_sigema = "Sigma/10"
  135. cv2.namedWindow(window_name)
  136. cv2.createTrackbar( trackbar_size, window_name, \
  137. GaussianBlur_size, max_type, GaussianBlurSize )
  138. cv2.createTrackbar( trackbar_sigema, window_name, \
  139. GaussianBlur_sigma, max_value, GaussianBlurSigma )
  140. GaussianBlurSize(1)
  141. GaussianBlurSigma(15)
  142. cv2.waitKey(0)
  143.  
  144. def Frangi(img):
  145. #using Frangi
  146. real = filters.frangi(img, scale_range=(1, 10), scale_step=0.05, beta1=0.04, beta2=0.04, black_ridges=True)
  147. return real
  148. """
  149. kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3, 3))
  150. dilated = cv2.dilate(real,kernel)
  151. eroded = cv2.erode(img,kernel)
  152. closed = cv2.morphologyEx(real, cv2.MORPH_CLOSE, kernel)
  153. opened = cv2.morphologyEx(real, cv2.MORPH_OPEN, kernel)
  154. plt.figure('FRANGI')
  155. plt.subplot(131)
  156. plt.imshow(closed)
  157. plt.subplot(132)
  158. plt.imshow(opened,plt.cm.gray)
  159. plt.subplot(133)
  160. plt.figure('FRANGI')
  161. plt.imshow(real)
  162. plt.show()
  163. """
  164.  
  165. def ImageToMatrix(path):
  166. im = Image.open(path)
  167. width,height = im.size
  168. im = im.convert("L")
  169. data = im.getdata()
  170. data = np.matrix(data,dtype='float')
  171. new_data = np.reshape(data,(height,width))
  172. new_im_data = np.uint8(np.array(new_data))
  173. return new_im_data
  174.  
  175. def MatrixToImage(data):
  176. data = data*255
  177. new_im = Image.fromarray(data.astype(np.uint8))
  178. return new_im
  179.  
  180. def get_imlist(path): #此函数读取特定文件夹下的tif/gif格式图像
  181. return [os.path.join(path,f) for f in os.listdir(path) if f.endswith('.tif')]
  182.  
  183. #以下代码看可以读取文件夹下所有文件
  184. # def getAllImages(folder):
  185. # assert os.path.exists(folder)
  186. # assert os.path.isdir(folder)
  187. # imageList = os.listdir(folder)
  188. # imageList = [os.path.abspath(item) for item in imageList if os.path.isfile(os.path.join(folder, item))]
  189. # return imageList
  190.  
  191. # print getAllImages(r"D:\\test")
  192. "***************************第二部分将图片数据保存为txt文件****************************************"
  193. def Img2Txt():
  194. #图片生成txt
  195. data = np.empty((22,1,565*584))
  196. for i in range(1,21):#20
  197. path0 = PICTURE_PATH0+"/"+str(i)+"_"+"manual1"+".gif"
  198. print path0
  199. #path = PICTURE_PATH+"/"+str(i)+"_"+"test"+".tif"
  200. #path = PICTURE_PATH+"/"+str(22)+"_"+"training"+".tif"
  201. #img = cv2.imread(path)
  202. img = ImageToMatrix(path0)
  203. #img[:,:,2] = 0
  204. #gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  205. #real = gray
  206. #real = Morphology(gray)
  207. #real = Gabor(gray)
  208. #real = Frangi(gray)
  209. #real = Hessian(gray)
  210. img_ndarray=np.asarray(img)
  211. #img_ndarray=np.asarray(real,dtype='float64') #将图像转化为数组并将像素转化到0-1之间
  212. #data[i]=np.ndarray.flatten(img_ndarray*10) #将图像的矩阵形式转化为一维数组保存到data中
  213. #A=np.array(data[i]).reshape(565*584,1)
  214. #pl.savetxt('test_label'+str(i)+'.txt',A,fmt ="%.00f") #将矩阵保存到txt文件中
  215.  
  216. def ImgSplit(path):
  217. img =cv2.imread(path)
  218. b,g,r = cv2.split(img)
  219. cv2.imshow('Red',r)
  220. cv2.imshow('Green',g)
  221. cv2.imshow('Blue',b)
  222. cv2.waitKey(0)
  223. return g#同时获得绿色通道图片
  224.  
  225. def featureExtract(path):
  226. g = ImgSplit(path)#图片的通道分离
  227. fra = Frangi(g)
  228. hes = Hessian(g)
  229. ga = Gabor(g)
  230. eroded ,opened,closed,dilated = Morphology(g)
  231. cv2.imshow("Eroded Image",eroded);
  232. cv2.imshow("Dilated Image",dilated);
  233. cv2.imshow("Origin", g)
  234. cv2.imshow('Frangi',fra)
  235. cv2.imshow('Hessian',hes)
  236. cv2.imshow('Gabor',ga)
  237. cv2.waitKey(0)
  238. if __name__=='__main__':
  239. path = PICTURE_PATH+"/"+str(22)+"_"+"training"+".tif"
  240. featureExtract(path)

图像特征的提取(gaussian,gabor,frangi,hessian,Morphology...)及将图片保存为txt文件的更多相关文章

  1. 【Python图像特征的音乐序列生成】一个更科学的图片分类参考方法,以及一个看起来很好用的数据集

    数据集地址:http://www.imageemotion.org/ 论文地址:http://www.doc88.com/p-1905670442096.html

  2. 原来CNN是这样提取图像特征的。。。

    对于即将到来的人工智能时代,作为一个有理想有追求的程序员,不懂深度学习(Deep Learning)这个超热的领域,会不会感觉马上就out了?作为机器学习的一个分支,深度学习同样需要计算机获得强大的学 ...

  3. VGG16提取图像特征 (torch7)

    VGG16提取图像特征 (torch7) VGG16 loadcaffe torch7 下载pretrained model,保存到当前目录下 th> caffemodel_url = 'htt ...

  4. CNN基础二:使用预训练网络提取图像特征

    上一节中,我们采用了一个自定义的网络结构,从头开始训练猫狗大战分类器,最终在使用图像增强的方式下得到了82%的验证准确率.但是,想要将深度学习应用于小型图像数据集,通常不会贸然采用复杂网络并且从头开始 ...

  5. 第三讲_图像特征与描述Image Feature Descriptor

    第三讲_图像特征与描述Image Feature Descriptor 概要 特征提取方法 直方图 对图片数据/特征分布的一种统计:对不同量进行直方图统计:可以表示灰度,颜色,梯度,边缘,形状,纹理, ...

  6. 特征点提取之Harris角点提取法

    1. 特征点提取的意义 2.角点 3. Harris角点检測的基本原理 4.Harris角点检測算法的步骤 5.Harris角点提取算法设计 <span style="font-siz ...

  7. OpenCV计算机视觉学习(13)——图像特征点检测(Harris角点检测,sift算法)

    如果需要处理的原图及代码,请移步小编的GitHub地址 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/ComputerVisionPractice 前言 ...

  8. 【图像算法】图像特征:GLCM灰度共生矩阵,纹理特征

    [图像算法]图像特征:GLCM SkySeraph Aug 27th 2011  HQU Email:zgzhaobo@gmail.com    QQ:452728574 Latest Modifie ...

  9. 机器学习进阶-图像特征sift-SIFT特征点 1.cv2.xfeatures2d.SIFT_create(实例化sift) 2. sift.detect(找出关键点) 3.cv2.drawKeypoints(画出关键点) 4.sift.compute(根据关键点计算sift向量)

    1. sift = cv2.xfeatures2d.SIFT_create() 实例化 参数说明:sift为实例化的sift函数 2. kp = sift.detect(gray, None)  找出 ...

随机推荐

  1. 解决微信小程序使用switchTab跳转后页面不刷新的问题

    wx.switchTab({ url: '../index/index', success: function(e) { var page = getCurrentPages().pop(); if ...

  2. QR 编码原理(三)

    一.日本汉字(KANJI)是两个字节表示的字符码,编码的方式是将其转换为13字节的二进制码制. 转换步骤为: 1.对于JIS值为8140(hex) 到9FFC(hex)之间字符: a)将待转换的JIS ...

  3. Epoll模型【转】

    转自:https://www.cnblogs.com/tianhangzhang/p/5295972.html 相比于select,epoll最大的好处在于它不会随着监听fd数目的增长而降低效率.因为 ...

  4. 不允许lseek文件 | nonseekable_open()【转】

    转自:https://blog.csdn.net/gongmin856/article/details/8273545 使用数据区时,可以使用 lseek 来往上往下地定位数据.但像串口或键盘一类设备 ...

  5. python打印朱莉娅集合

    # -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt # 複素数列の計算回数を出力する関数loopmax ...

  6. C++中for_each的应用

    C++中for_each的应用   for each语法是方便的,也是很自然的,这也是为什么很多语言都有这样的语法,就我所知,包括java(jdk5.0以上),python,php,asp.net等语 ...

  7. DHCP Server (推荐使用Windows)

    一些小的服务 windows做的比linux好 DHCP服务概述: 名称:DHCP (Dynamic Host Configuration Protocol --动态主机配置协议) 功能:是一个局域网 ...

  8. 打造一个上传图片到图床利器的插件(Mac版 开源)

    写markdown博客如何将截图快速上传到图床--记一个工具插件的实现(windows版 开源)(2017-05-31 20:23) 打造一个上传图片到图床利器的插件 鉴于写博客截图手动上传到图床的步 ...

  9. ubuntu 远程登录错误

    利用  ubuntu 16.04 自带功能远程登录到同事的电脑时,提示如下错误: This file server type is not recognized 百度一下,得知,缺少必要的文件,安装后 ...

  10. ansible笔记(5):常用模块之文件操作(二)

    ansible笔记():常用模块之文件操作(二) 文件操作类模块 find模块 find模块可以帮助我们在远程主机中查找符合条件的文件,就像find命令一样. 此处我们介绍一些find模块的常用参数, ...