def img_first_pre(self, car_pic_file): """ :param car_pic_file: 图像文件 :return:已经处理好的图像文件 原图像文件 """ if type(car_pic_file) == type(""): img = img_math.img_read(car_pic_file) else: img = car_pic_file pic_hight, pic_width = img.shape[:2] if pic_width > MAX_WIDTH: resize_rate = MAX_WIDTH / pic_width img = cv2.resize(img, (MAX_WIDTH, int(pic_hight * resize_rate)), interpolation=cv2.INTER_AREA) # 缩小图片 blur = 5 img = cv2.GaussianBlur(img, (blur, blur), 0) oldimg = img img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 转化成灰度图像 Matrix = np.ones((20, 20), np.uint8) img_opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, Matrix) img_opening = cv2.addWeighted(img, 1, img_opening, -1, 0) # 创建20*20的元素为1的矩阵 开操作,并和img重合 ret, img_thresh = cv2.threshold(img_opening, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) img_edge = cv2.Canny(img_thresh, 100, 200) # Otsu’s二值化 找到图像边缘 Matrix = np.ones((4, 19), np.uint8) img_edge1 = cv2.morphologyEx(img_edge, cv2.MORPH_CLOSE, Matrix) img_edge2 = cv2.morphologyEx(img_edge1, cv2.MORPH_OPEN, Matrix) return img_edge2, oldimg
def show_img_pre(self): if self.thread_run: return self.thread_run = False self.thread_run2 = False filename = img_math.img_read("tmp/img_contours.jpg") screenwidth = win.winfo_screenwidth() screenheight = win.winfo_screenheight() win.update() width = win.winfo_width() height = win.winfo_height() laji1 = int((screenwidth - width) / 2) laji2 = int((screenheight - height) / 2) cv2.imshow("preimg", filename) cv2.moveWindow("preimg", laji1 + 100, laji2)
def pic(self, pic_path): self.apistr = None img_bgr = img_math.img_read(pic_path) first_img, oldimg = self.predictor.img_first_pre(img_bgr) if not self.cameraflag: self.imgtk = self.get_imgtk(img_bgr) self.image_ctl.configure(image=self.imgtk) th1 = ThreadWithReturnValue(target=self.predictor.img_color_contours, args=(first_img, oldimg)) th2 = ThreadWithReturnValue(target=self.predictor.img_only_color, args=(oldimg, oldimg, first_img)) th1.start() th2.start() r_c, roi_c, color_c = th1.join() r_color, roi_color, color_color = th2.join() try: Plate = HyperLPR_PlateRecogntion(img_bgr) # print(Plate[0][0]) r_c = Plate[0][0] r_color = Plate[0][0] except: pass if not color_color: color_color = color_c if not color_c: color_c = color_color self.show_roi2(r_color, roi_color, color_color) self.show_roi1(r_c, roi_c, color_c) # self.center_window() localtime = time.asctime(time.localtime(time.time())) if not self.cameraflag: if not (r_color or color_color or r_c or color_c): self.api_ctl2(pic_path) return value = [ localtime, color_c, r_c, color_color, r_color, self.apistr, self.pic_source ] img_excel.excel_add(value) img_sql.sql(value[0], value[1], value[2], value[3], value[4], value[5], value[6]) print(localtime, "|", color_c, r_c, "|", color_color, r_color, "| ", self.apistr, "|", self.pic_source)
def img_mser(self, filename): if type(filename) == type(""): img = img_math.img_read(filename) else: img = filename oldimg = img mser = cv2.MSER_create(_min_area=600) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) regions, boxes = mser.detectRegions(gray) colors_img = [] for box in boxes: x, y, w, h = box width, height = w, h if width < height: width, height = height, width ration = width / height if w * h > 1500 and 3 < ration < 4 and w > h: cropimg = img[y:y + h, x:x + w] colors_img.append(cropimg)
def clean(self): if self.thread_run: self.cameraflag = 0 return self.thread_run = False self.thread_run2 = False self.p1.set("") img_bgr3 = img_math.img_read("pic/hy.png") self.imgtk2 = self.get_imgtk(img_bgr3) self.image_ctl.configure(image=self.imgtk2) self.r_ctl.configure(text="") self.color_ctl.configure(text="", state='enable') self.r_ct2.configure(text="") self.color_ct2.configure(text="", state='enable') self.pilImage3 = Image.open("pic/locate.png") w, h = self.pilImage3.size pil_image_resized = self.resize(w, h, self.pilImage3) self.tkImage3 = ImageTk.PhotoImage(image=pil_image_resized) self.roi_ctl.configure(image=self.tkImage3, state='enable') self.roi_ct2.configure(image=self.tkImage3, state='enable')