|
| 1 | +# Author Bhargav K |
| 2 | + |
| 3 | +# Morphological Filters |
| 4 | +# BooleanEdgeDetectionFilter |
| 5 | + |
| 6 | +import sys |
| 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 "Choose BooleanFilterType" |
| 16 | +print "1 None" |
| 17 | +print "2 EdgeDetect" |
| 18 | +print "3 Sharpen" |
| 19 | +filtertype=input() |
| 20 | +filtersize=input("Choose the Size of Filter (Usually 3) : ") |
| 21 | +print "Usually 1.0" |
| 22 | +redfactor=input("Enter red factor value : ") |
| 23 | +greenfactor=input("Enter green factor value : ") |
| 24 | +bluefactor=input("Enter blue factor value : ") |
| 25 | +threshold=input("Enter Threshold value (0.0 to 200.0):") |
| 26 | + |
| 27 | +edgemasks=[] |
| 28 | +edgemasks.append("011011011") |
| 29 | +edgemasks.append("000111111") |
| 30 | +edgemasks.append("110110110") |
| 31 | +edgemasks.append("111111000") |
| 32 | +edgemasks.append("011011001") |
| 33 | +edgemasks.append("100110110") |
| 34 | +edgemasks.append("111011000") |
| 35 | +edgemasks.append("111110000") |
| 36 | +edgemasks.append("111011001") |
| 37 | +edgemasks.append("100110111") |
| 38 | +edgemasks.append("001011111") |
| 39 | +edgemasks.append("111110100") |
| 40 | +edgemasks.append("000011111") |
| 41 | +edgemasks.append("000110111") |
| 42 | +edgemasks.append("001011011") |
| 43 | +edgemasks.append("001011011") |
| 44 | +edgemasks.append("110110100") |
| 45 | + |
| 46 | +filterwidth=filtersize |
| 47 | +filterheight=filtersize |
| 48 | + |
| 49 | +filterOffset = (filterwidth-1)/2; |
| 50 | + |
| 51 | +offsety=filterOffset |
| 52 | +offsetx=filterOffset |
| 53 | + |
| 54 | +j = Image.new(i.mode,(width+(2*offsetx),height+(2*offsety))) |
| 55 | + |
| 56 | +for x in range(width+(2*offsetx)): |
| 57 | + for y in range(height+(2*offsety)): |
| 58 | + if(x<offsetx): |
| 59 | + j.putpixel((x,y),(0,0,0)) |
| 60 | + if(y<offsety): |
| 61 | + j.putpixel((x,y),(0,0,0)) |
| 62 | + if(x>=width): |
| 63 | + j.putpixel((x,y),(0,0,0)) |
| 64 | + if(y>=height): |
| 65 | + j.putpixel((x,y),(0,0,0)) |
| 66 | + if(x>=offsetx and y>=offsety and x < width and y <height): |
| 67 | + cpixel=pixels[x-offsetx,y-offsety] |
| 68 | + j.putpixel((x,y),cpixel) |
| 69 | + |
| 70 | +for y in range(height+(2*offsety)): |
| 71 | + for x in range(width+(2*offsetx)): |
| 72 | + if(x>=offsetx+1 and y>=offsety+1 and x < width and y <height): |
| 73 | + matrixmean=0 |
| 74 | + matrixtotal=0 |
| 75 | + matrixvariance=0.0 |
| 76 | + matrixpattern='' |
| 77 | + filterx=x-offsetx |
| 78 | + filtery=y-offsety |
| 79 | + for fy in range(filterheight): |
| 80 | + for fx in range(filterwidth): |
| 81 | + cpixel=j.getpixel((x-offsety+fx,y-offsety+fy)) |
| 82 | + matrixmean+=(cpixel[0]) |
| 83 | + matrixmean+=(cpixel[1]) |
| 84 | + matrixmean+=(cpixel[2]) |
| 85 | + matrixmean=matrixmean/9 |
| 86 | + for fy in range(filterheight): |
| 87 | + for fx in range(filterwidth): |
| 88 | + cpixel=j.getpixel((x-offsety+fx,y-offsety+fy)) |
| 89 | + matrixtotal=(cpixel[0]) |
| 90 | + matrixtotal+=(cpixel[1]) |
| 91 | + matrixtotal+=(cpixel[2]) |
| 92 | + if(matrixtotal>matrixmean): |
| 93 | + matrixpattern+='1' |
| 94 | + else: |
| 95 | + matrixpattern+='0' |
| 96 | + matrixvariance+=pow(matrixmean-(cpixel[0]+cpixel[1]+cpixel[2]),2) |
| 97 | + matrixvariance=matrixvariance/9.0 |
| 98 | + cpixel=j.getpixel((x,y)) |
| 99 | + if(filtertype==3): |
| 100 | + red=cpixel[0] |
| 101 | + green=cpixel[1] |
| 102 | + blue=cpixel[2] |
| 103 | + if(matrixvariance>threshold): |
| 104 | + if(matrixpattern in edgemasks): |
| 105 | + red=int(red*redfactor) |
| 106 | + green=int(green*greenfactor) |
| 107 | + blue=int(blue*bluefactor) |
| 108 | + if(red<0): |
| 109 | + red=0 |
| 110 | + if(red>255): |
| 111 | + red=255 |
| 112 | + if(green<0): |
| 113 | + green=0 |
| 114 | + if(green>255): |
| 115 | + green=255 |
| 116 | + if(blue<0): |
| 117 | + blue=0 |
| 118 | + if(blue>255): |
| 119 | + blue=255 |
| 120 | + elif(matrixvariance>threshold and (matrixpattern in edgemasks)): |
| 121 | + red=255 |
| 122 | + green=255 |
| 123 | + blue=255 |
| 124 | + else: |
| 125 | + red=0 |
| 126 | + green=0 |
| 127 | + blue=0 |
| 128 | + del matrixpattern |
| 129 | + k.putpixel((x-offsetx,y-offsety),(int(red),int(green),int(blue))) |
| 130 | +k.save('output.png') |
0 commit comments