-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexperiment.py
77 lines (65 loc) · 2.52 KB
/
experiment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'''Command line tool to run the Image Segmentation Senior Composition project.
This tool takes in a set of commands, and currently uses it to display the images
and their segmentations.
To be added:
A full experiment as decided on by the group
Slight restructuring of commands to make things easier
'''
from PIL import Image
from kmeans import kmeans
from thresholding import baseline_thresholding
from eval import bde, region_based_eval
import argparse
def parse_args():
'''
Parse command line for user commands
'''
p = argparse.ArgumentParser()
p.add_argument("--kmeans", action="store_true", help="Set this flag to run"+\
"K-means image segmentation")
p.add_argument("--segments", type=int, default=2, help="Number of segments"+\
"for the K-means algorithm")
p.add_argument("--threshold", action="store_true", help="Set this flag to" +\
"run thresholding image segmentation")
p.add_argument("--image", help="Name of image to be segmented",
default="22093.jpg")
p.add_argument("--displayOriginal", action="store_true", help="Displays" +\
"the original image along with the new one")
p.add_argument("--evaluate", action="store_true", help="Determines whether" +\
"evaluation metrics will be run on segmentations.")
return p.parse_args()
def merge(imgL, imgR):
'''A function that takes in two images and merges them to display
the new images next to eachother:
input: two PIL images
output: one horizontally merged PIL image'''
imgSize = imgL.size
imgWidth = 2 * imgSize[0]
imgHeight = imgSize[1]
imgNew = Image.new('RGB', (imgWidth, imgHeight))
imgNew.paste(imgL,(0,0))
imgNew.paste(imgR,(int(imgWidth/2), 0))
return imgNew
def main():
'''Runs the specified commands from the command line using our built in
segmentation algorithms and files. Runs on a default image for debugging
'''
args = parse_args()
if args is None:
print("Please enter arguments to run different segmentations")
if args.kmeans:
orig = Image.open(args.image)
temp = kmeans(orig, args.segments)
img = Image.fromarray(temp*(255 / args.segments))
if args.displayOriginal:
img = merge(orig, img)
img.show()
if args.threshold:
orig = Image.open(args.image)
temp = baseline_thresholding(orig)
img = Image.fromarray(temp*255)
if args.displayOriginal:
img = merge(orig, img)
img.show()
if __name__ == "__main__":
main()