Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.32 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import os
  4. import csv
  5.  
  6. countAll=0
  7. countNeg=0
  8.  
  9. for filename in os.listdir("80nm_updated/80nm_dataset_2_mixedexperiments/80nm_dataset_2_mixedexperiments/train/pos"):
  10. if filename.endswith(".PNG") or filename.endswith(".png"):
  11.  
  12. img = cv2.imread('80nm_updated/80nm_dataset_2_mixedexperiments/80nm_dataset_2_mixedexperiments/train/pos/'+filename, cv2.IMREAD_GRAYSCALE)
  13. mask = np.zeros(img.shape, dtype=np.uint8)
  14. h = mask.shape[0]
  15. w = mask.shape[1]
  16.  
  17.  
  18. for y in range(0, h):
  19. for x in range(0, w):
  20. if x>12 and y>12 and x<36 and y<36: mask[y, x] = 255
  21.  
  22. clahe = cv2.createCLAHE(clipLimit=15, tileGridSize=(5, 5))
  23.  
  24. clahe2 = cv2.createCLAHE(clipLimit=25, tileGridSize=(1, 1))
  25.  
  26. kernal = np.ones((5, 5), np.uint8)
  27.  
  28. median = cv2.GaussianBlur(img, (3, 3), 1)
  29. heq = median
  30. median2 = cv2.medianBlur(heq, 3)
  31. heq2 = clahe2.apply(median2)
  32. median3 = cv2.GaussianBlur(heq2, (3, 3), 1)
  33. median3 = cv2.medianBlur(median3, 5)
  34.  
  35.  
  36. median3 = clahe.apply(median3)
  37.  
  38.  
  39. _, thersh = cv2.threshold(median3, 230, 255, cv2.THRESH_BINARY)
  40.  
  41. maskedThresh = cv2.bitwise_and(thersh, thersh, mask=mask)
  42.  
  43. nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(maskedThresh)
  44. sizes = stats[1:, -1];
  45. nlabels = nlabels - 1
  46. min_size = 10
  47. filteredImg = np.zeros((labels.shape))
  48. for i in range(0, nlabels):
  49. if sizes[i] >= min_size:
  50. filteredImg[labels == i + 1] = 255
  51.  
  52. n_white_pix = np.sum(filteredImg == 255)
  53. result="Positive" if n_white_pix>1 else "Negative"
  54. # print(result)
  55. countAll=countAll+1
  56. if n_white_pix>1:
  57. countNeg=countNeg+1
  58. else:
  59. continue
  60. msg = "train_dataset2: Positive: %f" % float(((countNeg/countAll)*100))
  61. msg += "%"
  62. print(msg)
  63.  
  64.  
  65. countAll=0
  66. countNeg=0
  67. for filename in os.listdir("80nm_updated/80nm_dataset_1_splitbyexperiment/80nm_dataset_1_splitbyexperiment/train/pos"):
  68. if filename.endswith(".PNG") or filename.endswith(".png"):
  69.  
  70. img = cv2.imread('80nm_updated/80nm_dataset_1_splitbyexperiment/80nm_dataset_1_splitbyexperiment/train/pos/'+filename, cv2.IMREAD_GRAYSCALE)
  71. mask = np.zeros(img.shape, dtype=np.uint8)
  72. h = mask.shape[0]
  73. w = mask.shape[1]
  74.  
  75.  
  76. for y in range(0, h):
  77. for x in range(0, w):
  78. if x>12 and y>12 and x<36 and y<36: mask[y, x] = 255
  79.  
  80. clahe = cv2.createCLAHE(clipLimit=15, tileGridSize=(5, 5))
  81.  
  82. clahe2 = cv2.createCLAHE(clipLimit=25, tileGridSize=(1, 1))
  83.  
  84. kernal = np.ones((5, 5), np.uint8)
  85.  
  86. median = cv2.GaussianBlur(img, (3, 3), 1)
  87. heq = median
  88. median2 = cv2.medianBlur(heq, 3)
  89. heq2 = clahe2.apply(median2)
  90. median3 = cv2.GaussianBlur(heq2, (3, 3), 1)
  91. median3 = cv2.medianBlur(median3, 5)
  92.  
  93.  
  94. median3 = clahe.apply(median3)
  95.  
  96.  
  97. _, thersh = cv2.threshold(median3, 230, 255, cv2.THRESH_BINARY)
  98.  
  99. maskedThresh = cv2.bitwise_and(thersh, thersh, mask=mask)
  100.  
  101. nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(maskedThresh)
  102. sizes = stats[1:, -1];
  103. nlabels = nlabels - 1
  104. min_size = 10
  105. filteredImg = np.zeros((labels.shape))
  106. for i in range(0, nlabels):
  107. if sizes[i] >= min_size:
  108. filteredImg[labels == i + 1] = 255
  109.  
  110. n_white_pix = np.sum(filteredImg == 255)
  111. result="Positive" if n_white_pix>1 else "Negative"
  112. # print(result)
  113. countAll=countAll+1
  114. if n_white_pix>1:
  115. countNeg=countNeg+1
  116. else:
  117. continue
  118. msg = "train_dataset1: Positive: %f" % float(((countNeg/countAll)*100))
  119. msg += "%"
  120. print(msg)
  121.  
  122.  
  123. countAll=0
  124. countNeg=0
  125. for filename in os.listdir("80nm_updated/80nm_dataset_2_mixedexperiments/80nm_dataset_2_mixedexperiments/train/neg"):
  126. if filename.endswith(".PNG") or filename.endswith(".png"):
  127.  
  128. img = cv2.imread('80nm_updated/80nm_dataset_2_mixedexperiments/80nm_dataset_2_mixedexperiments/train/neg/'+filename, cv2.IMREAD_GRAYSCALE)
  129. mask = np.zeros(img.shape, dtype=np.uint8)
  130. h = mask.shape[0]
  131. w = mask.shape[1]
  132.  
  133.  
  134. for y in range(0, h):
  135. for x in range(0, w):
  136. if x>12 and y>12 and x<36 and y<36: mask[y, x] = 255
  137.  
  138. clahe = cv2.createCLAHE(clipLimit=15, tileGridSize=(5, 5))
  139.  
  140. clahe2 = cv2.createCLAHE(clipLimit=25, tileGridSize=(1, 1))
  141.  
  142. kernal = np.ones((5, 5), np.uint8)
  143.  
  144. median = cv2.GaussianBlur(img, (3, 3), 1)
  145. heq = median
  146. median2 = cv2.medianBlur(heq, 3)
  147. heq2 = clahe2.apply(median2)
  148. median3 = cv2.GaussianBlur(heq2, (3, 3), 1)
  149. median3 = cv2.medianBlur(median3, 5)
  150.  
  151.  
  152. median3 = clahe.apply(median3)
  153.  
  154.  
  155. _, thersh = cv2.threshold(median3, 230, 255, cv2.THRESH_BINARY)
  156.  
  157. maskedThresh = cv2.bitwise_and(thersh, thersh, mask=mask)
  158.  
  159. nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(maskedThresh)
  160. sizes = stats[1:, -1];
  161. nlabels = nlabels - 1
  162. min_size = 10
  163. filteredImg = np.zeros((labels.shape))
  164. for i in range(0, nlabels):
  165. if sizes[i] >= min_size:
  166. filteredImg[labels == i + 1] = 255
  167.  
  168. n_white_pix = np.sum(filteredImg == 255)
  169. result="Positive" if n_white_pix>1 else "Negative"
  170. # print(result)
  171. countAll=countAll+1
  172. if n_white_pix<=1:
  173. countNeg=countNeg+1
  174. else:
  175. continue
  176. msg = "train_dataset2: Negative: %f" % float(((countNeg/countAll)*100))
  177. msg += "%"
  178. print(msg)
  179.  
  180.  
  181. countAll=0
  182. countNeg=0
  183. for filename in os.listdir("80nm_updated/80nm_dataset_1_splitbyexperiment/80nm_dataset_1_splitbyexperiment/train/neg"):
  184. if filename.endswith(".PNG") or filename.endswith(".png"):
  185.  
  186. img = cv2.imread('80nm_updated/80nm_dataset_1_splitbyexperiment/80nm_dataset_1_splitbyexperiment/train/neg/'+filename, cv2.IMREAD_GRAYSCALE)
  187. mask = np.zeros(img.shape, dtype=np.uint8)
  188. h = mask.shape[0]
  189. w = mask.shape[1]
  190.  
  191.  
  192. for y in range(0, h):
  193. for x in range(0, w):
  194. if x>12 and y>12 and x<36 and y<36: mask[y, x] = 255
  195.  
  196. clahe = cv2.createCLAHE(clipLimit=15, tileGridSize=(5, 5))
  197.  
  198. clahe2 = cv2.createCLAHE(clipLimit=25, tileGridSize=(1, 1))
  199.  
  200. kernal = np.ones((5, 5), np.uint8)
  201.  
  202. median = cv2.GaussianBlur(img, (3, 3), 1)
  203. heq = median
  204. median2 = cv2.medianBlur(heq, 3)
  205. heq2 = clahe2.apply(median2)
  206. median3 = cv2.GaussianBlur(heq2, (3, 3), 1)
  207. median3 = cv2.medianBlur(median3, 5)
  208.  
  209.  
  210. median3 = clahe.apply(median3)
  211.  
  212.  
  213. _, thersh = cv2.threshold(median3, 230, 255, cv2.THRESH_BINARY)
  214.  
  215. maskedThresh = cv2.bitwise_and(thersh, thersh, mask=mask)
  216.  
  217. nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(maskedThresh)
  218. sizes = stats[1:, -1];
  219. nlabels = nlabels - 1
  220. min_size = 10
  221. filteredImg = np.zeros((labels.shape))
  222. for i in range(0, nlabels):
  223. if sizes[i] >= min_size:
  224. filteredImg[labels == i + 1] = 255
  225.  
  226. n_white_pix = np.sum(filteredImg == 255)
  227. result="Positive" if n_white_pix>1 else "Negative"
  228. # print(result)
  229. countAll=countAll+1
  230. if n_white_pix<=1:
  231. countNeg=countNeg+1
  232. else:
  233. continue
  234. msg = "train_dataset1: Negative: %f" % float(((countNeg/countAll)*100))
  235. msg += "%"
  236. print(msg)
  237.  
  238.  
  239. countAll=0
  240. countCorrect=0
  241. for filename in os.listdir("80nm_updated/80nm_dataset_1_splitbyexperiment/80nm_dataset_1_splitbyexperiment/test"):
  242. if filename.endswith(".PNG") or filename.endswith(".png"):
  243.  
  244. img = cv2.imread('80nm_updated/80nm_dataset_1_splitbyexperiment/80nm_dataset_1_splitbyexperiment/test/'+filename, cv2.IMREAD_GRAYSCALE)
  245. mask = np.zeros(img.shape, dtype=np.uint8)
  246. h = mask.shape[0]
  247. w = mask.shape[1]
  248.  
  249.  
  250. for y in range(0, h):
  251. for x in range(0, w):
  252. if x>12 and y>12 and x<36 and y<36: mask[y, x] = 255
  253.  
  254. clahe = cv2.createCLAHE(clipLimit=15, tileGridSize=(5, 5))
  255.  
  256. clahe2 = cv2.createCLAHE(clipLimit=25, tileGridSize=(1, 1))
  257.  
  258. kernal = np.ones((5, 5), np.uint8)
  259.  
  260. median = cv2.GaussianBlur(img, (3, 3), 1)
  261. heq = median
  262. median2 = cv2.medianBlur(heq, 3)
  263. heq2 = clahe2.apply(median2)
  264. median3 = cv2.GaussianBlur(heq2, (3, 3), 1)
  265. median3 = cv2.medianBlur(median3, 5)
  266.  
  267.  
  268. median3 = clahe.apply(median3)
  269.  
  270.  
  271. _, thersh = cv2.threshold(median3, 230, 255, cv2.THRESH_BINARY)
  272.  
  273. maskedThresh = cv2.bitwise_and(thersh, thersh, mask=mask)
  274.  
  275. nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(maskedThresh)
  276. sizes = stats[1:, -1];
  277. nlabels = nlabels - 1
  278. min_size = 10
  279. filteredImg = np.zeros((labels.shape))
  280. for i in range(0, nlabels):
  281. if sizes[i] >= min_size:
  282. filteredImg[labels == i + 1] = 255
  283.  
  284. n_white_pix = np.sum(filteredImg == 255)
  285. result="Positive" if n_white_pix>1 else "Negative"
  286. # print(result)
  287. countAll=countAll+1
  288.  
  289. with open(
  290. '80nm_updated/80nm_dataset_1_splitbyexperiment/80nm_dataset_1_splitbyexperiment/annotations_test.csv') as file:
  291. reader = csv.reader(file)
  292. for row in reader:
  293. if row[0].split("/")[2] == filename:
  294. # print(row[0].split("/")[2] + " : " + row[1])
  295. if (row[1]=="0" and result=="Negative") or (row[1]=="1" and result=="Positive"):
  296. countCorrect=countCorrect+1
  297.  
  298. else:
  299. continue
  300. msg = "test_dataset1: Correct: %f" % float(((countCorrect/countAll)*100))
  301. msg += "%"
  302. print(msg)
  303.  
  304. countAll=0
  305. countCorrect=0
  306. for filename in os.listdir("80nm_updated/80nm_dataset_2_mixedexperiments/80nm_dataset_2_mixedexperiments/test"):
  307. if filename.endswith(".PNG") or filename.endswith(".png"):
  308.  
  309. img = cv2.imread('80nm_updated/80nm_dataset_2_mixedexperiments/80nm_dataset_2_mixedexperiments/test/'+filename, cv2.IMREAD_GRAYSCALE)
  310. mask = np.zeros(img.shape, dtype=np.uint8)
  311. h = mask.shape[0]
  312. w = mask.shape[1]
  313.  
  314.  
  315. for y in range(0, h):
  316. for x in range(0, w):
  317. if x>12 and y>12 and x<36 and y<36: mask[y, x] = 255
  318.  
  319. clahe = cv2.createCLAHE(clipLimit=15, tileGridSize=(5, 5))
  320.  
  321. clahe2 = cv2.createCLAHE(clipLimit=25, tileGridSize=(1, 1))
  322.  
  323. kernal = np.ones((5, 5), np.uint8)
  324.  
  325. median = cv2.GaussianBlur(img, (3, 3), 1)
  326. heq = median
  327. median2 = cv2.medianBlur(heq, 3)
  328. heq2 = clahe2.apply(median2)
  329. median3 = cv2.GaussianBlur(heq2, (3, 3), 1)
  330. median3 = cv2.medianBlur(median3, 5)
  331.  
  332.  
  333. median3 = clahe.apply(median3)
  334.  
  335.  
  336. _, thersh = cv2.threshold(median3, 230, 255, cv2.THRESH_BINARY)
  337.  
  338. maskedThresh = cv2.bitwise_and(thersh, thersh, mask=mask)
  339.  
  340. nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(maskedThresh)
  341. sizes = stats[1:, -1];
  342. nlabels = nlabels - 1
  343. min_size = 10
  344. filteredImg = np.zeros((labels.shape))
  345. for i in range(0, nlabels):
  346. if sizes[i] >= min_size:
  347. filteredImg[labels == i + 1] = 255
  348.  
  349. n_white_pix = np.sum(filteredImg == 255)
  350. result="Positive" if n_white_pix>1 else "Negative"
  351. # print(result)
  352. countAll=countAll+1
  353.  
  354. with open(
  355. '80nm_updated/80nm_dataset_2_mixedexperiments/80nm_dataset_2_mixedexperiments/annotations_test.csv') as file:
  356. reader = csv.reader(file)
  357. for row in reader:
  358. if row[0].split("/")[2] == filename:
  359. # print(row[0].split("/")[2] + " : " + row[1])
  360. if (row[1]=="0" and result=="Negative") or (row[1]=="1" and result=="Positive"):
  361. countCorrect=countCorrect+1
  362.  
  363. else:
  364. continue
  365. msg = "test_dataset2: Correct: %f" % float(((countCorrect/countAll)*100))
  366. msg += "%"
  367. print(msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement