def mser(img, str): if str == 'blue': mser = cv2.MSER_create(_min_area=100, _max_area=1000) if str == 'red': mser = cv2.MSER_create(_min_area=400, _max_area=800) regions, _ = mser.detectRegions(img) return regions
def do_MSER(frame, stop_contours): id_red, id_blue = process_image(frame) red_frame = frame[:, :, 2] blue_frame = frame[:, :, 0] height = np.shape(id_red)[0] width = np.shape(id_red)[1] new_red = cv2.rectangle(id_red, (np.float32(0), np.float32( (height / 2))), (np.float32(width), np.float32(height)), (0, 0, 0), -1) new_red = cv2.rectangle(new_red, (np.float32(0), np.float32( (0))), (np.float32(width / 2), np.float32(250)), (0, 0, 0), -1) new_blue = cv2.rectangle(id_blue, (np.float32(0), np.float32(height / 2)), (np.float32(width), np.float32(height)), (0, 0, 0), -1) new_red = new_red.astype(np.uint8) new_blue = new_blue.astype(np.uint8) mser_blue = cv2.MSER_create(5, 300, 9000, 0.25, 0.2, 200, 1.01, 0.003, 5) mser_red = cv2.MSER_create(7, 400, 7000, 0.25, 0.2, 200, 1.01, 0.003, 5) red_region, red_box = mser_red.detectRegions(new_red) blue_region, blue_box = mser_blue.detectRegions(new_blue) red_hull = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in red_region] blue_hull = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in blue_region] mask = np.zeros((frame.shape[0], frame.shape[1], 1), dtype=np.uint8) mask_new = np.zeros((frame.shape[0], frame.shape[1]), dtype=np.uint8) roi_blue = [] for contour in blue_hull: x_blue, y_blue, w, h = cv2.boundingRect(contour) aspect_ratio = float(w) / h if aspect_ratio < 1: blue_binary = cv2.drawContours(mask, [contour], -1, (255, 255, 255), -1) roi_blue = frame[y_blue:y_blue + h, x_blue:x_blue + w] roi_red = [] for contour in red_hull: area = cv2.contourArea(contour) x_red, y_red, w, h = cv2.boundingRect(contour) aspect_ratio = float(w) / h norm_w = w / (w + h) norm_h = h / (w + h) compare = cv2.matchShapes(contour, stop_contours, 1, 0.0) if aspect_ratio > 1 and abs( norm_w - norm_h ) < 0.3 and area > 1000 and area < 2000 and cv2.isContourConvex( contour) and compare < 0.1: red_binary = cv2.drawContours(mask_new, [contour], -1, (255, 255, 255), -1) roi_red = frame[y_red:y_red + h, x_red:x_red + w] return mask, mask_new, roi_blue, roi_red
def mser_extract_regions(cv_image, lower_color_bound, upper_color_bound) -> [[Region, int]]: image = cv_image.copy() lower_bound = np.array(lower_color_bound) upper_bound = np.array(upper_color_bound) mask = cv2.inRange(image, lower_bound, upper_bound) mask_rgb = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) masked_image = image & mask_rgb mser = cv2.MSER_create(_min_area=250, _max_area=50000, _max_evolution=50000) regions, _ = mser.detectRegions(masked_image) detected_regions = [] for p in regions: xmax, ymax = np.amax(p, axis=0) xmin, ymin = np.amin(p, axis=0) detected_regions.append([Region(xmax, xmin, ymax, ymin), 0]) return detected_regions
def __init__(self, npImage=None): self.img = npImage # image as numpy array self.mser = cv2.MSER_create(_max_variation=10) self.regions = None if npImage is not None: self.imageY = self.img.shape[0] self.imageX = self.img.shape[1]
def mser(image): #Detecting Text Regions #Create MSER object mser = cv2.MSER_create() vis = image.copy() regions, _ = mser.detectRegions(image) #Use to extend the text regions to extend over sharp turns hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions] cv2.polylines(vis, hulls, 1, (0, 255, 0)) cv2.imshow('img', vis) mask = np.zeros((image.shape[0], image.shape[1], 1), dtype=np.uint8) for contour in hulls: cv2.drawContours(mask, [contour], -1, (255, 255, 255), -1) #this is used to find only text regions, remaining are ignored text_only = cv2.bitwise_and(image, image, mask=mask) cv2.imshow("text only", text_only) cv2.waitKey(0) return text_only
def __init__(self, ratio_matching = 0.5, ratio_pose = 0.5, detecteur = 'fast', descripteur = 'orb'): self.ratio_matching = ratio_matching self.ratio_pose = ratio_pose self.detecteur = detecteur self.descripteur = descripteur self.detecteurs = { 'akaze': cv2.AKAZE_create(), 'agast': cv2.AgastFeatureDetector_create(), 'brisk': cv2.BRISK_create(), 'fast' : cv2.FastFeatureDetector_create(), 'gftt' : cv2.GFTTDetector_create(), 'kaze' : cv2.KAZE_create(), 'mser' : cv2.MSER_create(), 'orb' : cv2.ORB_create(), 'blob' : cv2.SimpleBlobDetector_create() } self.descripteurs = {'brisk': cv2.BRISK_create(), 'orb' : cv2.ORB_create(),} self.detector = self.detecteurs[self.detecteur] self.descriptor = self.descripteurs[self.descripteur] self.matcheur = cv2.BFMatcher(normType = self.descriptor.defaultNorm(), crossCheck = True ) self.KF = [] self.current_kf = None self.traj = [] self.current_time = 0 self.prev_pts = None print 'construction du SLAM'
def detectRegion(self): if '3.0' in cv2.__version__: msers = cv2.MSER_create().detectRegions(self.image, None) elif '2.4' in cv2.__version__: msers = cv2.MSER().detect(self.image, None) """ MSER + α の候補を格納""" BBs = [] for bb in msers: pt1 = tuple(np.min(bb, 0)) pt2 = tuple(np.max(bb, 0)) bb_width = np.abs(pt2[0] - pt1[0]) bb_height = np.abs(pt2[1] - pt1[1]) #BBs.append(BB(pt1[0], pt1[1], bb_width, bb_height)) for pad in (10, 20, 30, 35): BBs.append( BB(pt1[0] - pad, pt1[1] - pad, bb_width + pad * 2, bb_height + pad * 2)) BBs.append( BB(pt1[0] - pad, pt2[1] - pad, bb_width + pad * 2, bb_width + pad * 2)) BBs.append( BB(pt1[0] - pad, pt1[1] - pad - bb_width, bb_width + pad * 2, bb_width + pad * 2)) BBs.append( BB(pt1[0] - pad, pt1[1] - pad, bb_height + pad * 2, bb_height + pad * 2)) BBs.append( BB(pt1[0] - bb_height - pad, pt1[1] - pad, bb_height + pad * 2, bb_height + pad * 2)) return BBs
def feature_detector_create(self, detector_name, adaptation): if int(self.OPENCV_MAJOR) < 3: name = adaptation + detector_name detector = cv2.FeatureDetector_create(name) else: if detector_name == DetectorType.ORB: detector = cv2.ORB(adaptation) elif detector_name == DetectorType.FAST: # noinspection PyUnresolvedReferences detector = cv2.FastFeatureDetector_create() elif detector_name == DetectorType.STAR: # noinspection PyUnresolvedReferences detector = cv2.xfeatures2d.StarDetector_create() elif detector_name == DetectorType.MSER: # noinspection PyUnresolvedReferences detector = cv2.MSER_create() elif detector_name == DetectorType.GFTT: # noinspection PyUnresolvedReferences detector = cv2.GFTTDetector_create() elif detector_name == DetectorType.HARRIS: # noinspection PyUnresolvedReferences detector = cv2.xfeatures2d.HarrisLaplaceFeatureDetector_create() elif detector_name == DetectorType.BLOB: # noinspection PyUnresolvedReferences detector = cv2.SimpleBlobDetector_create() else: # detector.detector() == DetectorType.BRISK: detector = cv2.BRISK(adaptation) return detector
def segmentPlate2(roi): characters = [] column_list = [] mser = cv2.MSER_create() area = roi.shape[0] * roi.shape[1] # Resize the image so that MSER can work better img = cv2.resize(roi, (roi.shape[1] * 2, roi.shape[0] * 2)) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) Threshold = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2) kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) Mor = cv2.morphologyEx(Threshold, cv2.MORPH_CLOSE, kernel) vis = img.copy() regions = mser.detectRegions(Mor) for p in regions[0]: if cv2.contourArea(p) > 50: [x, y, w, h] = cv2.boundingRect(p) cv2.rectangle(vis, (x, y), (x + w, y + h), color=(0, 0, 255), thickness=2) roiCharacter = Mor[y:y + h, x:x + w] / 255 roiCharacter = resize(roiCharacter, (20, 20)) characters.append(roiCharacter) column_list.append(x) # hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions[0]] # cv2.polylines(vis, hulls, 1, (0, 255, 0)) return vis, characters, column_list
def getFeaturesByDetector(mode,img): detector = cv2.SimpleBlobDetector_create() if mode == featuresDetector.Agast: detector = cv2.AgastFeatureDetector_create() elif mode == featuresDetector.AKAZE: detector = cv2.AKAZE_create() elif mode == featuresDetector.BRISK: detector = cv2.BRISK_create() elif mode == featuresDetector.FAST: detector = cv2.FastFeatureDetector_create() elif mode == featuresDetector.KAZE: detector = cv2.KAZE_create() elif mode == featuresDetector.MSER: detector = cv2.MSER_create() elif mode == featuresDetector.ORB: detector = cv2.ORB_create() elif mode == featuresDetector.SIFT: detector = cv2.xfeatures2d.SIFT_create() elif mode == featuresDetector.SimpleBlob: detector = cv2.SimpleBlobDetector_create() keypoints = detector.detect(img) descriptors = detector.compute(img, keypoints) return descriptors
def get_mser(img): # check if image is grayscale if len(img.shape) == 3: gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) elif len(img.shape) == 1: gray = img else: raise ValueError( 'Invalid shape for image in get_mser.py, got image with shape %s' % str(img_shape)) (rows, cols) = gray.shape num_pixels = rows * cols # valid mser must have num pixels > 0.01% of image and < 40% of image min_px = int((0.01 / 100.0) * num_pixels) max_px = int(0.4 * num_pixels) # delta is set to 1 to maximise recall mser = cv2.MSER_create(_delta=1, _min_area=min_px, _max_area=max_px) (regions, bboxes) = mser.detectRegions(gray) return (regions, bboxes)
def crop_img(img, scale=1.0,name='default'): center_x, center_y = img.shape[1] / 2, img.shape[0] / 2 width_scaled, height_scaled = img.shape[1] , img.shape[0] * scale left_x, right_x = 0 , img.shape[1] top_y, bottom_y = 0, center_y + height_scaled / 2 img_cropped = img[int(top_y):int(bottom_y), int(left_x):int(right_x)] img_remove=img[int(bottom_y):int(img.shape[0]), int(left_x):int(right_x)] mser = cv2.MSER_create() vis=img_remove.copy() gray=cv2.cvtColor(img_remove,cv2.COLOR_BGRA2GRAY) regions,_=mser.detectRegions(gray) hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions] cv2.polylines(vis, hulls, 1, (0, 255, 0)) im_bw= cv2.cvtColor(vis, cv2.COLOR_RGB2GRAY) area= cv2.minMaxLoc(im_bw) hit=area[3][0] width=img.shape[1] f=hit/width print('image rate %s' % f) # if f>0.700: # print('if') # cv2.imwrite('data/crop4/%s' % name ,img_cropped) # else: cv2.imwrite( name ,img_cropped)
def get_MSER(image_path, ): image = cv2.imread(image_path) rgb_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) negtive_map = np.zeros(image.shape[:2]) mser = cv2.MSER_create(_delta=5, _min_area=10, _max_variation=0.8) regions, bboxes = mser.detectRegions(rgb_img) # 绘制文本区域(不规则轮廓) hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions] keep = [] for hull in hulls: x, y, w, h = cv2.boundingRect(hull) keep.append([x, y, x + w, y + h]) # 使用非极大值抑制获取不重复的矩形框 pick = non_max_suppression_fast(np.array(keep), overlapThresh=0.4) # loop over the picked bounding boxes and draw them for (startX, startY, endX, endY) in pick: cv2.fillPoly( negtive_map, np.array([[[startX, startY], [endX, startY], [endX, endY], [startX, endY]]]), (1)) negtive_map = Image.fromarray(negtive_map) return negtive_map
def gn(): global video_camera video_camera = camera.VideoStreaming() mser = cv2.MSER_create() out = '' num = 0 while True: ret, frame = video_camera.get_frame() #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) vis = frame.copy() #regions, _ = mser.detectRegions(gray) #hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions] #cv2.polylines(vis, hulls, 1, (0, 255, 0)) width = vis.shape[0] height = vis.shape[1] #start = (0, int(width/2)) #end = (height, int(width/2)) #start_width = (int(height/2),0) #end_width = (int(height/2), width) #cv2.line(vis, start, end, (0, 148, 82) ,2) #cv2.line(vis, start_width, end_width, (0, 148, 82), 2) cv2.circle(vis, (695, 1080), 1, (0, 0, 255), 10) ret, jpg = cv2.imencode('.JPEG', vis) jpg_bytes = jpg.tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + jpg_bytes + b'\r\n\r\n') cam.cap_res.release() video.release()
def mask_blobs(gray): mask = np.zeros(gray.shape[:2], dtype='uint8') # detect blobs #mser = cv2.MSER_create(_delta=4, _min_area=65, _max_area=14400, _max_variation=1.0) #blobs, _ = mser.detectRegions(gray) mser = cv2.MSER_create(_delta=4, _min_area=65, _max_area=14400, _max_variation=1.0) blobs, _ = mser.detectRegions(gray) # find circular blobs for blob in blobs: hull = cv2.convexHull(blob.reshape(-1, 1, 2)) epsilon = 0.01 * cv2.arcLength(hull, True) poly = cv2.approxPolyDP(hull, epsilon, True) # select polygons with more than 9 vertices if len(poly) > 9: cv2.polylines(mask, [blob], 1, 255, 1) return mask
def __getitem__(self, idx): args = self.args img_in_LR = cv2.imread(self.file_in_list[idx]) img_tar_LR = cv2.imread(self.file_tar_list[idx//15]) if args.need_patch: img_in_LR, img_tar_LR = getPatch(img_in_LR, img_tar_LR, self.args) img_in_LR, img_tar_LR = augment(img_in_LR, img_tar_LR) # img_in_LR = img_in[::2, ::2, :] # img_tar_LR = img_tar[::2, ::2, :] ################################################# mser = cv2.MSER_create() h, w, c = img_in_LR.shape keypoint_size = (h//16, w//16) keypoints_in_pos = mser.detect(img_in_LR) keypoints_in_pos = np.uint16(np.asarray([p.pt for p in keypoints_in_pos])) keypoints_in = get_keypoints(keypoints_in_pos, img_in_LR, keypoint_size) # keypoints_tar_pos = mser.detect(img_tar_LR) # keypoints_tar_pos = np.uint16(np.asarray([p.pt for p in keypoints_tar_pos])) # keypoints_tar = get_keypoints(keypoints_tar_pos, img_tar, 32) ################################################# img_tar_LR, img_tar_LR, img_in_LR = RGB_np2tensor(img_tar_LR, img_tar_LR, img_in_LR, args.nchannel) keypoints_in = RGB_np2tensor_kpt(keypoints_in, 128) return img_in_LR, keypoints_in, img_tar_LR, img_tar_LR
def further_process(thresh_img): r_thresh = cv2.GaussianBlur(thresh_img, (5, 5), 0) # Gaussian filtering kernel_1 = np.ones((3, 3), np.uint8) # Define a 3*3 array of all ones kernel_2 = np.ones((5, 5), np.uint8) # Corrosion, expansion and opening operations in morphology erosion = cv2.erode(r_thresh, kernel_1, iterations=1) dilation = cv2.dilate(erosion, kernel_2, iterations=1) opening = cv2.morphologyEx(dilation, cv2.MORPH_OPEN, kernel_2) # Do MSER mser_red = cv2.MSER_create(8, 100, 10000) regions, _ = mser_red.detectRegions(np.uint8(opening)) hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions] blank_im = np.zeros_like(r_thresh) # create a mask cv2.fillPoly(np.uint8(blank_im), hulls, (255, 255, 255)) # fill a blank image with the detected hulls # close operations in morphology kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 6)) closed = cv2.morphologyEx(blank_im, cv2.MORPH_CLOSE, kernel) # find contours which are traffic signs possibly cnts = cv2.findContours(closed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) return cnts
def mser_func(image): mser = cv2.MSER_create() source = image.copy() regions, _ = mser.detectRegions(source) hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions] # cv2.polylines(source, hulls, 1, (0, 255, 0)) return hulls
def test_mser_params(): ''' 该函数测试的结论: 1. `cv2.MSER_create(5, 50, 100000, 0.25)`, 其中的`50`是能检测的最小的区域,用来检测标点等, 其中的100000是能检测到的最大的区域,这个参数限制了大字体; 其它参数在我们的场景中不是很重要; ''' inner_save_names = init_detect_inner_folder() resume_path = '../sub_func_test/ocr_image/0001.jpg' img = cv2.imread(resume_path) cv2.imwrite(inner_save_names['raw'], img) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # ret, gray = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY) cv2.imwrite(inner_save_names['gray'], gray) minareas = [20, 50, 100, 200, 300, 400, 500] for minarea in minareas: mser = cv2.MSER_create(5, minarea, 10000, 0.5) regions, boxes = mser.detectRegions(gray) gray_output = img.copy() for box in boxes: x, y, w, h = box cv2.rectangle(gray_output, (x, y), (x + w, y + h), (0, 0, 255), 2) cv2.imwrite( inner_save_names['mser_box'].split('.jpg')[0] + '_minarea{}.jpg'.format(minarea), gray_output)
def mser_mask(input_image): w = input_image.shape[1] h = input_image.shape[0] area = w * h mask_regions = np.zeros((h, w), np.uint8) # int delta = 13; #int img_area = img.cols*img.rows; #Ptr<MSER> cv_mser = MSER::create(delta,(int)(0.00002*img_area),(int)(0.11*img_area),55,0.); #mser = cv2.MSER_create() #delta, minArea, maxArea, maxVariation, minDiversity, maxEvolution, areaThresh, minMargin, edgeBlurSize #mser = cv2.MSER_create(8, 5, 14400, 0.25, 0.01, 100, 1.01, 0.03, 5) # good1 #mser = cv2.MSER_create(13, 2, 14400, 55, 0) #mser = cv2.MSER_create(13, int(0.00002*area),int(0.11*area), 55, 0) mser = cv2.MSER_create(13, 10, 14400, 55, 0) # good2 #1 #mser = cv2.MSER_create(5,5, 14400, .25, .4, 100, 1.01, 0.003, 5) #mser = cv2.MSER_create(5, 5, 14400, 0.25, 0.2, 200, 1.01, 0.003, 5) gray = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY) regions = mser.detectRegions(gray, None) for p in regions: for pts in p: x, y = pts mask_regions[y, x] = 255 return mask_regions
def mser(self): #Create MSER object self.mser = cv2.MSER_create() #Your image path i-e receipt path self.img = cv2.imread('hh3.png') #Convert to grayscale self.gray = cv2.cvtColor(self.img, cv2.COLOR_BGR2GRAY) self.vis = self.img.copy() #detect regions in gray scale image self.regions, _ = self.mser.detectRegions(self.gray) self.hulls = [ cv2.convexHull(p.reshape(-1, 1, 2)) for p in self.regions ] cv2.polylines(self.vis, self.hulls, 1, (0, 255, 0)) cv2.imshow('img', self.vis) cv2.waitKey(0) self.mask = np.zeros((self.img.shape[0], self.img.shape[1], 1), dtype=np.uint8) for contour in self.hulls: cv2.drawContours(self.mask, [contour], -1, (255, 255, 255), -1) #this is used to find only text regions, remaining are ignored text_only = cv2.bitwise_and(self.img, self.img, mask=self.mask) cv2.imshow("text only", text_only) cv2.waitKey(0)
def detect_text(path): img = cv2.imread(path) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) vis = img.copy() mser = cv2.MSER_create() regions, _ = mser.detectRegions(gray) hulls = [] for p in regions: for i in range(len(p)): hulls.append(cv2.convexHull(p[i].reshape(-1,1,2))) cv2.polylines(vis,hulls,1, (0,255,0)) #commented out for batch programming #cv2.imshow("image", vis) #cv2.waitKey(0) mask = np.zeros((img.shape[0], img.shape[1], 1), dtype=np.uint8) for contour in hulls: cv2.drawContours(mask, [contour], -1,(255,255,255),-1) text_regions = cv2.bitwise_and(img, img, mask = mask) #commented out for batch programing #cv2.imshow("text", text_regions) return text_regions
def MSER_blobs(img, params, mask=None, display=None): # Unpack MSER parameters and create MSER object with them. delta, minArea, maxArea, maxVariation, minDiversity, maxEvolution, areaThreshold, minMargin, edgeBlurSize = params mser = cv2.MSER_create(delta, minArea, maxArea, maxVariation, maxEvolution, areaThreshold, minMargin, edgeBlurSize) # Use MSER to detect regions within the given image. if img.dtype == 'uint16': img = np.divide(img, 256) img = img.astype('uint8') regions, _ = mser.detectRegions(img) # Extract the hulls corresponding to the contours found by the MSER algorithm. hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions] # Optionally draw the hulls on a display image. if display is not None: try: display = cv2.cvtColor(display, cv2.COLOR_GRAY2BGR) except: print("Display image correctly formatted.") print(len(hulls)) cv2.polylines(display, hulls, 1, (0, 65000, 0)) #cv2.imshow("Display Image with MSER hulls", display) #cv2.waitKey(0) #cv2.destroyAllWindows() #cv2.imwrite("MSER Hulls.png", display) hulls = np.asarray(hulls) if display is not None: return hulls, display return hulls
def mser_ecoli(): if is_old_cv: mser = cv.MSER() else: mser = cv.MSER_create( # cv.MSER_create() _delta=5, _min_area=720, _max_area=9000, _max_variation=15.0, _min_diversity=10.0, _max_evolution=10, _area_threshold=12.0, _min_margin=2.9, _edge_blur_size=10) # img_path = 'C:\\dev\\courses\\2.131 - Advanced Instrumentation\\E.coli.tif' img_path = 'C:\\dev\\Holographic-Images\\Combined-Half-and Half-capture-2018-04-30-20h-52m-09s.png' pil_img = Image.open(img_path) # for i in range(149): # pil_img.seek(i) img = np.array(pil_img) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) vis = img.copy() if is_old_cv: regions = mser.detect(gray, None) else: regions, q = mser.detectRegions(gray) #polylines hulls = [cv.convexHull(p.reshape(-1, 1, 2)) for p in regions] # cv.polylines(vis, hulls, 1, (0, 255, 0)) #boundingboxes mask = np.zeros((img.shape[0], img.shape[1], 1), dtype=np.uint8) mask = cv2.dilate(mask, np.ones((150, 150), np.uint8)) for contour in hulls: cv2.drawContours(mask, [contour], -1, (255, 255, 255), -1) bboxes = [] for i, contour in enumerate(hulls): x, y, w, h = cv2.boundingRect(contour) bboxes.append(cv2.boundingRect(contour)) #cv2.rectangle(vis, (x, y), (x + w, y + h), (0, 255, 0), 2) for i in range(len(bboxes)): j = i + 1 while j < len(bboxes): if intersection(bboxes[i], bboxes[j]) > 0: break else: j = j + 1 if j == len(bboxes): x, y, w, h = bboxes[i] cv2.rectangle(vis, (x, y), (x + w, y + h), (0, 0, 255), 2) return img, vis, bboxes cv2.imshow('img', vis) cv2.waitKey()
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) debug.img_show(img) colors, car_imgs = img_math.img_color(colors_img) for i, color in enumerate(colors): if color != "no": print(color) debug.img_show(car_imgs[i])
def detectorcall(): # AgastFeatureDetector #detector = cv2.AgastFeatureDetector_create() # FAST #detector = cv2.FastFeatureDetector_create() # MSER detector = cv2.MSER_create() # AKAZE #detector = cv2.AKAZE_create() # BRISK #detector = cv2.BRISK_create() # KAZE #detector = cv2.KAZE_create() # ORB (Oriented FAST and Rotated BRIEF) #detector = cv2.ORB_create() # SimpleBlobDetector #detector = cv2.SimpleBlobDetector_create() # SIFT #detector = cv2.xfeatures2d.SIFT_create() return detector
def generate_blobs(page, local_directory): image = cv2.imread(page) # Create MSER object mser = cv2.MSER_create() # creates a gray image object gray = generate_gray_image(image) # regions are the contours detected on image (i.e, the circle inside the letter o); bboxes are the bouding boxes that can be formed from coordinates of regions regions, bboxes = mser.detectRegions(gray) # drawing contours and bounding boxes of an image to easily see them. Proof of Concept and helps visualize what we are working with. draw_contours_bboxes(gray, regions, bboxes, image) # this function eliminates the bounding boxes that we don't need (i.e, formed by lines or large shapes) since they can alter our clustering processes bboxes_list = filter_bboxes(bboxes, page, image) # returns bboxes. Format of bbox: [x, y, x + width, y + height] # or [x1, y1, x2, y2] if (x1, y1) and (x2, y2) top-left and right bottom extremities print(len(bboxes_list)) print(len(bboxes_list) is not None) if len(bboxes_list) is not 0: bboxes_list = sorted(bboxes_list, key=lambda k: (k[1], k[0])) # Sort by x1 then y1 coordinate (x and y of the left-top coordinate of box) combined_bboxes = create_clusters(bboxes_list) # creates a list of lists of bounding boxes that are close to each other rectangles = group_clusters(combined_bboxes, image) # groups those bounding boxes by picking the extremities clusters = merge_clusters(rectangles, local_directory, page, image) # merges combined boxes that overlap
def getBoundingBox(): #convert to gray scale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) mser = cv2.MSER_create() coordinates, bboxes = mser.detectRegions(gray) #format x , y, w, h points = [] for bbox in bboxes: x, y, w, h = bbox if [x, y, x + w, y + h] not in points: points.append([x, y, x + w, y + h]) #check inside flag = [True] * len(points) for p in points: x1, y1, x2, y2 = p for i in range(len(points)): if p != points[i] and flag[i]: a1, b1, a2, b2 = points[i] if a1 >= x1 and a2 <= x2 and b1 >= y1 and b2 <= y2: flag[i] = False total_bboxes = [] for i in range(len(flag)): if flag[i]: total_bboxes.append(points[i]) total_bboxes.sort() print("Total no. of characters to be found = " + str(len(total_bboxes))) return total_bboxes
def main(): try: video_src = sys.argv[1] except: video_src = 0 cam = video.create_capture(video_src) mser = cv.MSER_create() while True: ret, img = cam.read() if ret == 0: break gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) vis = img.copy() regions, _ = mser.detectRegions(gray) hulls = [cv.convexHull(p.reshape(-1, 1, 2)) for p in regions] cv.polylines(vis, hulls, 1, (0, 255, 0)) cv.imshow('img', vis) if cv.waitKey(5) == 27: break print('Done')
def ocr_single_image(img_path): # 读取图片 imagePath = img_path img = cv2.imread(imagePath) # 灰度化 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) vis = img.copy() orig = img.copy() # 调用 MSER 算法 mser = cv2.MSER_create() regions, _ = mser.detectRegions(gray) # 获取文本区域 hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions] # 绘制文本区域 cv2.polylines(img, hulls, 1, (0, 255, 0)) # cv2.imshow('img', img) img_pil = cv2pil(img) # plt.imshow(img) # plt.show() # 将不规则检测框处理成矩形框 keep = [] for c in hulls: x, y, w, h = cv2.boundingRect(c) keep.append([x, y, x + w, y + h]) cv2.rectangle(vis, (x, y), (x + w, y + h), (255, 255, 0), 1) # cv2.imshow("hulls", vis) vis_pil = cv2pil(vis) # plt.imshow(vis_pil) # plt.show() return img_pil,vis_pil