# 前處理 [hough](https://hackmd.io/Zm31qOqDSWiKz3ScXQBAQw) ## 多次處理 ```python= from os import listdir from os.path import isfile, join mypath = "C:/Users/mua89/hough/Dataset/Test/Cataract" file_names = [f for f in listdir(mypath) if isfile(join(mypath, f))] print(str(len(file_names)) + ' images loaded') # Read image as gray-scale import cv2 import numpy as np size=64 for n, file in enumerate(file_names): img = cv2.imread(mypath+"/"+file,0) h,w=img.shape if(h>128 and w>128): img = cv2.resize(img, (size, size), interpolation = cv2.INTER_AREA) img = cv2.medianBlur(img,5) cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR) circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20, param1=50,param2=30,minRadius=0,maxRadius=0) if circles is not None: circles = np.uint16(np.around(circles)) for i in circles[0,:]: # draw the outer circle cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2) # draw the center of the circle cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3) print(file) cv2.resizeWindow('image', 600,600) cv2.imshow('detected circles',cimg) cv2.waitKey(0) cv2.destroyAllWindows() ``` ## 切割照片 ```python= import cv2 import numpy as np from matplotlib import pyplot as plt bgr_img = cv2.imread('eye.jpg') # read as it is if bgr_img.shape[-1] == 3: # color image b,g,r = cv2.split(bgr_img) # get b,g,r rgb_img = cv2.merge([r,g,b]) # switch it to rgb gray_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2GRAY) else: gray_img = bgr_img img = cv2.medianBlur(gray_img, 5) cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR) circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,100, param1=100,param2=30,minRadius=10,maxRadius=65) circles = np.uint16(np.around(circles)) #print(circles) for i in circles[0,:]: # draw the outer circle cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2) #print(cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)) # draw the center of the circle cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3) plt.subplot(121),plt.imshow(rgb_img) plt.title('Input Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122),plt.imshow(cimg) plt.title('Hough Transform'), plt.xticks([]), plt.yticks([]) plt.show() #crop max_circle = max(circles[0,:], key=lambda x:x[2]) # print(max_circle) # # Create mask height,width = img.shape mask = np.zeros((height,width), np.uint8) for i in [max_circle]: cv2.circle(mask,(i[0],i[1]),i[2],(255,255,255),thickness=-1) masked_data = cv2.bitwise_and(img, img, mask=mask) _,thresh = cv2.threshold(mask,1,255,cv2.THRESH_BINARY) # Find Contour contours = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0] x,y,w,h = cv2.boundingRect(contours[0]) # Crop masked_data crop = masked_data[y:y+h,x:x+w] #Code to close Window cv2.imshow('OG',img) cv2.imshow('Cropped ROI',crop) cv2.imwrite("test1.jpg", crop) cv2.waitKey(0) cv2.destroyAllWindows() ``` ## final ```python= import cv2 import numpy as np from matplotlib import pyplot as plt from os import listdir from os.path import isfile, join import datetime mypath = "C:/Users/mua89/hough/123" file_names = [f for f in listdir(mypath) if isfile(join(mypath, f))] print(str(len(file_names)) + ' images loaded') # Read image as gray-scale size=64 start = datetime.datetime.now() for n, file in enumerate(file_names): img = cv2.imread(mypath+"/"+file,0) h,w=img.shape if(h>128 and w>128): img = cv2.resize(img, (size, size), interpolation = cv2.INTER_AREA) img = cv2.medianBlur(img,5) cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR) circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,10, param1=100,param2=30,minRadius=5,maxRadius=50) if circles is not None: circles = np.uint16(np.around(circles)) for i in circles[0,:]: # draw the outer circle cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2) # draw the center of the circle cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3) print(file) #crop max_circle = max(circles[0,:], key=lambda x:x[2]) # print(max_circle) # # Create mask height,width = img.shape mask = np.zeros((height,width), np.uint8) for i in [max_circle]: cv2.circle(mask,(i[0],i[1]),i[2],(255,255,255),thickness=-1) masked_data = cv2.bitwise_and(img, img, mask=mask) _,thresh = cv2.threshold(mask,1,255,cv2.THRESH_BINARY) # Find Contour contours = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0] x,y,w,h = cv2.boundingRect(contours[0]) # Crop masked_data crop = masked_data[y:y+h,x:x+w] #cv2.resizeWindow('image', 600,600) #cv2.imshow('detected circles',cimg) #cv2.waitKey(0) #cv2.destroyAllWindows() cv2.imwrite("C:/Users/mua89/hough/test/test"+str(n)+'.jpg' , crop) end = datetime.datetime.now() print("執行時間:", end - start) ``` ## 反光處理 - illuminationChange https://blog.csdn.net/fangyan90617/article/details/100533808 - <另外> https://blog.csdn.net/qq_43555843/article/details/102510425 ```python= import cv2 import os,shutil #找亮光位置 def create_mask(imgpath): image = cv2.imread(imgpath, cv2.IMREAD_GRAYSCALE) _, mask = cv2.threshold(image, 200, 255, cv2.THRESH_BINARY) return mask #修复图片 def xiufu(imgpath,maskpath): src_ = cv2.imread(imgpath) mask = cv2.imread(maskpath, cv2.IMREAD_GRAYSCALE) #缩放因子(fx,fy) res_ = cv2.resize(src_,None,fx=0.6, fy=0.6, interpolation = cv2.INTER_CUBIC) mask = cv2.resize(mask,None,fx=0.6, fy=0.6, interpolation = cv2.INTER_CUBIC) dst = cv2.inpaint(res_, mask, 10, cv2.INPAINT_TELEA) return dst if __name__=='__main__': rootpath = 'C:/Users/Nan/orange.jpg' masksavepath= 'C:/Users/Nan/mask.jpg' savepath = 'C:/Users/Nan/result.jpg' imgfiles = os.listdir(rootpath) for i in range(0, len(imgfiles)): path = os.path.join(rootpath, imgfiles[i]) print(imgfiles[i]) if os.path.isfile(path): if (imgfiles[i].endswith("jpg") or imgfiles[i].endswith("JPG")): maskpath =os.path.join(masksavepath, "mask_"+imgfiles[i]) cv2.imwrite(maskpath, create_mask(path)) dst=xiufu(path,maskpath) newname = 'xiufu_' + imgfiles[i].split(".")[0] cv2.imwrite(os.path.join(savepath, newname + ".jpg"), dst) shutil.copyfile(os.path.join(rootpath, imgfiles[i].split(".")[0] + ".xml"), os.path.join(savepath, newname + ".xml")) ``` ![](https://i.imgur.com/ANQiPNJ.png) ### 處理方式 https://download.csdn.net/download/weixin_38530846/15090328 - 根據 Paper,有以下做法: - 多角度拍照,重疊照片互補(可行) - 根據反射量,霧化反光區域(可行) - 物質表面特徵(暫不使用) - 調整曝光時間,使閃光處不飽和(待了解) - 醫師建議 - 建模時,刪除反光重疊到瞳孔的部分資料 - 請user重新拍照