Advertisement
Guest User

QRlab

a guest
Jan 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. import numpy as np
  2. import cv2
  3. import glob
  4. import zbar
  5. from scipy import misc
  6. import scipy
  7. %matplotlib inline
  8. from matplotlib import pyplot as plt
  9. dirname = 'QR_images'
  10. for fname in glob.glob(dirname + '/*') :
  11. fpath = fname
  12. print(fpath)
  13. img = cv2.imread(fpath, 0)
  14. #rows,cols,channels = img.shape
  15. #print(img.shape, img.dtype)
  16. #plt.plot("qrtest", imgflat)
  17. #plt.show()
  18. #print(img)
  19. #print(list(img[:,15]))
  20. scanner = zbar.Scanner()
  21. results = scanner.scan(img)
  22. for result in results:
  23. print(result.type, result.data)
  24.  
  25.  
  26.  
  27. def marker(size):
  28. pic = np.full((round(size), round(size)), 0, dtype = np.uint8)
  29. w = size/7
  30. for x in range(int(w), int(6*w)): #horisontal white lines
  31. for y in range(int(w), int(2*w)) + range(int(5*w), int(6*w)):
  32. pic[x][y] = 255
  33. for y in range(int(w), int(6*w)): #wertical white lines
  34. for x in range(int(w), int(2*w)) + range(int(5*w), int(6*w)):
  35. pic[x][y] = 255
  36. return pic
  37.  
  38. def dist(p1, p2):
  39. return math.hypot(p1[0]-p2[0], p1[1]-p2[1])
  40.  
  41. def vector(p1, p2):
  42. vec = np.zeros(2, dtype = np.float64)
  43. vec[0] = p1[0] - p2[0]
  44. vec[1] = p1[1] - p2[1]
  45. return vec
  46.  
  47. def cosanglevec(v1, v2):
  48. mult = v1[0]*v2[0]+v1[1]*v2[1]
  49. l1 = math.hypot(v1[0], v1[1])
  50. l2 = math.hypot(v2[0], v2[1])
  51. return mult/l1/l2
  52.  
  53. def veclen(v):
  54. return math.hypot(v[0], v[1])
  55.  
  56. def getangle(cort1, cort2):
  57. vec1 = cort1[0]
  58. vec2 = cort2[0]
  59. beg1 = cort1[1]
  60. beg2 = cort2[1]
  61. end1 = cort1[2]
  62. end2 = cort2[2]
  63. if (beg1 != beg2 and beg1!= end2 and end1 != beg2 and end1 != end2):
  64. return(False, 0)
  65. if (beg1 == beg2):
  66. return (True, 0)
  67. if (end1 == end2):
  68. return (True, 180)
  69. if (-3 <= math.atan2(vec2[0], vec2[1])<=3):
  70. return (True, 270)
  71. if (-3 <= math.atan2(vec1[0], vec1[1])<=3):
  72. return (True, 90)
  73. return (False, 180) # something is wrong...
  74.  
  75.  
  76. import math
  77. for fname in sorted(glob.glob(dirname + '/*')) :
  78. rez_pic = (False, '')
  79. fpath = fname
  80. print(fpath)
  81. img = cv2.imread(fpath, 0)
  82. ret,img = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
  83. img_orig = img
  84. angle = 0;
  85. textpic = 'not read'
  86. while (angle < 360): #check every angle, +=5 degrees
  87. rows,cols = img_orig.shape
  88. M = cv2.getRotationMatrix2D((cols/2,rows/2),angle,1)
  89. img = cv2.warpAffine(img_orig,M,(cols,rows))
  90. del M
  91. size_sqr = rows/3.0
  92. while (size_sqr > rows/10): # looking for markers of different sizes
  93. mark = marker(size_sqr)
  94. #plt.imshow(mark, cmap = 'gray'), plt.show()
  95. res = cv2.matchTemplate(img, mark, cv2.TM_SQDIFF_NORMED)
  96. threshold = 0.15
  97. loc = np.where( res <= threshold)
  98. while (len(loc[0]) >= 3): # not a cycle. there will be a break in the end
  99. print(angle)
  100. print('size = ',size_sqr)
  101. if (len(loc[0]) >= 10):
  102. print ('That is a lot of points. not looking this one...')
  103. break
  104. imgcopy = img
  105. points = np.array(zip(*loc[::-1]))
  106. centers = (points + size_sqr/2).round() #centers of the makers.
  107. pairs = []
  108. i = 0
  109. while i < len(centers):
  110. j = i + 1
  111. while j < len(centers):
  112. if(dist(centers[i], centers[j])>= size_sqr):
  113. pairs.append((centers[i]-centers[j], i, j)) #vector betveen 2 points, № of point1, № of point2
  114. j += 1
  115. i += 1
  116. vecsorted = sorted(pairs, key = lambda x: math.hypot(x[0][0], x[0][1])) #sort by vector length
  117. del pairs
  118. i = 0
  119. needangle = -1 #placeholder
  120. while i < len(vecsorted):
  121. j = i + 1
  122. while j < len(vecsorted):
  123. if (veclen(vecsorted[i][0])<=veclen(vecsorted[j][0])<=veclen(vecsorted[i][0])*1.05):
  124. if (abs(cosanglevec(vecsorted[i][0], vecsorted[j][0]))<=0.087): #angle from about 85 to about 95 dgr
  125. rez = getangle(vecsorted[i], vecsorted[j])
  126. if (rez[0] == False):
  127. break
  128. needangle = rez[1]
  129. for pt in zip(*loc[::-1]):
  130. s = int(round(size_sqr))
  131. cv2.rectangle(imgcopy, pt, (pt[0] + s, pt[1] + s), 125, 1)
  132. rows,cols = imgcopy.shape
  133. M = cv2.getRotationMatrix2D((cols/2,rows/2),needangle,1)
  134. imgcopy = cv2.warpAffine(imgcopy,M,(cols,rows))
  135. del M
  136. plt.imshow(imgcopy)
  137. plt.show()
  138. scanner = zbar.Scanner()
  139. results = scanner.scan(img)
  140. for result in results:
  141. rez_pic = (True, result.data)
  142. print(rez_pic[1])
  143. if (rez_pic[0] == True):
  144. break
  145. j += 1
  146. if (rez_pic[0] == True):
  147. break
  148. i += 1
  149. if (rez_pic[0] == True):
  150. break
  151. break
  152. size_sqr = size_sqr*98/100
  153. if (rez_pic[0] == True):
  154. break
  155. angle = angle+5
  156. if (rez_pic[0] == True):
  157. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement