''' MO815 - Topicos em Processamento de Imagens 1s 2003 Professor Neucimar J. Leite 14/04/2003 Usando propriedade: E(f, D(b, c)) = E(E(f, b), c) ''' def zncc(c1, c2): from Numeric import sqrt, sum L = min(len(c1), len(c2)) c1 = c1[:L] c2 = c2[:L] s1 = sum(c1) s2 = sum(c2) s11 = sum(c1*c1) s22 = sum(c2*c2) s12 = sum(c1*c2) num = L*s12 - s1*s2 den = sqrt((L*s11 - s1*s1) * (L*s22 - s2*s2)) return num/den def find(dict, c, log, cc=0.999998): ''' dict: a dictionary like dict = [['image1.jpg', array([4.453, 4.423])], ['image2.jpg', array([3.453, 7.423])]] c: curve, that an array to be compared to each one in the dictionary log: log file cc: correlation criteria. An image is considered equal to another if their ZNCC is greater then cc. ''' pattern = "NOT FOUND" log.writelines(["Criteria: corr > " + str(cc) + "\n", "Iterations: " + str(len(c)) + "\n"]) for img in dict: corr = zncc(img[1], c) # log.write("corr('" + img[0] + "') = " + str(corr) + "\n") log.write(".") if corr > cc: pattern = img[0] break log.write("\n") return pattern def zncctest(): from Numeric import array dict = [[IMAGES_PATH + 'g_Brickm.jpg', array([0.0532445311546,0.169468834996,0.235978707671,0.278851121664,0.309265762568, 0.332554519176,0.351151943207,0.36665686965,0.379768908024,0.391187399626])], [IMAGES_PATH + 'g_Brkwea_t.jpg', array([0.0150678493083,0.0515709519386,0.0824776962399,0.110843315721,0.136748328805, 0.160955861211,0.183814242482,0.205334752798,0.225155994296,0.243682578206])]] c = array([0.0150678493083,0.0515709519386,0.0824776962399,0.110843315721,0.136748328805, 0.160955861211,0.183814242482,0.205334752798,0.225155994296,0.243682578206]) if find(dict, c) == IMAGES_PATH + 'g_Brkwea_t.jpg': print 'OK' else: print 'FUCK' return