Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- import os
- import csv
- countAll=0
- countNeg=0
- for filename in os.listdir("80nm_updated/80nm_dataset_2_mixedexperiments/80nm_dataset_2_mixedexperiments/train/pos"):
- if filename.endswith(".PNG") or filename.endswith(".png"):
- img = cv2.imread('80nm_updated/80nm_dataset_2_mixedexperiments/80nm_dataset_2_mixedexperiments/train/pos/'+filename, 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>12 and y>12 and x<36 and y<36: 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), 1)
- heq = median
- median2 = cv2.medianBlur(heq, 3)
- heq2 = clahe2.apply(median2)
- median3 = cv2.GaussianBlur(heq2, (3, 3), 1)
- median3 = cv2.medianBlur(median3, 5)
- median3 = clahe.apply(median3)
- _, thersh = cv2.threshold(median3, 230, 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 = 10
- filteredImg = np.zeros((labels.shape))
- for i in range(0, nlabels):
- if sizes[i] >= min_size:
- filteredImg[labels == i + 1] = 255
- n_white_pix = np.sum(filteredImg == 255)
- result="Positive" if n_white_pix>1 else "Negative"
- # print(result)
- countAll=countAll+1
- if n_white_pix>1:
- countNeg=countNeg+1
- else:
- continue
- msg = "train_dataset2: Positive: %f" % float(((countNeg/countAll)*100))
- msg += "%"
- print(msg)
- countAll=0
- countNeg=0
- for filename in os.listdir("80nm_updated/80nm_dataset_1_splitbyexperiment/80nm_dataset_1_splitbyexperiment/train/pos"):
- if filename.endswith(".PNG") or filename.endswith(".png"):
- img = cv2.imread('80nm_updated/80nm_dataset_1_splitbyexperiment/80nm_dataset_1_splitbyexperiment/train/pos/'+filename, 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>12 and y>12 and x<36 and y<36: 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), 1)
- heq = median
- median2 = cv2.medianBlur(heq, 3)
- heq2 = clahe2.apply(median2)
- median3 = cv2.GaussianBlur(heq2, (3, 3), 1)
- median3 = cv2.medianBlur(median3, 5)
- median3 = clahe.apply(median3)
- _, thersh = cv2.threshold(median3, 230, 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 = 10
- filteredImg = np.zeros((labels.shape))
- for i in range(0, nlabels):
- if sizes[i] >= min_size:
- filteredImg[labels == i + 1] = 255
- n_white_pix = np.sum(filteredImg == 255)
- result="Positive" if n_white_pix>1 else "Negative"
- # print(result)
- countAll=countAll+1
- if n_white_pix>1:
- countNeg=countNeg+1
- else:
- continue
- msg = "train_dataset1: Positive: %f" % float(((countNeg/countAll)*100))
- msg += "%"
- print(msg)
- countAll=0
- countNeg=0
- for filename in os.listdir("80nm_updated/80nm_dataset_2_mixedexperiments/80nm_dataset_2_mixedexperiments/train/neg"):
- if filename.endswith(".PNG") or filename.endswith(".png"):
- img = cv2.imread('80nm_updated/80nm_dataset_2_mixedexperiments/80nm_dataset_2_mixedexperiments/train/neg/'+filename, 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>12 and y>12 and x<36 and y<36: 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), 1)
- heq = median
- median2 = cv2.medianBlur(heq, 3)
- heq2 = clahe2.apply(median2)
- median3 = cv2.GaussianBlur(heq2, (3, 3), 1)
- median3 = cv2.medianBlur(median3, 5)
- median3 = clahe.apply(median3)
- _, thersh = cv2.threshold(median3, 230, 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 = 10
- filteredImg = np.zeros((labels.shape))
- for i in range(0, nlabels):
- if sizes[i] >= min_size:
- filteredImg[labels == i + 1] = 255
- n_white_pix = np.sum(filteredImg == 255)
- result="Positive" if n_white_pix>1 else "Negative"
- # print(result)
- countAll=countAll+1
- if n_white_pix<=1:
- countNeg=countNeg+1
- else:
- continue
- msg = "train_dataset2: Negative: %f" % float(((countNeg/countAll)*100))
- msg += "%"
- print(msg)
- countAll=0
- countNeg=0
- for filename in os.listdir("80nm_updated/80nm_dataset_1_splitbyexperiment/80nm_dataset_1_splitbyexperiment/train/neg"):
- if filename.endswith(".PNG") or filename.endswith(".png"):
- img = cv2.imread('80nm_updated/80nm_dataset_1_splitbyexperiment/80nm_dataset_1_splitbyexperiment/train/neg/'+filename, 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>12 and y>12 and x<36 and y<36: 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), 1)
- heq = median
- median2 = cv2.medianBlur(heq, 3)
- heq2 = clahe2.apply(median2)
- median3 = cv2.GaussianBlur(heq2, (3, 3), 1)
- median3 = cv2.medianBlur(median3, 5)
- median3 = clahe.apply(median3)
- _, thersh = cv2.threshold(median3, 230, 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 = 10
- filteredImg = np.zeros((labels.shape))
- for i in range(0, nlabels):
- if sizes[i] >= min_size:
- filteredImg[labels == i + 1] = 255
- n_white_pix = np.sum(filteredImg == 255)
- result="Positive" if n_white_pix>1 else "Negative"
- # print(result)
- countAll=countAll+1
- if n_white_pix<=1:
- countNeg=countNeg+1
- else:
- continue
- msg = "train_dataset1: Negative: %f" % float(((countNeg/countAll)*100))
- msg += "%"
- print(msg)
- countAll=0
- countCorrect=0
- for filename in os.listdir("80nm_updated/80nm_dataset_1_splitbyexperiment/80nm_dataset_1_splitbyexperiment/test"):
- if filename.endswith(".PNG") or filename.endswith(".png"):
- img = cv2.imread('80nm_updated/80nm_dataset_1_splitbyexperiment/80nm_dataset_1_splitbyexperiment/test/'+filename, 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>12 and y>12 and x<36 and y<36: 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), 1)
- heq = median
- median2 = cv2.medianBlur(heq, 3)
- heq2 = clahe2.apply(median2)
- median3 = cv2.GaussianBlur(heq2, (3, 3), 1)
- median3 = cv2.medianBlur(median3, 5)
- median3 = clahe.apply(median3)
- _, thersh = cv2.threshold(median3, 230, 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 = 10
- filteredImg = np.zeros((labels.shape))
- for i in range(0, nlabels):
- if sizes[i] >= min_size:
- filteredImg[labels == i + 1] = 255
- n_white_pix = np.sum(filteredImg == 255)
- result="Positive" if n_white_pix>1 else "Negative"
- # print(result)
- countAll=countAll+1
- with open(
- '80nm_updated/80nm_dataset_1_splitbyexperiment/80nm_dataset_1_splitbyexperiment/annotations_test.csv') as file:
- reader = csv.reader(file)
- for row in reader:
- if row[0].split("/")[2] == filename:
- # print(row[0].split("/")[2] + " : " + row[1])
- if (row[1]=="0" and result=="Negative") or (row[1]=="1" and result=="Positive"):
- countCorrect=countCorrect+1
- else:
- continue
- msg = "test_dataset1: Correct: %f" % float(((countCorrect/countAll)*100))
- msg += "%"
- print(msg)
- countAll=0
- countCorrect=0
- for filename in os.listdir("80nm_updated/80nm_dataset_2_mixedexperiments/80nm_dataset_2_mixedexperiments/test"):
- if filename.endswith(".PNG") or filename.endswith(".png"):
- img = cv2.imread('80nm_updated/80nm_dataset_2_mixedexperiments/80nm_dataset_2_mixedexperiments/test/'+filename, 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>12 and y>12 and x<36 and y<36: 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), 1)
- heq = median
- median2 = cv2.medianBlur(heq, 3)
- heq2 = clahe2.apply(median2)
- median3 = cv2.GaussianBlur(heq2, (3, 3), 1)
- median3 = cv2.medianBlur(median3, 5)
- median3 = clahe.apply(median3)
- _, thersh = cv2.threshold(median3, 230, 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 = 10
- filteredImg = np.zeros((labels.shape))
- for i in range(0, nlabels):
- if sizes[i] >= min_size:
- filteredImg[labels == i + 1] = 255
- n_white_pix = np.sum(filteredImg == 255)
- result="Positive" if n_white_pix>1 else "Negative"
- # print(result)
- countAll=countAll+1
- with open(
- '80nm_updated/80nm_dataset_2_mixedexperiments/80nm_dataset_2_mixedexperiments/annotations_test.csv') as file:
- reader = csv.reader(file)
- for row in reader:
- if row[0].split("/")[2] == filename:
- # print(row[0].split("/")[2] + " : " + row[1])
- if (row[1]=="0" and result=="Negative") or (row[1]=="1" and result=="Positive"):
- countCorrect=countCorrect+1
- else:
- continue
- msg = "test_dataset2: Correct: %f" % float(((countCorrect/countAll)*100))
- msg += "%"
- print(msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement