def test_identity(self): im = Image.open("/u/konolige/vslam/data/indoor1/left-%04d.ppm" % 1000) kp = [(x-16, y-16) for (x,y) in fast.fast(im.tostring(), im.size[0], im.size[1], 150, 40)] cl1 = calonder.classifier() cl1.train(im.tostring(), im.size[0], im.size[1], kp) cl1.write('unittest.tree') def testclassifier(kp, im, cl): ma = calonder.BruteForceMatcher() sigs = [] for (x,y) in kp: patch = im.crop((x,y,x+32,y+32)) sig = cl.getSignature(patch.tostring(), patch.size[0], patch.size[1]) #print ["%3f" % x for x in sig.dump()] sigs.append(sig) ma.addSignature(sig) for (i,(x,y)) in enumerate(kp): patch = im.crop((x,y,x+32,y+32)) sig = cl.getSignature(patch.tostring(), patch.size[0], patch.size[1]) (index, distance) = ma.findMatch(sig) self.assert_(i == index) testclassifier(kp, im, cl1) del cl1 # Now make another classifier, and read it from the file above cl2 = calonder.classifier() cl2.read('unittest.tree') testclassifier(kp, im, cl2)
def decrypt(C, d, N): """Decrypt cipher with d block by block and convert them to ASCII.""" message = [] for c in C: msg = f.fast(c, d, N) message.append(str(unichr(msg))) return message
def fermat(n): for i in range(10): # δημιουργία τυχαίου αριθμού βάσης a = random.randint(1, n - 1) gcd = fast.fast(a, n - 1, n) # a * 1 mod n # εαν το gcd(a,n) ειναι 1 το ο n ειναι πρωτος if gcd == 1: return True else: #επιστρέφει false εαν ο αριθμός ειναι σύνθετος return False return True
def fermatTest(n, k): """ Fermat primality test n: The number to test k: Number of times to repeat the test Returns: True if prime else False """ for i in range(k): a = randint(2, n-2) if fast(a,n-1,n) != 1: return False return True
def decrypt(C, ds, N): """ Decrypts array of integers C, using d and N of RSA """ f = open("message.txt", "w") for d in ds: f.write("==== D: " + str(d) + " ====\n") try: for c in C: m = fast.fast(c, d, N) f.write(str(chr(m))) f.write("\n") except: f.write("FAILED") f.write("\n")
def kp_d(frame): fkp = fast.fast(frame.tostring(), frame.size[0], frame.size[1], 10, 15) fkp = [(x, y, r) for (x, y, r) in fkp if (16 <= x and 16 <= y and x <= frame.size[0] - 16 and y <= frame.size[1] - 16)] fkp = fast.nonmax(fkp) #damn this is slow kp = [(x, y) for ( x, y, r) in sorted(fkp, key=lambda x: (x[2], x[0], x[1]), reverse=True)[:200] ] print 'kp_d gave %d points' % len(kp) descriptors = [] for (x, y) in kp: patch = frame.crop((x - 16, y - 16, x + 16, y + 16)) sig = cl.getSignature(patch.tostring(), patch.size[0], patch.size[1]) descriptors.append(sig) return descriptors
def kp_d(frame): #sd = starfeature.star_detector(frame.size[0], frame.size[1], 5, 10.0, 10.0) #kp = [ (x,y) for (x,y,s,r) in sd.detect(frame.tostring()) ] fkp = fast.fast(frame.tostring(), frame.size[0], frame.size[1], 15, 9) fkp = [(x, y, r) for (x, y, r) in fast.nonmax(fkp) if (16 <= x and 16 <= y and x <= frame.size[0] - 16 and y <= frame.size[1] - 16)] kp = [(x, y) for (x, y, r) in sorted( fkp, key=lambda x: (x[2], x[0], x[1]), reverse=True)[:500]] print 'kp_d gave %d points' % len(kp) descriptors = [] for (x, y) in kp: patch = frame.crop((x - 16, y - 16, x + 16, y + 16)) sig = cl.getSignature(patch.tostring(), patch.size[0], patch.size[1]) descriptors.append(sig) return descriptors
def get_features(frame, classifier): # Get keypoints from FAST detector fkp = fast.fast(frame.tostring(), frame.size[0], frame.size[1], 12, 9) # Keep local maxima that are at 16 pixels away from image border fkp = [(x, y, r) for (x, y, r) in fast.nonmax(fkp) if (16 <= x and 16 <= y and x <= frame.size[0] - 16 and y <= frame.size[1] - 16)] # Keep at most 500 strongest keypoints kp = [(x, y) for ( x, y, r) in sorted(fkp, key=lambda x: (x[2], x[0], x[1]), reverse=True)[:500] ] # Compute Calonder descriptor for each keypoint descriptors = [] for (x, y) in kp: patch = frame.crop((x - 16, y - 16, x + 16, y + 16)) sig = classifier.getSignature(patch.tostring(), patch.size[0], patch.size[1]) descriptors.append(sig) return descriptors, kp
def get_features(self, frame, target_points): assert len(frame.rawdata) == (frame.size[0] * frame.size[1]) return fast.fast(frame.rawdata, frame.size[0], frame.size[1], int(self.thresh), 40)
# Σειρά παίρνει ο έλεγχος για ποιοι πρώτοι αριθμοί αν τους πολλαπλασιασουμε βγάζουν γινομενο Ν for i in range(len(prime_factors)): for j in range(1, len(prime_factors)): # Έλεγχος αν οι αριθμοί βγάζουν γινόμενο N if (prime_factors[i] * prime_factors[j] == N): p = prime_factors[i] q = prime_factors[j] # Βρίσκουμε το fn fn = (p - 1) * (q - 1) # τώρα βρισκουμε το κλειδι με το οποίο έγινε η κρυπτογράφηση # θέλουμε έναν αριθμό x δηλαδη τέτοιον ώστε: e * x mod fn = 1 for x in range(fn): if ((e * x) % fn == 1): d = x msg = '' # το i ειναι το καθε block του c # για κάθε μπλοκ του c i^d mod N με d private key for i in C: # print(i)--> Το εσωτερικό του C # αποκρυπτογραφούμε block by block όπως λέει η εκφώνηση # 3203 --> αντιστοιχεί στο πρτο γραμμα του μηνυματος κοκ.. k = fast.fast(i, d, N) # chr() επιστρέφει το integer k στη unicode μορφή του # κάθε φορά προσθέτουμε στο μήνυμα το επόμενο αποκρυπτογραφημένο γράμμα του C msg += chr(k) # print(msg) # για ελεγχο print(f"To encrypted mhnyma einai: {msg}")
def test_identity(self): im = Image.open("f0-left.png") kp = [(x-16, y-16) for (x,y,r) in fast.fast(im.tostring(), im.size[0], im.size[1], 55, 0) if (x > 32) and (y > 32) and (x < (640-32)) and (y < (480-32))] print "keypoints", len(kp) dim = 176 # actual dimension will be min(176, |kp|) cl1 = calonder.classifier() if 0: cl1.train(im.tostring(), im.size[0], im.size[1], kp, 50, 10, 100, dim, 0) else: # CMakefile.txt downloads current.rtc before this test runs filename = 'current.rtc' cl1.read(filename) dim = cl1.dimension() if 0: for i in range(1000000): print i sigs = cl1.getSignatures(im.size, im.tostring(), kp) #for (x,y) in kp: # patch = im.crop((x,y,x+32,y+32)) # sig = cl1.getSignature(patch.tostring(), patch.size[0], patch.size[1]) return def testclassifier(kp, im, cl): ma = calonder.BruteForceMatcher(dim) sigs = [] kp = kp[:] for (x,y) in kp: print x,y patch = im.crop((x,y,x+32,y+32)) sig = cl.getSignature(patch.tostring(), patch.size[0], patch.size[1]) sigs.append(sig) ma.addSignature(sig) #print cl.getSignatures(im, kp) for (i,(x,y)) in enumerate(kp): patch = im.crop((x,y,x+32,y+32)) sig = cl.getSignature(patch.tostring(), patch.size[0], patch.size[1]) (index, distance) = ma.findMatch(sig) print "i = %d, match = %u, distance = %.3f" % (i, index, distance) self.assert_(i == index) testclassifier(kp, im, cl1) print "done" print "Writing to unittest.tree... ", cl1.write('unittest.tree') print "done" del cl1 # Now make another classifier, and read it from the file above cl2 = calonder.classifier() print "Reading classifier... ", cl2.read('unittest.tree') print "done" testclassifier(kp, im, cl2) f = open('sigs.pickle', 'w') for (x,y) in kp[:3]: patch = im.crop((x,y,x+32,y+32)) sig = cl2.getSignature(patch.tostring(), patch.size[0], patch.size[1]) pickle.dump(sig, f) print "saved", sig f.close() f = open('sigs.pickle') for i in range(3): print "loaded", pickle.load(f)
def powerMod(a,g,N): b = a % g for i in range(g-1): b = (a * b) % N return b print("=== (i) ===") (g, u ,v) = gcd(126048, 5050) print("GCD(126048, 5050) = " + str(g)) print(str(int(u)) + " " + str(int(v))) print("") print("=== (ii) ===") for i in range(1002): if (i*809)%1001 == 1: print(i) break print("") print("=== (iii) ===") print("2^(100)mod101 = " + str(powerMod(2,100,101))) print("") print("=== (iv) ===") print("2^(1234567)mod12345 = " + str(fast.fast(2,1234567,12345))) print("130^(7654321)mod567 = " + str(fast.fast(130,7654321,567))) print("")
def FAST(imdata, xsize, ysize, thresh, barrier=9): kp = fast.fast(imdata, xsize, ysize, barrier, int(thresh)) return sorted(fast.nonmax(kp), key=lambda x: (x[2], x[0], x[1]), reverse=True)
def decrypt(c, d, n): """decrypting function""" return fast(c, d, n)
def encrypt(m, e, n): """encrypting function""" return fast(m, e, n)