Pixels17.files.wordpress.com



import cv2print (cv2.__version__)import numpy as npfrom matplotlib import pyplot as pltimg = cv2.imread('73.png',0)# ip is the input imagecv2.imshow('image',img)cv2.waitKey(0)# histogram of the input imagehist = cv2.calcHist(img,[0],None,[256],[0,256])plt.hist(img.ravel(),256,[0,256])plt.title('Histogram of the image')plt.show()cv2.waitKey(0)#background removalkernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))# lesser the better, ideal is 5 for the datasetclosing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)cv2.imshow('image',closing)cv2.waitKey(0)whiteAdjusted = img/(closing*0.85)cv2.imshow('image',whiteAdjusted)cv2.waitKey(0)#global thresholdingret1,th1 = cv2.threshold(whiteAdjusted,0.8,255,cv2.THRESH_BINARY)cv2.imshow('image',th1)cv2.waitKey(0)#denoising 1th1=np.uint8(th1);# converting to uint8 datatypecv2.fastNlMeansDenoising(th1,th1,40,7,21)#3rd argument indicates the filter strength 4th argument templateWindowSize : should be odd. (recommended 7) 5th argument searchWindowSize : should be odd. (recommended 21)cv2.imshow('image',th1)cv2.waitKey(0)#denoising 2#gaussian blur(lpf)th1=np.uint8(th1);blur = cv2.GaussianBlur(th1,(3,3),0)#width and height of kernel, ideal value is 3 for the datasetcv2.imshow('image',blur)cv2.waitKey(0)#unsharp masking to sharpen the edgessharp=th1cv2.addWeighted(th1, 0.25, blur, 0.75, 0, sharp)#2nd and 4th arguments are alpha and 1- alpha which determine the weightage to be given to 1st and 3rd parametercv2.imshow('image',sharp)cv2.waitKey(0)'''#in case the image has uneven illumination morphological top-hat filtering is performed followed by thresholding,denoising and sharpening as mentioned above#Top-hat filtering can be used to remove uneven background illumination from an image with a dark background. Hence the original image is first inverted to obtain a dark background.img=255-imgcv2.imshow('image',img)cv2.waitKey(0)kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))#ideal value is 3 for the datasettophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)cv2.imshow('image',tophat)cv2.waitKey(0)tophat=255-tophatcv2.imshow('image',tophat)cv2.waitKey(0)''''''# other thresholding methods(the parameters values must be obtained through trial and error)#otsuret2,th2 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)cv2.imshow('image',th2)cv2.waitKey(0)#Adaptive Thresholding with mean weighted average#5th argument Block Size - It decides the size of neighbourhood area. 6th argument - It is just a constant which is subtracted from the mean or weighted mean calculated.th3 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 5,20)cv2.imshow('image',th3)cv2.waitKey(0)#Adaptive Thresholding with gaussian weighted averageth4 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5, 7)cv2.imshow('image',th4)cv2.waitKey(0)'''#local otsur1=np.zeros((size[0],size[1]),dtype=sharp.dtype)size=test_image.shapewindowsize_r =size[0]/5windowsize_c = size[1]/5for r in range(0,test_image.shape[0] + windowsize_r, windowsize_r): for c in range(0,test_image.shape[1] + windowsize_c, windowsize_c): window = test_image[r:r+windowsize_r,c:c+windowsize_c] ret3, th3 = cv2.threshold(window, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) #applyoing Otsu on each segment of image r1[r:r + windowsize_r, c:c + windowsize_c] = th3#Bernsen's method of thresholdingm=cv2.minMaxLoc(test_image)th=(m[0]+m[1])/2r2=numpy.zeros((size[0],size[1]))for r in range(0,test_image.shape[0],1): for c in range(0,test_image.shape[1], 1): if test_image[r,c] > th :#checking if greater than threshold r2[r,c]=1 else: r2[r,c]=0cv2.imshow('image',r2)cv2.waitKey(0)# Sauvola and Pietik?inen’s MethodMa=cv2.meanStdDev(test_image)R=max(y)-min(y)z=Ma[1]/Rth=Ma[0]*0.3*z #works well in the range [0.2,0.4]for r in range(0,test_image.shape[0],1): for c in range(0,test_image.shape[1], 1): if test_image[r,c] > th : #checking if greater than threshold r2[r,c]=1 else: r2[r,c]=0cv2.imshow('image',r2)cv2.waitKey(0)#Nick' Methodr2=numpy.zeros((size[0],size[1]))x=0for r in range(0,test_image.shape[0] + windowsize_r, windowsize_r): for c in range(0,test_image.shape[1] + windowsize_c, windowsize_c): window = test_image[r:r + windowsize_r, c:c + windowsize_c] #Each Block of image for r1 in range(0, window.shape[0], 1): for c1 in range(0, window.shape[1], 1): x=x+((window[r1,c1]*window[r1,c1])-(Ma[0]*Ma[0]))th=Ma[0]-0.7*math.sqrt(x/img.size)#Works in the range 06,0.9 ,Threshold for Nicks methisfor r in range(0,test_image.shape[0],1): for c in range(0,test_image.shape[1], 1): if test_image[r,c] > th : r2[r,c]=1 else: r2[r,c]=0cv2.imshow('image',r2)cv2.waitKey(0) ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download