Created
April 22, 2018 13:42
-
-
Save vincentius15/3fe8a22783c15cd0fa176ec8d51c7831 to your computer and use it in GitHub Desktop.
Morphology Image Processing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
#thresholding | |
def thresholding(img): | |
#load image | |
pixels = img.load() | |
#create blank output image | |
output = Image.new(img.mode, img.size) | |
outputPixels = output.load() | |
#scan image | |
for i in range(0,img.size[0]): | |
for j in range(0,img.size[1]): | |
#extract per channel value | |
r,g,b = pixels[i,j] | |
#transform each pixel | |
if((r+g+b)/3 > 103) : | |
outputPixels[i,j] = (255, 255, 255) | |
else : | |
outputPixels[i,j] = (0, 0, 0) | |
#save and return output image | |
output.save("outputThreshold.jpg") | |
return output | |
#erosion | |
def erosion(img): | |
#load image | |
pixels = img.load() | |
#create blank output image | |
output = Image.new(img.mode, img.size) | |
outputPixels = output.load() | |
#scan image | |
for i in range(0,img.size[0]): | |
for j in range(0,img.size[1]): | |
#handling corner of image | |
if(i==0 or j==0 or i==img.size[0]-1 or j==img.size[1]-1): | |
outputPixels[i,j] = (255, 255, 255) | |
#handling 0 value | |
elif(pixels[i,j] == (255, 255, 255)): | |
outputPixels[i,j] = (255, 255, 255) | |
#handling 1 value | |
else : | |
#assume pixel value is 1 | |
outputPixels[i,j] = (0, 0, 0) | |
#convolution | |
for k in range(i-1,i+1): | |
if(k<0 or k>img.size[0]-1) : | |
outputPixels[i,j] = (0, 0, 0) | |
break | |
for l in range(j-1,j+1): | |
if(l<0 or l>img.size[1]-1) : | |
outputPixels[i,j] = (0, 0, 0) | |
break | |
r,g,b = pixels[k,l] | |
#when at least one 0 value found, change pixel value to 0 | |
if(r == 255) : | |
outputPixels[i,j] = (255, 255, 255) | |
break | |
#save and return output image | |
output.save("outputErosion.jpg") | |
return output | |
#dilation | |
def dilation(img): | |
#load image | |
pixels = img.load() | |
#create blank output image | |
output = Image.new(img.mode, img.size) | |
outputPixels = output.load() | |
#scan image | |
for i in range(0,img.size[0]): | |
for j in range(0,img.size[1]): | |
#when pixel with 1 value found | |
if(pixels[i,j] == (0, 0, 0)) : | |
outputPixels[i,j] = (0, 0, 0) | |
#else | |
else : | |
#assume pixel value is 0 | |
outputPixels[i,j] = (255, 255, 255) | |
#convolution | |
for k in range(i-1,i+1): | |
#handling corner of image | |
if(k<0 or k>img.size[0]-1) : continue | |
for l in range(j-1,j+1): | |
if(l<0 or l>img.size[1]-1) : continue | |
r,g,b = pixels[k,l] | |
#when at least one 1 pixel value found, change pixel value to 1 | |
if(r == 0): | |
outputPixels[i,j] = (0, 0, 0) | |
break | |
output.save("outputDilation.jpg") | |
return output | |
#opening operator | |
def opening(img): | |
return dilation(erosion(img)) | |
#closing operator | |
def closing(img): | |
return erosion(dilation(img)) | |
#invert binary image | |
def invert(img) : | |
pixels = img.load() | |
output = Image.new(img.mode, img.size) | |
outputPixels = output.load() | |
for i in range(0,img.size[0]): | |
for j in range(0,img.size[1]): | |
#extract per channel value | |
r,g,b = pixels[i,j] | |
if(r == 0) : | |
outputPixels[i,j] = (255, 255, 255) | |
else : | |
outputPixels[i,j] = (0, 0, 0) | |
output.save("outputInvert.jpg") | |
return output | |
#driver program | |
if __name__ == "__main__": | |
img = Image.open('Picture3.jpg') | |
output = thresholding(img) | |
output.save("thresholding.jpg") | |
output = invert(output) | |
output.save("invert.jpg") | |
# output = closing(opening(output)) | |
# output.save("result.jpg") | |
output = erosion(output) | |
output.save("1-erosion.jpg") | |
output = erosion(output) | |
output.save("2-erosion.jpg") | |
output = dilation(output) | |
output.save("3-dilation.jpg") | |
output = dilation(output) | |
output.save("4-dilation.jpg") | |
output = dilation(output) | |
output.save("4-dilation.jpg") | |
output = erosion(output) | |
output.save("5-erosion.jpg") | |
# output = erosion(output) | |
# output.save("6-erosion.jpg") | |
# output = dilation(output) | |
# output.save("7-dilation.jpg") | |
output.close() | |
img.close() | |
# outputErosion = erosion(outputThreshold) | |
# outputErosion.save("outputErosion.jpg") | |
# outputDilation = dilation(outputErosion) | |
# outputDilation.save("outputDilation.jpg") | |
# outputDilation.close() | |
# outputThreshold.close() | |
# outputErosion.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment