Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- from matplotlib import pyplot as plt
- img = cv2.imread('80nm_updated/4_2.PNG', cv2.IMREAD_GRAYSCALE)
- mask = np.zeros(img.shape, dtype=np.uint8)
- h = mask.shape[0]
- w = mask.shape[1]
- for y in range(0, h):
- for x in range(0, w):
- if x > 18 and y > 18 and x < 30 and y < 30: mask[y, x] = 255
- clahe = cv2.createCLAHE(clipLimit=15, tileGridSize=(5, 5))
- clahe2 = cv2.createCLAHE(clipLimit=25, tileGridSize=(1, 1))
- kernal = np.ones((5, 5), np.uint8)
- median = cv2.GaussianBlur(img, (3, 3), 3) #
- heq = median
- median2 = cv2.medianBlur(heq, 3)
- heq2 = clahe2.apply(median2)
- median3 = cv2.GaussianBlur(heq2, (3, 3), 5)
- median3 = cv2.medianBlur(median3, 3)
- median3 = clahe.apply(median3)
- _, thersh = cv2.threshold(median3, 228, 255, cv2.THRESH_BINARY)
- maskedThresh = cv2.bitwise_and(thersh,thersh,mask=mask)
- nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(maskedThresh)
- sizes = stats[1:, -1]; nlabels = nlabels - 1
- min_size = 5
- filteredImg = np.zeros((labels.shape))
- for i in range(0, nlabels):
- if sizes[i] >= min_size:
- filteredImg[labels == i + 1] = 255
- titles = ['image','median','Heq','median2','heq2','median3','threshold','Final']
- images = [img,median,heq,median2,heq2,median3,maskedThresh,filteredImg]
- n_white_pix = np.sum(filteredImg == 255)
- print("Positive" if n_white_pix>1 else "Negative")
- for i in range(len(images)):
- plt.subplot(2, 4, i+1), plt.imshow(images[i], 'gray')
- plt.title(titles[i])
- plt.xticks([]),plt.yticks([])
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement