def smooth(image): print "smooth: "+str(image[:10]) if image[0]=="P3": import color2grayscale image=color2grayscale.color2grayscale(image) print "smooth: "+str(image[:10]) file2=image[:] #first line=P2 #second line=comment width=int(image[1]) height=int(image[2]) count=0 for x in range(1,width-1): for y in range(1,height-1): file2[width*y+x+4]=int((1*getpixel(x-1,y-1,image)+2*getpixel(x,y-1,image)+1*getpixel(x+1,y-1,image)+2*getpixel(x-1,y,image)+4*getpixel(x,y,image)+2*getpixel(x+1,y,image)+1*getpixel(x-1,y+1,image)+2*getpixel(x,y+1,image)+1*getpixel(x+1,y+1,image))/16) return file2
def floodfill(image): oldimage=image width=int(image[1]) height=int(image[2]) count=0 #loop through all non-edge pixels for x in range(1,width-1): for y in range(1,height-1): if getpixel(x,y,image)==255: image[width*y+x+4]=0 image=fill(x,y,image) pixelsum=width*height return image
def edgedetect(image): print image[:10] tvals = image[:] hg = image[:] vg = image[:] theta = image[:] global pixelsum, pixelcount oldimage = image if image[0] == "P3": # converts file to grayscale and smoothes it import smooth image = smooth.smooth(image) print image[:10] file2 = oldimage[:] width = int(image[1]) height = int(image[2]) count = 0 # loop through all non-edge pixels for x in range(1, width - 1): for y in range(1, height - 1): # calculate the horizontal gradient of the current pixel hg[width * y + x + 4] = int( -1 * getpixel(x - 1, y - 1, image) + 0 * getpixel(x, y - 1, image) + 1 * getpixel(x + 1, y - 1, image) - 2 * getpixel(x - 1, y, image) + 0 * getpixel(x, y, image) + 2 * getpixel(x + 1, y, image) - 1 * getpixel(x - 1, y + 1, image) + 0 * getpixel(x, y + 1, image) + 1 * getpixel(x + 1, y + 1, image) ) # calculate the vertical gradient of the current pixel vg[width * y + x + 4] = int( 1 * getpixel(x - 1, y - 1, image) + 2 * getpixel(x, y - 1, image) + 1 * getpixel(x + 1, y - 1, image) + 0 * getpixel(x - 1, y, image) + 0 * getpixel(x, y, image) + 0 * getpixel(x + 1, y, image) - 1 * getpixel(x - 1, y + 1, image) - 2 * getpixel(x, y + 1, image) - 1 * getpixel(x + 1, y + 1, image) ) # store the threshold value tvals[width * y + x + 4] = abs(hg[width * y + x + 4]) + abs(vg[width * y + x + 4]) theta[width * y + x + 4] = float(atan2(-vg[width * y + x + 4], hg[width * y + x + 4])) # if the threshold is reached, mark the pixel for x in range(1, width - 1): for y in range(1, height - 1): if int(tvals[width * y + x + 4]) >= hthreshold: fill(x, y, file2, tvals, theta, lthreshold) pixelsum = width * height return file2