Several people have suggested that importing OpenCV is cheating and as such the claim that MintEye was broken in 23 lines of Python is disingenuous. So, here’s a solution in 34 lines of Python:
import sys import os import matplotlib.pyplot as plt import math from PIL import Image for dir in range(1,14): dir = str(dir) total_images = len(os.listdir(dir))+1 points_sob = [] for i in xrange(1,total_images): image = Image.open(dir+'/'+str(i)+'.jpg') im = image.load() #convert to grayscale (ITU-R 601-2 luma transform) grey = [[None for _ in range(image.size[1])] for _ in range(image.size[0])] for x in xrange(image.size[0]): for y in xrange(image.size[1]): grey[x][y] = im[x,y][0]*299/1000 + im[x,y][1]*587/1000 + im[x,y][2]*114/1000 #sobel sum_of_sob = 0 for x in xrange(1,image.size[0]-1): for y in xrange(1,image.size[1]-1): gx = -grey[x-1][y-1] + grey[x+1][y-1] - 2*grey[x-1][y] + 2*grey[x+1][y] - grey[x-1][y+1] + grey[x+1][y+1] gy = -grey[x-1][y-1] - 2*grey[x][y-1] - grey[x+1][y-1] + grey[x-1][y+1] + 2*grey[x][y+1] + grey[x+1][y+1] sum_of_sob = sum_of_sob + math.sqrt(gx*gx + gy*gy) print sum_of_sob points_sob.append(sum_of_sob) res = points_sob.index(min(points_sob)) + 1 x = xrange(1,total_images) plt.plot(res,points_sob[res-1], marker='o', color='r', ls='') plt.plot(x, points_sob) #plt.savefig(dir+'.png') plt.show()
The point I’m trying to make is that Sobel is a very simple operator – that’s why it was created, as a crude approximation of the derivative of a 2D signal. In fact, the only non-trivial maths imported from OpenCV previously are the DCTs to decode the JPEGs.
Yes, Sobel is pretty simple – but IMHO in this case, Canny operator is more suitable.