def PreHamor2(src): '''Preprocess image of Hamorrhage by morphology. ''' print 'Preprocessing...' img=src.astype(float) b,g,r = cv2.split(img) kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(20, 20)) # eroded = cv2.erode(g,kernel) # dilated = cv2.dilate(eroded,kernel) blackhat = cv2.morphologyEx(g, cv2.MORPH_BLACKHAT, kernel) tmp = g-blackhat*5 dst = g-blackhat*5 tmp.clip(0,255) dst.clip(0,255) f_dst = createFlatView(dst) f_src = createFlatView(src) #The background area must be removed for i in xrange(f_dst.size): if f_dst[i]<30 and (f_src[3*i]>10 or f_src[3*i+1]>10 or f_src[3*i+2]>10): f_dst[i]=80 else: f_dst[i]=0 # dst = dst.astype(np.uint8) # dst = cv2.medianBlur(dst,3) # # tmp_xx=cv2.filter2D(tmp,-1, np.array([[0,0,0],[0,1,-1],[0,-1,1]])) # beta=0 # alpha=30 # tmp_xx = 255/(1+np.exp(-(tmp_xx-beta)/alpha)) # tmp_xy=cv2.filter2D(tmp,-1, np.array([[0,0,0],[0,1,-1],[0,-1,1]])) # beta=0 # alpha=30 # tmp_xy = 255/(1+np.exp(-(tmp_xy-beta)/alpha)) # tmp_yy=cv2.filter2D(tmp,-1, np.array([[0,1,0],[0,-2,0],[0,1,0]])) # beta=0 # alpha=30 # tmp_yy = 255/(1+np.exp(-(tmp_yy-beta)/alpha)) featureImg = np.zeros(img.shape,img.dtype) cv2.merge((tmp,g,tmp),featureImg) tmpImg = np.zeros(img.shape,img.dtype) cv2.merge((tmp,tmp,tmp),tmpImg) return featureImg,dst,tmpImg
def PreHamor(src): '''Preprocess image of Hamorrhage by morphology. ''' print 'Preprocessing...' img=src.astype(float) b,g,r = cv2.split(img) kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(20, 20)) # eroded = cv2.erode(g,kernel) # dilated = cv2.dilate(eroded,kernel) blackhat = cv2.morphologyEx(g, cv2.MORPH_BLACKHAT, kernel) tmp = g-blackhat*5 dst = g-blackhat*5 # blackhat = cv2.morphologyEx(dst, cv2.MORPH_BLACKHAT, kernel) # tmp = g-blackhat # wsize=img.shape[0]/30 # if wsize%2 is 0: # wsize=wsize+1 # # tmp=tmp.clip(0,255) # tmp1 = tmp.astype(np.uint8) # bgEst = cv2.medianBlur(tmp1,wsize) # # dst=(bgEst-tmp).clip(0,255) # # f_dst = createFlatView(dst) # for i in xrange(f_dst.size): # if f_dst[i]<20: # f_dst[i]=0 # dst=dst*5 dst.clip(0,255) f_dst = createFlatView(dst) f_src = createFlatView(src) #The background area must be removed for i in xrange(f_dst.size): if f_dst[i]<15 and (f_src[3*i]>10 or f_src[3*i+1]>10 or f_src[3*i+2]>10): f_dst[i]=80 else: f_dst[i]=0 # beta=10 # alpha=-0.05 # dst = 255/(1+np.exp(-(tmp-beta)/alpha)) dst = dst.astype(np.uint8) dst = cv2.medianBlur(dst,3) return dst
def ExudateSegment(originImg, ResultPath, presegPath=''): presegImg = PreprocessingModule.PreExudate(originImg) presegImg = presegImg.astype(np.uint8) presegImg = cv2.medianBlur(presegImg, 3) f_presegImg = createFlatView(presegImg) for i in xrange(f_presegImg.size): if f_presegImg[i]>100: f_presegImg[i]=80 else: f_presegImg[i]=0 #cv2.imwrite(presegPath,presegImg) CNNSeg = CNNSegModule.ExudateCNNSegNetwork('./ConfExu.txt') result=CNNSeg.segment(originImg, presegImg, 128) #connect two Images showImg = np.full((result.shape[0],result.shape[1]*2+3,result.shape[2]), 255, result.dtype) showImg[0:originImg.shape[0],0:originImg.shape[1],:]=originImg[:,:,:] showImg[0:result.shape[0],originImg.shape[1]+3:originImg.shape[1]+result.shape[1]+3,:]= result[:,:,:] cv2.imwrite(ResultPath,showImg)
def apply(self, src, dst): """Apply the filter with a BGR or gray source/destination.""" srcFlatView = utils.createFlatView(src) dstFlatView = utils.createFlatView(dst) utils.applyLookupArray(self._vLookupArray, srcFlatView, dstFlatView)
def apply(self, src, dest): flatSrc = utils.createFlatView(src) flatDest = utils.createFlatView(dest) utils.searchLookupTable(self._lookupArray, flatSrc, flatDest)
def HamorSegment(originImg, ResultPath, presegPath=''): # cv2.imwrite("./temp_ori.jpg",originImg) featureImg,presegImg,tmpImg = PreprocessingModule.PreHamor2(originImg) # presegImg = presegImg.astype(np.uint8) # presegImg = cv2.medianBlur(presegImg, 3) # # f_presegImg = createFlatView(presegImg) # # for i in xrange(f_presegImg.size): # if f_presegImg[i]>100: # f_presegImg[i]=80 # else: # f_presegImg[i]=0 ###Preprocess by Jinpeng # originImg1 = cv2.imread("/home/linhuangjing/Desktop/Retina_Data/Group-1/DR/DR_102.jpg") cv2.imwrite("./temp_ori.jpg",originImg) originImg2 = cv2.imread("./temp_ori.jpg") # cv2.imwrite("/home/linhuangjing/Desktop/Retina_Data/Group-1/results/strange.jpg",originImg1-originImg2) import JPModule JPModule.Preprocess("./temp_ori.jpg","./temp_jppre.jpg") JPImg=cv2.imread("./temp_jppre.jpg") b,g,r = cv2.split(JPImg) cv2.imwrite("/home/linhuangjing/Desktop/Retina_Data/Group-1/results/MyPre_102.jpg",presegImg) cv2.imwrite("/home/linhuangjing/Desktop/Retina_Data/Group-1/results/JPPre_102.jpg",JPImg) #Mask two images f_g = createFlatView(g) f_presegImg = createFlatView(presegImg) for i in xrange(f_presegImg.size): if f_presegImg[i]>f_g[i]: f_presegImg[i] = f_g[i] cv2.imwrite("/home/linhuangjing/Desktop/Retina_Data/Group-1/results/Pre_102.jpg",presegImg) preImg3 = np.zeros(originImg.shape,presegImg.dtype) cv2.merge((presegImg,presegImg,presegImg),preImg3) showTmpImg = np.full((originImg.shape[0],originImg.shape[1]*2+3,originImg.shape[2]), 255, tmpImg.dtype) showTmpImg[0:preImg3.shape[0],0:preImg3.shape[1],:]=preImg3[:,:,:] showTmpImg[0:tmpImg.shape[0],preImg3.shape[1]+3:preImg3.shape[1]+tmpImg.shape[1]+3,:]= tmpImg[:,:,:] cv2.imwrite(presegPath,showTmpImg) featureImg = featureImg.clip(0,255) cv2.imwrite("/home/linhuangjing/Desktop/featureImg.jpg",featureImg) CNNSeg = CNNSegModule.HamorCNNSegNetwork('./ConfHamor.txt') result=CNNSeg.segment(featureImg, presegImg, 1) #connect two Images # showImg = np.full((result.shape[0],result.shape[1]*2+3,result.shape[2]), 255, result.dtype) # showImg[0:originImg.shape[0],0:originImg.shape[1],:]=originImg[:,:,:] # showImg[0:result.shape[0],originImg.shape[1]+3:originImg.shape[1]+result.shape[1]+3,:]= result[:,:,:] result=result.astype(float) b,g,r = cv2.split(result) a = np.zeros(g.shape,g.dtype) f_g = createFlatView(g) f_a = createFlatView(a) f_r = createFlatView(r) f_b = createFlatView(b) for i in xrange(f_g.size): if f_g[i]>20 or f_r[i]>20 or f_b[i]>20: f_a[i]=255 showImg = np.zeros((result.shape[0],result.shape[1],4),g.dtype) cv2.merge((b,g,r,a),showImg) cv2.imwrite(ResultPath,showImg)
def PreExudate(src): '''Preprocess image of exudates by estimate the Background via a Median Filter, Which is used for exudates preprocessing. ''' # InImage=cv2.cvtColor(src,cv2.cv.CV_BGR2GRAY) print 'Preprocessing...' img=src.astype(float) b,g,r = cv2.split(img) InImage = np.zeros(g.shape,g.dtype) cv2.addWeighted(b,0.333333,r,0.333333,0,InImage) cv2.addWeighted(g,0.333333,InImage,1.0,0,InImage) # cv2.addWeighted(b,0.5,r,0.5,0,InImage) wsize=img.shape[0]/30 if wsize%2 is 0: wsize=wsize+1 InImage = InImage.astype(np.uint8) bgEst = cv2.medianBlur(InImage,wsize) bgMask = np.zeros(bgEst.shape,bgEst.dtype) bgMask2 = np.zeros(bgEst.shape,bgEst.dtype) f_InImage = createFlatView(InImage) f_bgEst = createFlatView(bgEst) f_bgMask = createFlatView(bgMask) f_bgMask2 = createFlatView(bgMask2) for i in xrange(f_bgMask.size): if f_InImage[i]>f_bgEst[i]: f_bgMask[i] = f_InImage[i] f_bgMask2[i] = f_bgEst[i] else: f_bgMask[i] = f_bgEst[i] f_bgMask2[i] = f_InImage[i] im1 = bgMask-bgMask2 im2 = g-bgEst dst = np.zeros(im2.shape,im2.dtype) f_im1 = createFlatView(im1) f_im2 = createFlatView(im2) f_dst = createFlatView(dst) for i in xrange(f_im1.size): if f_im1[i]<8: f_im1[i] = 0 if f_im2[i]<6: f_im2[i] = 0 if f_im1[i]<f_im2[i]: f_dst[i] = f_im1[i] else: f_dst[i] = f_im2[i] if f_dst[i]>5: f_dst[i] = 255 else: f_dst[i] = 0 return dst
def apply(self, src, dst): ''' Apply the filter with BGR or gray source/dest''' srcFlatView = utils.createFlatView(src) dstFlatView = utils.createFlatView(dst) utils.applyLookupArray(self._vLookupArray, srcFlatView, dstFlatView)