Project -6

Brucele
thresholdingAssignment.zip

thresholdingAssignment/program_main.py

from PIL import Image from PIL import ImageOps import statistics threshold = 0 # Global, so we can use to normalize for Adaptive progressive Threshold def otsu(grayImage): global threshold pxlEdit = grayImage.load() w,h = grayImage.size imgSize = w * h mean_Weight = 1/imgSize imgHistogram = grayImage.histogram() # Compute Image Histogram varmax,sum,sumB,q1,q2,mu1,mu2 = 0,0,0,0,0,0,0 for i in range(0,256): sum += i * imgHistogram[i] for j in range(0,255): q1 += imgHistogram[j] # Dark q2 = imgSize - q1 # Light if q1 == 0: continue if q2 == 0: continue sumB += j * imgHistogram[j] mu1 = sumB/q1 # Avg Dark mu2 = (sum-sumB) /q2 # Avg Lights varSquared = q1*q2 * ((mu1 - mu2)*(mu1 - mu2)) # Interclass Difference if varSquared > varmax: threshold = j varmax = varSquared return threshold, pxlEdit def imageCreator(threshold,pxlEdit,image): w,h = image.size # Creating of binary image for a in range(0,w): for b in range(0,h): grayValue = pxlEdit[a,b] if grayValue > threshold: pxlEdit[a,b] = 255 else: pxlEdit[a, b] = 0 def AdaptiveProgThresh(grayImage): global threshold pxlEdit = grayImage.load() w,h = grayImage.size imageCreator(threshold,pxlEdit,grayImage) # Runing otsu and APT images def createImages(imagePath): global threshold imgOTSU = Image.open(imagePath).convert('L') threshold, pxlEdit = otsu(imgOTSU) print('Otsu Adaptive Threshold:', threshold) imageCreator(threshold,pxlEdit,imgOTSU) imgOTSU.show() imgAPT = Image.open(imagePath).convert('L') imgAPT = imgAPT.quantize(threshold) otsu(imgAPT) imgAPT = Image.open(imagePath).convert('L') AdaptiveProgThresh(imgAPT) print('Adaptive Progressive Threshold: ', threshold) imageCreator(threshold,pxlEdit,imgAPT) imgAPT.show() print("1 = image of coins\n2 = image of room with clock\n3 = image of color palette \n4 = image of lena") userImagePath = str(input("Kindly enter the number of image you want from above:")) if userImagePath == '1': createImages('Project-Data/coins.bmp') elif(userImagePath == '2'): createImages('Project-Data/image1.jpg') elif(userImagePath == '3'): createImages('Project-Data/image2.jpg') elif(userImagePath == '4'): createImages('Project-Data/lena.png') else: print("ERROR: values not accepted") exit(1)

thresholdingAssignment/Project-Data/coins.bmp

thresholdingAssignment/Project-Data/image1.jpg

thresholdingAssignment/Project-Data/image2.jpg

thresholdingAssignment/Project-Data/lena.png