|
| 1 | +# Author Bhargav K |
| 2 | + |
| 3 | +# Morphological Filters |
| 4 | +# DilateAndErodeFilter |
| 5 | + |
| 6 | + |
| 7 | +from PIL import Image |
| 8 | +i = Image.open("input.png") |
| 9 | + |
| 10 | +#store pixels of input image |
| 11 | +pixels = i.load() |
| 12 | +width, height = i.size |
| 13 | +k=Image.new(i.mode,i.size) |
| 14 | + |
| 15 | +print "Filter size should be an odd positive number" |
| 16 | +filtersize=input("Choose the Size of Filter: ") |
| 17 | +print "Type (True) or (False) without braces" |
| 18 | +applyred=input("Choose to apply red : ") |
| 19 | +applygreen=input("Choose to apply green : ") |
| 20 | +applyblue=input("Choose to apply blue : ") |
| 21 | +print "Choose Morphology Type" |
| 22 | +print "1 Dilation" |
| 23 | +print "2 Erosion" |
| 24 | + |
| 25 | +morphtype=input() |
| 26 | + |
| 27 | +morphresetvalue=0.0 |
| 28 | +if(morphtype==2): |
| 29 | + morphresetvalue=255.0 |
| 30 | + |
| 31 | +print "Choose Morphology Edge Type" |
| 32 | +print "1 EdgeDetection" |
| 33 | +print "2 SharpenEdgeDetection" |
| 34 | + |
| 35 | +edgetype=input() |
| 36 | + |
| 37 | +filterwidth=filtersize |
| 38 | +filterheight=filtersize |
| 39 | + |
| 40 | +filterOffset = (filterwidth-1)/2; |
| 41 | + |
| 42 | +offsety=filterOffset |
| 43 | +offsetx=filterOffset |
| 44 | + |
| 45 | + |
| 46 | +j = Image.new(i.mode,(width+(2*offsetx),height+(2*offsety))) |
| 47 | + |
| 48 | +for x in range(width+(2*offsetx)): |
| 49 | + for y in range(height+(2*offsety)): |
| 50 | + if(x<offsetx): |
| 51 | + j.putpixel((x,y),(0,0,0)) |
| 52 | + if(y<offsety): |
| 53 | + j.putpixel((x,y),(0,0,0)) |
| 54 | + if(x>=width): |
| 55 | + j.putpixel((x,y),(0,0,0)) |
| 56 | + if(y>=height): |
| 57 | + j.putpixel((x,y),(0,0,0)) |
| 58 | + if(x>=offsetx and y>=offsety and x < width and y <height): |
| 59 | + cpixel=pixels[x-offsetx,y-offsety] |
| 60 | + j.putpixel((x,y),cpixel) |
| 61 | + |
| 62 | + |
| 63 | +for y in range(height+(2*offsety)): |
| 64 | + for x in range(width+(2*offsetx)): |
| 65 | + if(x>=offsetx and y>=offsety and x < width and y <height): |
| 66 | + blue=morphresetvalue |
| 67 | + green=morphresetvalue |
| 68 | + red=morphresetvalue |
| 69 | + filterx=x-offsetx |
| 70 | + filtery=y-offsety |
| 71 | + if(morphtype==1): |
| 72 | + for fy in range(filterheight): |
| 73 | + for fx in range(filterwidth): |
| 74 | + cpixel=j.getpixel((x-offsety+fx,y-offsety+fy)) |
| 75 | + if(float(cpixel[0]) > red): |
| 76 | + red=float(cpixel[0]) |
| 77 | + if(float(cpixel[1]) > green): |
| 78 | + green=float(cpixel[1]) |
| 79 | + if(float(cpixel[2]) > blue): |
| 80 | + blue=float(cpixel[2]) |
| 81 | + if(morphtype==2): |
| 82 | + for fy in range(filterheight): |
| 83 | + for fx in range(filterwidth): |
| 84 | + cpixel=j.getpixel((x-offsety+fx,y-offsety+fy)) |
| 85 | + if(float(cpixel[0]) < red): |
| 86 | + red=float(cpixel[0]) |
| 87 | + if(float(cpixel[1]) < green): |
| 88 | + green=float(cpixel[1]) |
| 89 | + if(float(cpixel[2]) < blue): |
| 90 | + blue=float(cpixel[2]) |
| 91 | + cpixel=j.getpixel((x,y)) |
| 92 | + if(applyred==False): |
| 93 | + red=cpixel[0] |
| 94 | + if(applygreen==False): |
| 95 | + green=cpixel[1] |
| 96 | + if(applyblue==False): |
| 97 | + blue=cpixel[2] |
| 98 | + if(edgetype==1 or edgetype==2): |
| 99 | + if(morphtype==1): |
| 100 | + red=red-cpixel[0] |
| 101 | + green=green-cpixel[1] |
| 102 | + blue=blue-cpixel[2] |
| 103 | + if(morphtype==2): |
| 104 | + red=cpixel[0]-red |
| 105 | + green=cpixel[1]-green |
| 106 | + blue=cpixel[2]-blue |
| 107 | + if(edgetype==2): |
| 108 | + red=red+cpixel[0] |
| 109 | + green=green+cpixel[1] |
| 110 | + blue=blue+cpixel[2] |
| 111 | + if(red<0): |
| 112 | + red=0 |
| 113 | + if(red>255): |
| 114 | + red=255 |
| 115 | + if(green<0): |
| 116 | + green=0 |
| 117 | + if(green>255): |
| 118 | + green=255 |
| 119 | + if(blue<0): |
| 120 | + blue=0 |
| 121 | + if(blue>255): |
| 122 | + blue=255 |
| 123 | + k.putpixel((x-offsetx,y-offsety),(int(red),int(green),int(blue))) |
| 124 | + |
| 125 | +k.save('output.png') |
0 commit comments