cv2img.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #-*-coding:utf-8 -*-
  2. import cv2
  3. import numpy as np
  4. from PIL import Image
  5. import imutils
  6. from imutils import contours
  7. from imutils.perspective import four_point_transform
  8. import os
  9. import json
  10. from PIL import Image,ImageDraw
  11. import pyzbar.pyzbar as pbar
  12. class CV2img:
  13. """识别出试卷上的类答题卡区域
  14. """
  15. def __init__(self,image_path="test.jpg",size="A4"):
  16. self.image_path = image_path
  17. self.image = cv2.imread(self.image_path)
  18. #图片的宽高度
  19. self.width = self.image.shape[1]
  20. self.height = self.image.shape[0]
  21. #图片转成灰度图
  22. self.gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)
  23. #边缘检测后的图
  24. #self.edged = cv2.Canny(self.gray, 75, 200)
  25. self.blur = cv2.GaussianBlur(self.gray,(5,5),0)
  26. #对灰度图进行二值化处理
  27. #ret,thresh = cv2.threshold(self.gray,0,255,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
  28. ret,thresh = cv2.threshold(self.blur,0,255,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
  29. #ret,thresh = cv2.threshold(self.gray,0,100,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
  30. self.thresh = thresh
  31. #if size == "A4":
  32. # self.thresh = cv2.resize(thresh, (2480, 3480), interpolation=cv2.INTER_LANCZOS4)
  33. #else:
  34. # self.thresh = cv2.resize(thresh, (4870, 3480), interpolation=cv2.INTER_LANCZOS4)
  35. #输出二值化操作后的图像
  36. spt_lst = os.path.splitext(self.image_path)
  37. close_path = spt_lst[0] + '_thresh_new' + spt_lst[1]
  38. cv2.imwrite(close_path,self.thresh)
  39. def _erode(self,w,h):
  40. '''
  41. 图像腐蚀操作
  42. '''
  43. #腐蚀核大小
  44. edsize = cv2.getStructuringElement(cv2.MORPH_RECT,(w,h))
  45. self.erode_image = cv2.erode(self.thresh,edsize)
  46. #输出腐蚀操作后的图像
  47. spt_lst = os.path.splitext(self.image_path)
  48. close_path = spt_lst[0] + '_erode' + spt_lst[1]
  49. cv2.imwrite(close_path,self.erode_image)
  50. return self.erode_image
  51. def _erode_dilate(self,w,h):
  52. '''
  53. 图像腐蚀操作
  54. '''
  55. #腐蚀核大小
  56. edsize = cv2.getStructuringElement(cv2.MORPH_RECT,(w,h))
  57. self.erode_dilate_image = cv2.dilate(self.erode_image,edsize)
  58. #输出腐蚀操作后的图像
  59. spt_lst = os.path.splitext(self.image_path)
  60. close_path = spt_lst[0] + '_erode_dilate' + spt_lst[1]
  61. cv2.imwrite(close_path,self.erode_dilate_image)
  62. return self.erode_dilate_image
  63. def _dilate(self,w,h):
  64. '''
  65. 图像膨胀操作
  66. '''
  67. dilsize = cv2.getStructuringElement(cv2.MORPH_RECT,(w,h))
  68. self.dilate_image = cv2.dilate(self.thresh,dilsize)
  69. #输出膨胀操作后的图像
  70. spt_lst = os.path.splitext(self.image_path)
  71. close_path = spt_lst[0] + '_dilate' + spt_lst[1]
  72. cv2.imwrite(close_path,self.dilate_image)
  73. return self.dilate_image
  74. def _dilate_erode(self,w,h):
  75. '''
  76. 图像膨胀操作
  77. '''
  78. dilsize = cv2.getStructuringElement(cv2.MORPH_RECT,(w,h))
  79. self.dilate_erode_image = cv2.erode(self.dilate_image,dilsize)
  80. #输出膨胀操作后的图像
  81. spt_lst = os.path.splitext(self.image_path)
  82. close_path = spt_lst[0] + '_dilate_erode' + spt_lst[1]
  83. cv2.imwrite(close_path,self.dilate_erode_image)
  84. return self.dilate_erode_image
  85. def _open(self,w=10,h=10):
  86. """开操作先腐蚀后膨胀
  87. """
  88. self._erode(w,h)
  89. #自定义开操作
  90. self.open_image = self._erode_dilate(w,h)
  91. #输出闭操作后的图像
  92. spt_lst = os.path.splitext(self.image_path)
  93. close_path = spt_lst[0] + '_open' + spt_lst[1]
  94. cv2.imwrite(close_path,self.open_image)
  95. return self.open_image
  96. def _close(self,w=20,h=40):
  97. """闭操作先膨胀后腐蚀
  98. """
  99. self._dilate()
  100. #自定义闭操作
  101. self.close_image = self._dilate_erode(w,h)
  102. #输出闭操作后的图像
  103. spt_lst = os.path.splitext(self.image_path)
  104. close_path = spt_lst[0] + '_close' + spt_lst[1]
  105. cv2.imwrite(close_path,self.close_image)
  106. return self.close_image
  107. def get_cnts(self,std_w=None,std_h=None):
  108. """识别出答案轮廓
  109. """
  110. self._open(std_w,std_h)
  111. cnts = cv2.findContours(self.open_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0]
  112. #cnts = cv2.findContours(self.open_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
  113. #cnts = cnts[0] if imutils.is_cv2() else cnts[0]
  114. all_c = []
  115. if len(cnts) > 0:
  116. cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
  117. all_c = []
  118. for c in cnts:
  119. peri = 0.01*cv2.arcLength(c, False)
  120. approx = cv2.approxPolyDP(c, peri, True)
  121. if len(approx)==4 or True:
  122. (x, y, w, h) = cv2.boundingRect(c)
  123. #标识出识别出的机型轮廓并输出
  124. cv2.drawContours(self.image,[c],-1,(255,0,255),3)
  125. spt_lst = os.path.splitext(self.image_path)
  126. draw_path = spt_lst[0] + '_draw' + spt_lst[1]
  127. cv2.imwrite(draw_path,self.image)
  128. all_c.append({'x':x,'y':y,'w':w,'h':h})
  129. return all_c
  130. def get_std_points(self,ow,oh):
  131. """
  132. """
  133. std_points = []
  134. cnts = self.get_cnts(ow,oh)
  135. for c in cnts:
  136. std_points.append(c)
  137. std_points = sorted(std_points,key=lambda x:x["x"]+x["y"])
  138. return std_points
  139. def rec_ans(self,std_ans_list):
  140. """
  141. """
  142. ans_list = []
  143. ans_dct = {0:"A",1:"B",2:"C",3:"D"}
  144. for index,point in enumerate(std_ans_list):
  145. x1 = point["x"]
  146. y1 = point["y"]
  147. x2 = point["x"]+point["w"]
  148. y2 = point["y"]+point["h"]
  149. count = 0
  150. roi = self.open_image[y1:y2,x1:x2]
  151. width = roi.shape[0]
  152. height = roi.shape[1]
  153. for i in range(width):
  154. for j in range(height):
  155. if roi[i,j] == 255:
  156. count += 1
  157. if count > 80:
  158. ans_list.append(ans_dct[index])
  159. return ",".join(ans_list)
  160. def get_kh_qrcode(self):
  161. """
  162. """
  163. self._dilate(10,10)
  164. cnts = cv2.findContours(self.dilate_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0]
  165. if len(cnts) > 0:
  166. cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
  167. all_c = []
  168. for c in cnts:
  169. peri = 0.01*cv2.arcLength(c, False)
  170. approx = cv2.approxPolyDP(c, peri, True)
  171. if len(approx)==4 or True:
  172. (x, y, w, h) = cv2.boundingRect(c)
  173. if w > 100 and h >50:
  174. return x,y,w,h
  175. ##标识出识别出的机型轮廓并输出
  176. #cv2.drawContours(self.image,[c],-1,(255,0,255),3)
  177. #spt_lst = os.path.splitext(self.image_path)
  178. #draw_path = spt_lst[0] + '_draw' + spt_lst[1]
  179. #cv2.imwrite(draw_path,self.image)
  180. #all_c.append({'x':x,'y':y,'w':w,'h':h})
  181. return all_c
  182. def rec_qrcode_kh(self):
  183. """识别二维码考号信息
  184. """
  185. def rec_bar_code(imgpath):
  186. """识别条码
  187. """
  188. image = cv2.imread(imgpath)
  189. barcodes = pbar.decode(image)
  190. barcode = barcodes[0]
  191. barcode_data = barcode.data.decode("utf-8")
  192. return barcode_data
  193. def crop_img_local(orgimg, point=(),i=0):
  194. """
  195. 切割图片
  196. """
  197. img = Image.open(orgimg)
  198. region = img.crop(point)
  199. new_img = "/tmp/" + os.path.split(orgimg)[-1].replace(".", "_crop{}.".format(i))
  200. region.save(new_img, format="png")
  201. return new_img
  202. def get_std_points(imgpath):
  203. img = cv2.imread(imgpath)
  204. h,w = img.shape[:2]
  205. img1 = crop_img_local(imgpath,(0,0,w/8,h/8),1)
  206. img_dect = CV2img(img1)
  207. std_points = img_dect.get_std_points(10,10)
  208. return std_points
  209. def rec_ans(imgpath,std_ans_list):
  210. img_dect = CV2img(imgpath)
  211. img_dect._open()
  212. ans = img_dect.rec_ans(std_ans_list)
  213. return ans
  214. def rec_qrcode_kh(imgpath,point):
  215. """识别二维码考号信息
  216. """
  217. std_points = get_std_points(imgpath)
  218. std_x = std_points[0]["x"]
  219. std_y = std_points[0]["y"]
  220. x,y,w,h = point["x"]+std_x,point["y"]+std_y,point["w"],point["h"]
  221. img1 = crop_img_local(imgpath,(x,y,x+w,y+h),1)
  222. img_dect = CV2img(img1)
  223. x,y,w,h = img_dect.get_kh_qrcode()
  224. img2 = crop_img_local(img1,(x,y,x+w,y+h),2)
  225. kh = rec_bar_code(img2)
  226. def main():
  227. std_points = get_std_points("/tmp/31_test01.png")
  228. if __name__ == "__main__":
  229. imgpath = "anscard.png"
  230. #std_ans_list = [{"h":22,"w":31,"x":78,"y":27,"z":82},{"h":22,"w":31,"x":123,"y":27,"z":125},{"h":23,"w":28,"x":173,"y":27,"z":175},{"h":22,"w":25,"x":219,"y":27,"z":220}]
  231. #ans = rec_ans(imgpath,std_ans_list)
  232. imgpath = "/tmp/33_test05.png"
  233. kh_point = {"h": 252.12448132780085, "w": 1558, "x": 12.506224066390049, "y": 223.84232365145232, "page": 0}
  234. rec_qrcode_kh(imgpath,kh_point)