조현아

get targets per shape

Showing 1000 changed files with 138 additions and 0 deletions

Too many changes to show.

To preserve performance only 1000 of 1000+ files are displayed.

1 +import pandas as pd
2 +import os
3 +import csv
4 +
5 +file_path = "../../data/MICCAI_BraTS_2019_Data_Training/train_frame_m1"
6 +csv_name = "../../data/MICCAI_BraTS_2019_Data_Training/train_targets.csv"
7 +
8 +f=open(csv_name, 'w', newline='')
9 +w=csv.writer(f)
10 +count = 0
11 +st = 'BraTS19_2013_10_1' #BraTS19_2013_10_1
12 +for path, dirs, files in os.walk(file_path):
13 + for filename in files:
14 + print(filename)
15 + if(filename.split('_seg_flair')[0] != st):
16 + st = filename.split('_seg_flair')[0]
17 + count = count + 1
18 + w.writerow([filename, count])
...\ No newline at end of file ...\ No newline at end of file
1 +import cv2
2 +import os
3 +import numpy as np
4 +import shutil
5 +import time
6 +
7 +font = cv2.FONT_HERSHEY_COMPLEX
8 +
9 +load_path = "./total_frame_m1"
10 +#load_path = "./sample"
11 +color = (255,255,255)
12 +
13 +for path, dirs, files in os.walk(load_path):
14 + for filename in files:
15 + img = cv2.imread(os.path.join(path, filename), cv2.IMREAD_GRAYSCALE)
16 + # (img, threshold_value, value, flag): if pixel val >= threshold_value: pixel_val = value
17 + _, threshold = cv2.threshold(img, 1, 255, cv2.THRESH_BINARY)
18 + contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
19 +
20 + n_ellipse, n_circle = 0, 0
21 + if (len(contours) > 0):
22 + area = cv2.contourArea(contours[0])
23 + largest = ""
24 +
25 + for cnt in contours:
26 + approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True)
27 + cv2.drawContours(img, [approx], 0, (0), 5)
28 + x = approx.ravel()[0]
29 + y = approx.ravel()[1]
30 +
31 + #print("approx", len(approx))
32 + if len(approx) < 12:
33 + #cv2.putText(img, "Ellipse", (x, y), font, 1, color)
34 + n_ellipse = n_ellipse + 1
35 + if (area < cv2.contourArea(cnt)):
36 + area = cv2.contourArea(cnt)
37 + largest = "e"
38 + else:
39 + #cv2.putText(img, "Circle", (x, y), font, 1, color)
40 + n_circle = n_circle + 1
41 + if (area < cv2.contourArea(cnt)):
42 + area = cv2.contourArea(cnt)
43 + largest = "c"
44 +
45 + print(n_ellipse, n_circle, filename)
46 + #assert n_ellipse !=0 or n_circle !=0
47 +
48 + if (n_ellipse > n_circle) or (n_ellipse==n_circle and largest == "e"):
49 + ellipase_path = os.path.join("./ellipse/", filename)
50 + shutil.copy(os.path.join(path, filename), ellipase_path)
51 +
52 + elif (n_ellipse < n_circle) or (n_ellipse==n_circle and largest == "c"):
53 + circle_path = os.path.join("./circle/", filename)
54 + shutil.copy(os.path.join(path, filename), circle_path)
55 +
56 + elif n_circle == 0 and n_ellipse == 0:
57 + shutil.copy(os.path.join(path, filename), os.path.join("./zero/", filename))
58 +
59 +
60 +# cv2.imshow("shapes", img)
61 +# cv2.imshow("Threshold", threshold)
62 +# cv2.waitKey(0)
63 +# cv2.destroyAllWindows()
...\ No newline at end of file ...\ No newline at end of file
1 +import cv2
2 +import numpy as np
3 +
4 +font = cv2.FONT_HERSHEY_COMPLEX
5 +
6 +#file_path = "../../data/MICCAI_BraTS_2019_Data_Training/train_frame_m1"
7 +
8 +# for path, dirs, files in os.walk(file_path):
9 +# for filename in files:
10 +
11 +img = cv2.imread("BraTS19_2013_2_1_seg_flair_8.png", cv2.IMREAD_GRAYSCALE)
12 + # (img, threshold_value, value, flag): if pixel val >= threshold_value: pixel_val = value
13 +_, threshold = cv2.threshold(img, 1, 255, cv2.THRESH_BINARY)
14 +contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
15 +
16 +color = (255,255,255)
17 +n_ellipse, n_circle = 0, 0
18 +area = cv2.contourArea(contours[0])
19 +
20 +for cnt in contours:
21 + approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True)
22 + cv2.drawContours(img, [approx], 0, (0), 5)
23 + x = approx.ravel()[0]
24 + y = approx.ravel()[1]
25 +
26 + print(area)
27 + if (area < cv2.contourArea(cnt)):
28 + area = cv2.contourArea(cnt)
29 +
30 + # ss = ""
31 +
32 + # if len(approx) == 3:
33 + # cv2.putText(img, "Triangle", (x, y), font, 1, color)
34 + # elif len(approx) == 4:
35 + # cv2.putText(img, "Rectangle", (x, y), font, 1, color)
36 + # elif len(approx) == 5:
37 + # cv2.putText(img, "Pentagon", (x, y), font, 1, color)
38 + if len(approx) < 12:
39 + cv2.putText(img, "Ellipse", (x, y), font, 1, color)
40 + n_ellipse = n_ellipse + 1
41 + # if (area < cv2.contourArea(cnt)):
42 + # area = cv2.contourArea(cnt)
43 + # ss = "ellipse"
44 + else:
45 + cv2.putText(img, "Circle", (x, y), font, 1, color)
46 + n_circle = n_circle + 1
47 + # if (area < cv2.contourArea(cnt)):
48 + # area = cv2.contourArea(cnt)
49 + # ss = "circle"
50 +
51 +print(n_ellipse, n_circle)
52 +#print(ss)
53 +
54 +cv2.imshow("shapes", img)
55 +cv2.imshow("Threshold", threshold)
56 +cv2.waitKey(0)
57 +cv2.destroyAllWindows()
...\ No newline at end of file ...\ No newline at end of file