Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4.  
  5.  
  6. img = cv2.imread('80nm_updated/4_2.PNG', cv2.IMREAD_GRAYSCALE)
  7. mask = np.zeros(img.shape, dtype=np.uint8)
  8.  
  9. h = mask.shape[0]
  10. w = mask.shape[1]
  11.  
  12. for y in range(0, h):
  13. for x in range(0, w):
  14. if x > 18 and y > 18 and x < 30 and y < 30: mask[y, x] = 255
  15.  
  16. clahe = cv2.createCLAHE(clipLimit=15, tileGridSize=(5, 5))
  17.  
  18. clahe2 = cv2.createCLAHE(clipLimit=25, tileGridSize=(1, 1))
  19.  
  20. kernal = np.ones((5, 5), np.uint8)
  21.  
  22. median = cv2.GaussianBlur(img, (3, 3), 3) #
  23. heq = median
  24. median2 = cv2.medianBlur(heq, 3)
  25. heq2 = clahe2.apply(median2)
  26. median3 = cv2.GaussianBlur(heq2, (3, 3), 5)
  27. median3 = cv2.medianBlur(median3, 3)
  28.  
  29. median3 = clahe.apply(median3)
  30.  
  31. _, thersh = cv2.threshold(median3, 228, 255, cv2.THRESH_BINARY)
  32.  
  33. maskedThresh = cv2.bitwise_and(thersh,thersh,mask=mask)
  34.  
  35. nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(maskedThresh)
  36. sizes = stats[1:, -1]; nlabels = nlabels - 1
  37. min_size = 5
  38. filteredImg = np.zeros((labels.shape))
  39. for i in range(0, nlabels):
  40. if sizes[i] >= min_size:
  41. filteredImg[labels == i + 1] = 255
  42.  
  43. titles = ['image','median','Heq','median2','heq2','median3','threshold','Final']
  44. images = [img,median,heq,median2,heq2,median3,maskedThresh,filteredImg]
  45.  
  46. n_white_pix = np.sum(filteredImg == 255)
  47. print("Positive" if n_white_pix>1 else "Negative")
  48.  
  49.  
  50. for i in range(len(images)):
  51. plt.subplot(2, 4, i+1), plt.imshow(images[i], 'gray')
  52. plt.title(titles[i])
  53. plt.xticks([]),plt.yticks([])
  54.  
  55. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement