-
Notifications
You must be signed in to change notification settings - Fork 0
/
applyMaskToImage.py
48 lines (33 loc) · 1.34 KB
/
applyMaskToImage.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
"""
Makes the output of the grabCut algorithm more useful by actually applying the masks that it generates to the images in question.
"""
import numpy as np
import matplotlib.pyplot as plt
import cv2
import directories
from visualization import *
from scoreOutput import greyscale
def main():
output = directories.loadImagesInFolder(directories.output)
output = np.asarray(output)
groundTruth = directories.loadImagesInFolder(directories.groundTruth)
groundTruth = np.asarray(groundTruth)
l = []
for outputImage in output:
#Find the corresponding groundTruth image
fname = outputImage[1]
groundTruthImage = groundTruth[groundTruth[:, 1] == fname][0][0]
#Now compare the output with the ground truth
groundTruthImage = greyscale(groundTruthImage)
pixelsTotal = groundTruthImage.size
outputImage = np.asarray(outputImage[0])
outputImage = greyscale(outputImage)
img = directories.loadImageByName(directories.data + fname + ".bmp")
if img is None:
img = directories.loadImageByName(directories.data + fname + ".jpg")
assert img is not None
img[outputImage == 0] = 0
cv2.cv.SaveImage("maskedImages/" + fname + ".jpg", cv2.cv.fromarray(np.asarray(img, dtype="int")))
print("Done")
if __name__ == "__main__":
main()