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

# -*- coding: utf-8 -*-
#2018-2-19 14:30:30
#Author:Fourmi_gsj import cv2 import numpy as np import pylab as pl from PIL import Image import skimage.io as io from skimage import data_dir,data,filters,color,morphology import matplotlib.pyplot as plt from math import exp,floor import os #"/home/fourmi/桌面/fourmi/DRIVE/test/images" PICTURE_PATH="/home/fourmi/桌面/fourmi/DRIVE/training/images" #"/home/fourmi/桌面/fourmi/DRIVE/training/1st_manual" PICTURE_PATH0 = "/home/fourmi/桌面/fourmi/DRIVE/test/1st_manual" "*********************第一部分特征提取(图像处理)***************************************" def load_image(): cv2.imshow("original",img) gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY) return img,gray def get_Image():#循环读取对应文件的图片 for i in range(21,41): path = PICTURE_PATH+"/"+str(i)+"_"+"training"+".tif" path0 = PICTURE_PATH0+"/"+str(i)+"_"+"manual1"+".gif" #Gabor(path) #img = cv2.imread(path0) img = ImageToMatrix(path0) gray = img #gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #SIFT(img,gray) #SURF(img,gray) #cv2.waitKey(0) return path,path0,img,gray def Gabor(img): #using Gabor real,imag = filters.gabor(img,frequency = 1) return real """ plt.figure('GABOR') plt.subplot(121) plt.imshow(img0) plt.subplot(122) plt.imshow(real,plt.cm.gray) #plt.figure('the imag') #plt.imshow(imag,plt.cm.gray) plt.show() """ def Hessian(img): #using Hessian # img0 = io.imread(path) real = filters.hessian(img, scale_range=(1, 10), scale_step=0.05, beta1=0.04, beta2=0.04) return real """ plt.figure('Hessian') plt.imshow(real) plt.show() """ def SIFT(img,gray): #using SIFT sift = cv2.xfeatures2d.SIFT_create() keypoints, descriptor = sift.detectAndCompute(gray,None) cv2.drawKeypoints(image = img, outImage = img, keypoints = keypoints, flags = cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS, color = (51,163,236)) cv2.imshow("SIFT",img) cv2.waitKey(0) def SURF(img,gray): #using SURF surf = cv2.xfeatures2d.SURF_create() keypoints, descriptor = surf.detectAndCompute(gray,None) cv2.drawKeypoints(image = img, outImage = img, keypoints = keypoints, flags = cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS, color = (51,163,236)) real = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) cv2.imshow("SURF",real) cv2.waitKey(0) return real def Morphology(img): #using Morphology kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3, 3)) eroded = cv2.erode(img,kernel) dilated = cv2.dilate(img,kernel) #NpKernel = np.uint8(np.ones((3,3))) #Nperoded = cv2.erode(img,NpKernel) #cv2.imshow("Eroded by NumPy kernel",Nperoded); kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5, 5)) closed = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel) opened = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) cv2.imshow("Close Image",closed); cv2.imshow("Open Image", opened); return eroded ,opened,closed,dilated def GaussianBlurSize(GaussianBlur_size): #using GaussianBlurSize global KSIZE KSIZE = GaussianBlur_size * 2 +3 print KSIZE, SIGMA dst = cv2.GaussianBlur(gray, (KSIZE,KSIZE), SIGMA, KSIZE) cv2.imshow(window_name,dst) def GaussianBlurSigma(GaussianBlur_sigma): #using GaussianBlurSigma global SIGMA SIGMA = GaussianBlur_sigma/10.0 print KSIZE, SIGMA dst = cv2.GaussianBlur(gray, (KSIZE,KSIZE), SIGMA, KSIZE) cv2.imshow(window_name,dst) def window_gaussian(): SIGMA = 1 KSIZE = 15 GaussianBlur_size = 1 GaussianBlur_sigma = 15 max_value = 300 max_type = 6 window_name = "GaussianBlurS Demo" trackbar_size = "Size*2+3" trackbar_sigema = "Sigma/10" cv2.namedWindow(window_name) cv2.createTrackbar( trackbar_size, window_name, GaussianBlur_size, max_type, GaussianBlurSize ) cv2.createTrackbar( trackbar_sigema, window_name, GaussianBlur_sigma, max_value, GaussianBlurSigma ) GaussianBlurSize(1) GaussianBlurSigma(15) cv2.waitKey(0) def Frangi(img): #using Frangi real = filters.frangi(img, scale_range=(1, 10), scale_step=0.05, beta1=0.04, beta2=0.04, black_ridges=True) return real """ kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3, 3)) dilated = cv2.dilate(real,kernel) eroded = cv2.erode(img,kernel) closed = cv2.morphologyEx(real, cv2.MORPH_CLOSE, kernel) opened = cv2.morphologyEx(real, cv2.MORPH_OPEN, kernel) plt.figure('FRANGI') plt.subplot(131) plt.imshow(closed) plt.subplot(132) plt.imshow(opened,plt.cm.gray) plt.subplot(133) plt.figure('FRANGI') plt.imshow(real) plt.show() """ def ImageToMatrix(path): im = Image.open(path) width,height = im.size im = im.convert("L") data = im.getdata() data = np.matrix(data,dtype='float') new_data = np.reshape(data,(height,width)) new_im_data = np.uint8(np.array(new_data)) return new_im_data def MatrixToImage(data): data = data*255 new_im = Image.fromarray(data.astype(np.uint8)) return new_im def get_imlist(path): #此函数读取特定文件夹下的tif/gif格式图像 return [os.path.join(path,f) for f in os.listdir(path) if f.endswith('.tif')] #以下代码看可以读取文件夹下所有文件 # def getAllImages(folder): # assert os.path.exists(folder) # assert os.path.isdir(folder) # imageList = os.listdir(folder) # imageList = [os.path.abspath(item) for item in imageList if os.path.isfile(os.path.join(folder, item))] # return imageList # print getAllImages(r"D:\test") "***************************第二部分将图片数据保存为txt文件****************************************" def Img2Txt(): #图片生成txt data = np.empty((22,1,565*584)) for i in range(1,21):#20 path0 = PICTURE_PATH0+"/"+str(i)+"_"+"manual1"+".gif" print path0 #path = PICTURE_PATH+"/"+str(i)+"_"+"test"+".tif" #path = PICTURE_PATH+"/"+str(22)+"_"+"training"+".tif" #img = cv2.imread(path) img = ImageToMatrix(path0) #img[:,:,2] = 0 #gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #real = gray #real = Morphology(gray) #real = Gabor(gray) #real = Frangi(gray) #real = Hessian(gray) img_ndarray=np.asarray(img) #img_ndarray=np.asarray(real,dtype='float64') #将图像转化为数组并将像素转化到0-1之间 #data[i]=np.ndarray.flatten(img_ndarray*10) #将图像的矩阵形式转化为一维数组保存到data中 #A=np.array(data[i]).reshape(565*584,1) #pl.savetxt('test_label'+str(i)+'.txt',A,fmt ="%.00f") #将矩阵保存到txt文件中 def ImgSplit(path): img =cv2.imread(path) b,g,r = cv2.split(img) cv2.imshow('Red',r) cv2.imshow('Green',g) cv2.imshow('Blue',b) cv2.waitKey(0) return g#同时获得绿色通道图片 def featureExtract(path): g = ImgSplit(path)#图片的通道分离 fra = Frangi(g) hes = Hessian(g) ga = Gabor(g) eroded ,opened,closed,dilated = Morphology(g) cv2.imshow("Eroded Image",eroded); cv2.imshow("Dilated Image",dilated); cv2.imshow("Origin", g) cv2.imshow('Frangi',fra) cv2.imshow('Hessian',hes) cv2.imshow('Gabor',ga) cv2.waitKey(0) if __name__=='__main__': path = PICTURE_PATH+"/"+str(22)+"_"+"training"+".tif" featureExtract(path)
原文地址:https://www.cnblogs.com/fourmi/p/8453762.html