Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import cv2
- import glob
- import zbar
- from scipy import misc
- import scipy
- %matplotlib inline
- from matplotlib import pyplot as plt
- dirname = 'QR_images'
- for fname in glob.glob(dirname + '/*') :
- fpath = fname
- print(fpath)
- img = cv2.imread(fpath, 0)
- #rows,cols,channels = img.shape
- #print(img.shape, img.dtype)
- #plt.plot("qrtest", imgflat)
- #plt.show()
- #print(img)
- #print(list(img[:,15]))
- scanner = zbar.Scanner()
- results = scanner.scan(img)
- for result in results:
- print(result.type, result.data)
- def marker(size):
- pic = np.full((round(size), round(size)), 0, dtype = np.uint8)
- w = size/7
- for x in range(int(w), int(6*w)): #horisontal white lines
- for y in range(int(w), int(2*w)) + range(int(5*w), int(6*w)):
- pic[x][y] = 255
- for y in range(int(w), int(6*w)): #wertical white lines
- for x in range(int(w), int(2*w)) + range(int(5*w), int(6*w)):
- pic[x][y] = 255
- return pic
- def dist(p1, p2):
- return math.hypot(p1[0]-p2[0], p1[1]-p2[1])
- def vector(p1, p2):
- vec = np.zeros(2, dtype = np.float64)
- vec[0] = p1[0] - p2[0]
- vec[1] = p1[1] - p2[1]
- return vec
- def cosanglevec(v1, v2):
- mult = v1[0]*v2[0]+v1[1]*v2[1]
- l1 = math.hypot(v1[0], v1[1])
- l2 = math.hypot(v2[0], v2[1])
- return mult/l1/l2
- def veclen(v):
- return math.hypot(v[0], v[1])
- def getangle(cort1, cort2):
- vec1 = cort1[0]
- vec2 = cort2[0]
- beg1 = cort1[1]
- beg2 = cort2[1]
- end1 = cort1[2]
- end2 = cort2[2]
- if (beg1 != beg2 and beg1!= end2 and end1 != beg2 and end1 != end2):
- return(False, 0)
- if (beg1 == beg2):
- return (True, 0)
- if (end1 == end2):
- return (True, 180)
- if (-3 <= math.atan2(vec2[0], vec2[1])<=3):
- return (True, 270)
- if (-3 <= math.atan2(vec1[0], vec1[1])<=3):
- return (True, 90)
- return (False, 180) # something is wrong...
- import math
- for fname in sorted(glob.glob(dirname + '/*')) :
- rez_pic = (False, '')
- fpath = fname
- print(fpath)
- img = cv2.imread(fpath, 0)
- ret,img = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
- img_orig = img
- angle = 0;
- textpic = 'not read'
- while (angle < 360): #check every angle, +=5 degrees
- rows,cols = img_orig.shape
- M = cv2.getRotationMatrix2D((cols/2,rows/2),angle,1)
- img = cv2.warpAffine(img_orig,M,(cols,rows))
- del M
- size_sqr = rows/3.0
- while (size_sqr > rows/10): # looking for markers of different sizes
- mark = marker(size_sqr)
- #plt.imshow(mark, cmap = 'gray'), plt.show()
- res = cv2.matchTemplate(img, mark, cv2.TM_SQDIFF_NORMED)
- threshold = 0.15
- loc = np.where( res <= threshold)
- while (len(loc[0]) >= 3): # not a cycle. there will be a break in the end
- print(angle)
- print('size = ',size_sqr)
- if (len(loc[0]) >= 10):
- print ('That is a lot of points. not looking this one...')
- break
- imgcopy = img
- points = np.array(zip(*loc[::-1]))
- centers = (points + size_sqr/2).round() #centers of the makers.
- pairs = []
- i = 0
- while i < len(centers):
- j = i + 1
- while j < len(centers):
- if(dist(centers[i], centers[j])>= size_sqr):
- pairs.append((centers[i]-centers[j], i, j)) #vector betveen 2 points, № of point1, № of point2
- j += 1
- i += 1
- vecsorted = sorted(pairs, key = lambda x: math.hypot(x[0][0], x[0][1])) #sort by vector length
- del pairs
- i = 0
- needangle = -1 #placeholder
- while i < len(vecsorted):
- j = i + 1
- while j < len(vecsorted):
- if (veclen(vecsorted[i][0])<=veclen(vecsorted[j][0])<=veclen(vecsorted[i][0])*1.05):
- if (abs(cosanglevec(vecsorted[i][0], vecsorted[j][0]))<=0.087): #angle from about 85 to about 95 dgr
- rez = getangle(vecsorted[i], vecsorted[j])
- if (rez[0] == False):
- break
- needangle = rez[1]
- for pt in zip(*loc[::-1]):
- s = int(round(size_sqr))
- cv2.rectangle(imgcopy, pt, (pt[0] + s, pt[1] + s), 125, 1)
- rows,cols = imgcopy.shape
- M = cv2.getRotationMatrix2D((cols/2,rows/2),needangle,1)
- imgcopy = cv2.warpAffine(imgcopy,M,(cols,rows))
- del M
- plt.imshow(imgcopy)
- plt.show()
- scanner = zbar.Scanner()
- results = scanner.scan(img)
- for result in results:
- rez_pic = (True, result.data)
- print(rez_pic[1])
- if (rez_pic[0] == True):
- break
- j += 1
- if (rez_pic[0] == True):
- break
- i += 1
- if (rez_pic[0] == True):
- break
- break
- size_sqr = size_sqr*98/100
- if (rez_pic[0] == True):
- break
- angle = angle+5
- if (rez_pic[0] == True):
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement