Skip to content

Commit

Permalink
Openend a new branch
Browse files Browse the repository at this point in the history
  • Loading branch information
lnugraha committed Feb 26, 2020
1 parent 5a19aa6 commit 395b798
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 58 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added images/.DS_Store
Binary file not shown.
Binary file added images/results/20px_test_image_9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
117 changes: 59 additions & 58 deletions trimap_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def extractImage(path):
# error handller if the intended path is not found
image = cv2.imread(path, cv2.IMREAD_GRAYSCALE);
image = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
return image

def checkImage(image):
Expand All @@ -20,27 +20,29 @@ def checkImage(image):
"""
if len(image.shape) > 2:
print("ERROR: non-binary image (RGB)");
sys.exit();

smallest = image.min(axis=0).min(axis=0); # lowest pixel value; should be 0 (black)
largest = image.max(axis=0).max(axis=0); # highest pixel value; should be 1 (white)
print("ERROR: non-binary image (RGB)")
sys.exit()

# lowest pixel value; should be 0 (black)
smallest = image.min(axis=0).min(axis=0)
# highest pixel value; should be 1 (white)
largest = image.max(axis=0).max(axis=0)

if (smallest == 0 and largest == 0):
print("ERROR: non-binary image (all black)");
sys.exit();
print("ERROR: non-binary image (all black)")
sys.exit()
elif (smallest == 255 and largest == 255):
print("ERROR: non-binary image (all white)");
sys.exit();
print("ERROR: non-binary image (all white)")
sys.exit()
elif (smallest > 0 or largest < 255 ):
print("ERROR: non-binary image (grayscale)");
sys.exit();
print("ERROR: non-binary image (grayscale)")
sys.exit()
else:
return True

class Toolbox:
def __init__(self, image):
self.image = image;
self.image = image

@property
def printImage(self):
Expand All @@ -62,15 +64,15 @@ def displayImage(self):
Display the image on a window
Press any key to exit
"""
cv2.imshow('Displayed Image', self.image);
cv2.waitKey(0);
cv2.destroyAllWindows();
cv2.imshow('Displayed Image', self.image)
cv2.waitKey(0)
cv2.destroyAllWindows()

def saveImage(self, title, extension):
"""
Save as a specific image format (bmp, png, or jpeg)
"""
cv2.imwrite("{}.{}".format(title,extension), self.image);
cv2.imwrite("{}.{}".format(title,extension), self.image)

def morph_open(self, image, kernel):
"""
Expand Down Expand Up @@ -121,29 +123,33 @@ def scaling(self, image, erosion):
image = np.where(image > 0, 255, image) ## Any gray-clored pixel becomes white (smoothing)
# Error-handler to prevent entire foreground annihilation
if cv2.countNonZero(image) == 0:
print("ERROR: foreground has been entirely eroded");
sys.exit();
return image;
print("ERROR: foreground has been entirely eroded")
sys.exit()
return image

class Dilation(DEFG):
def __init__(self, image):
self.image = image

def scaling(self, image, dilation):
dilation = int(dilation)
kernel = np.ones((3,3), np.uint8) ## Design an odd-sized erosion kernel
image = cv2.dilate(image, kernel, iterations=dilation) ## The number of dilations
image = np.where(image > 0, 255, image) ## Any gray-clored pixel becomes white (smoothing)
kernel = np.ones((3,3), np.uint8) ## Design an odd-sized erosion kernel

## The number of dilations
image = cv2.dilate(image, kernel, iterations=dilation)

## Any gray-clored pixel becomes white (smoothing)
image = np.where(image > 0, 255, image)
# Error-handler to prevent entire foreground domination
height = image.shape[0];
width = image.shape[1];
totalpixels = height*width;
height = image.shape[0]
width = image.shape[1]
totalpixels = height*width
n_white_pix = np.sum(image == 255)

if n_white_pix == totalpixels:
print("ERROR: foreground has been entirely expanded");
sys.exit();
return image;
print("ERROR: foreground has been entirely expanded")
sys.exit()
return image

def trimap(image, name, size, number, DEFG=None, num_iter=0):
"""
Expand All @@ -152,35 +158,33 @@ def trimap(image, name, size, number, DEFG=None, num_iter=0):
the last argument is optional; i.e., how many iterations will the image get eroded
Output : a trimap
"""
checkImage(image);
checkImage(image)

row = image.shape[0];
col = image.shape[1];

pixels = 2*size + 1; ## Double and plus 1 to have an odd-sized kernel
kernel = np.ones((pixels,pixels),np.uint8) ## How many pixel of extension do I get

row = image.shape[0]
col = image.shape[1]
pixels = 2*size + 1 ## Double and plus 1 to have an odd-sized kernel
kernel = np.ones((pixels,pixels),np.uint8) ## How many pixel extension do I get
if DEFG==None:
pass
elif (DEFG == Dilation):
expand = Dilation(image);
image = expand.scaling(image, num_iter);
expand = Dilation(image)
image = expand.scaling(image, num_iter)
elif (DEFG == Erosion):
shrink = Erosion(image);
image = shrink.scaling(image, num_iter);
shrink = Erosion(image)
image = shrink.scaling(image, num_iter)
else:
printf("ERROR: Unspecified foreground dilation or erosion method");
sys.exit();
printf("ERROR: Unspecified foreground dilation or erosion method")
sys.exit()

dilation = cv2.dilate(image, kernel, iterations = 1)

dilation = np.where(dilation == 255, 127, dilation) ## WHITE to GRAY
remake = np.where(dilation != 127, 0, dilation) ## Smoothing
dilation = np.where(dilation == 255, 127, dilation)## WHITE to GRAY
remake = np.where(dilation != 127, 0, dilation) ## Smoothing
remake = np.where(image > 127, 200, dilation) ## mark the tumor inside GRAY

remake = np.where(remake < 127, 0, remake) ## Embelishment
remake = np.where(remake > 200, 0, remake) ## Embelishment
remake = np.where(remake == 200, 255, remake) ## GRAY to WHITE
remake = np.where(remake < 127, 0, remake) ## Embelishment
remake = np.where(remake > 200, 0, remake) ## Embelishment
remake = np.where(remake == 200, 255, remake) ## GRAY to WHITE

#############################################
# Ensures only three pixel values available #
Expand All @@ -189,23 +193,20 @@ def trimap(image, name, size, number, DEFG=None, num_iter=0):
for i in range(0,row):
for j in range (0,col):
if (remake[i,j] != 0 and remake[i,j] != 255):
remake[i,j] = 127;
remake[i,j] = 127

path = "./images/results/" ## Change the directory
new_name = '{}px_'.format(size) + name + '_{}.png'.format(number);
path = "./images/results/" ## Change the directory
new_name = '{}px_'.format(size) + name + '_{}.png'.format(number)
cv2.imwrite(os.path.join(path , new_name) , remake)


#############################################
### TESTING SECTION ###
#############################################
if __name__ == '__main__':

path = "./images/test_images/test_image_12.png";
path = "./images/test_images/test_image_9.png"
image = extractImage(path)

size = 10;
number = path[-5];
size = 20
number = path[-5]
title = "test_image"

trimap(image, title, size, number, DEFG=None);
# trimap(image, title, size, number, DEFG=None)
trimap(image, title, size, number, DEFG=None)

0 comments on commit 395b798

Please sign in to comment.