def drawRectangle(self, image): [height, width] = image.shape cnts1 = cv.findContours(image, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) cnts1 = cnts1[1] if imutils.is_cv3() else cnts1[0] res = [] blank = np.zeros([int(height), int(width), 3], np.uint8) for c in cnts1: x, y, w, h = cv.boundingRect(c) # print(x, y, w, h) if w > self.rectEdgeThresh * width and h > self.rectEdgeThresh * height: x1 = x y1 = y x2 = x + w y2 = y + h cv.rectangle(blank, (x1, y1), (x2, y2), (0, 0, 255), cv.FILLED) blank2 = cv.cvtColor(blank, cv.COLOR_BGR2GRAY) cnts2 = cv.findContours(blank2, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) cnts2 = cnts2[1] if imutils.is_cv3() else cnts2[0] for c in cnts2: x, y, w, h = cv.boundingRect(c) if w > 0.08 * width and h > 0.08 * height: print(x, y, x + w, y + h) item = [str(x), str(y), str(x + w), str(y + h)] res.append(item) return res
def get_motions(f, fMask, thickness=1, color=(170, 170, 170)): ''' Iterates over the contours in a mask and draws a bounding box around the ones that encompas an area greater than a threshold. This will return an image of just the draw bock (black bg), and also an array of the box points. ''' rects_mot = [] f_rects = np.zeros(f.shape, np.uint8) # get contours if imutils.is_cv3(): _, cnts, hierarchy = cv2.findContours( fMask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) elif imutils.is_cv2(): cnts, hierarchy = cv2.findContours( fMask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # loop over the contours for c in cnts: # if the contour is too small, ignore it if cv2.contourArea(c) < contourThresh: continue if imutils.is_cv3(): box = cv2.boxPoints(cv2.minAreaRect(c)) elif imutils.is_cv2(): box = cv2.cv.BoxPoints(cv2.minAreaRect(c)) box = np.int0(box) cv2.drawContours(f_rects, [box], 0, color, thickness) rects_mot.append(cv2.boundingRect(c)) return f_rects, rects_mot
def __init__(self): # determine if we are using OpenCV v3.X and initialize the # cached homography matrix if imutils.is_cv3(): print("yes cv3") self.isv3 = imutils.is_cv3() self.cachedH = None
def drawRectangle(self, image, ratio): [height, width] = image.shape cnts = cv.findContours(image, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) cnts = cnts[1] if imutils.is_cv3() else cnts[0] thickness = 3 - len(cnts) // 100 res = [] # The first iteration blank = np.zeros([int(height), int(width), 3], np.uint8) for c in cnts: x, y, w, h = cv.boundingRect(c) # print(x, y, w, h) # if w > self.rectEdgeThresh * width and h > self.rectEdgeThresh * height: x1 = x y1 = y x2 = x + w y2 = y + h cv.rectangle(blank, (x1, y1), (x2, y2), color=(0, 0, 255), thickness=thickness) # The second iteration blank2 = cv.cvtColor(blank, cv.COLOR_BGR2GRAY) blank = np.zeros([int(height), int(width), 3], np.uint8) cnts = cv.findContours(blank2, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) cnts = cnts[1] if imutils.is_cv3() else cnts[0] # i = 0 for c in cnts: x, y, w, h = cv.boundingRect(c) # print(x, y, w, h) if w > self.rectEdgeThresh * width and h > self.rectEdgeThresh * height: x1 = x y1 = y x2 = x + w y2 = y + h cv.rectangle(blank, (x1, y1), (x2, y2), (0, 0, 255), 0) # The third iteration blank2 = cv.cvtColor(blank, cv.COLOR_BGR2GRAY) cnts = cv.findContours(blank2, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) cnts = cnts[1] if imutils.is_cv3() else cnts[0] for c in cnts: x, y, w, h = cv.boundingRect(c) if w > 0.06 * width and h > 0.06 * height: x1 = int(x * ratio[1]) y1 = int(y * ratio[0]) x2 = int((x + w) * ratio[1]) y2 = int((y + h) * ratio[0]) print(x1, y1, x2, y2) item = [str(x1), str(y1), str(x2), str(y2)] res.append(item) return res
def detectAndDescribe(image): # convert the image to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # check to see if we are using OpenCV 3.X if imutils.is_cv3(or_better=True): # detect and extract features from the image descriptor = cv2.xfeatures2d.SURF_create() (kps, features) = descriptor.detectAndCompute(image.astype(np.uint8), None) # orb feature is way faster # orb = cv2.ORB_create() # kp = orb.detect(gray, None) # (kps, features) = orb.compute(gray, kp) # otherwise, we are using OpenCV 2.4.X else: # detect keypoints in the image detector = cv2.FeatureDetector_create("SIFT") kps = detector.detect(gray) # extract features from the image extractor = cv2.DescriptorExtractor_create("SIFT") (kps, features) = extractor.compute(gray, kps) # convert the keypoints from KeyPoint objects to NumPy # arrays kps = np.float32([kp.pt for kp in kps]) # return a tuple of keypoints and features return (kps, features)
def work(self): imagePaths = sorted(list(paths.list_images('images'))) ip = [] # 用于存放按次序的图片,因为拼接对次序敏感 tmp = imagePaths[0] for i in range(len(imagePaths)): ip.append(tmp[:12] + str(i) + tmp[13:]) images = [] for i, imagePath in enumerate(ip[:]): if i % 1 == 0: # 2为隔一张,不需要隔则设置为1即可 print(imagePath) image = cv2.imread(imagePath) image = cv2.rotate(image, 2) # 横向旋转,因为拼接对方向敏感 images.append(image) import time a = time.time() print('stitching images...') stitcher = cv2.createStitcher() if imutils.is_cv3( ) else cv2.Stitcher_create() (status, stitched) = stitcher.stitch(images) print(time.time() - a) if status == 0: cv2.imwrite('res.png', stitched) return stitched else: print("got an error ({})".format(status))
def GenerateCharacterrs(): print("Karakterek kigyujtese a Captcha-bol") captcha_image_files = glob.glob(os.path.join("captcha_images", "*")) counts = {} for (i, captcha_image_file) in enumerate(captcha_image_files): # ciklus ami vegig megy az osszes training image-n filename = os.path.basename(captcha_image_file) captcha_correct_text = os.path.splitext(filename)[0] # mekeresei az adott file nevet image = cv2.imread(captcha_image_file) # beolvassa a képet, majd atalakitja grayscale-re gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.copyMakeBorder(gray, 8, 8, 8, 8, cv2.BORDER_REPLICATE) thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1] #threshold, azert hogy megtalalhassa a karaktereket mint contour contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # contour-ok megkeresése contours = contours[1] if imutils.is_cv3() else contours[0] letter_image_regions = [] # karakterek helyei, ehhez addodnak hozza a karakterek regioi for contour in contours: (x, y, w, h) = cv2.boundingRect(contour) # karakterek koruli bounding box koordinatak letter_image_regions.append((x, y, w, h)) letter_image_regions = sorted(letter_image_regions, key=lambda x: x[0]) # karakter regiok rendezese for letter_bounding_box, letter_text in zip(letter_image_regions, captcha_correct_text): # ciklus ami vegigmegy a boxokon x, y, w, h = letter_bounding_box letter_image = gray[y - 2:y + h + 2, x - 2:x + w + 2] # egy uj kep lesz a karakterekbol a koordinatak alapjan, majd ezeket lementi(minden karakter kap egy folder-t es azon belül szamozva kerulnek az uj kepek a karakterekrol) save_path = os.path.join("generated_contours", letter_text) if not os.path.exists(save_path): os.makedirs(save_path) count = counts.get(letter_text, 1) # szamozas p = os.path.join(save_path, "{}.png".format(str(count).zfill(6))) cv2.imwrite(p, letter_image) counts[letter_text] = count + 1 print("Karakterek kigyujtese vegetert")
def recoverPoseFromE_cv3(E, pts1, pts2, K): assert len(pts1) == len(pts2) """ RE1, RE2, tE = cv2.decomposeEssentialMat( E) # there will be two answers for R, and four answers in total: R1 t, R1 -t, R2 t, R2 -t print("R t from decomposeEssentialMat, note that there are four potential answers") DEBUG_Rt(RE1, tE, "RE1, tE") DEBUG_Rt(RE2, tE, "RE2, tE") """ assert imutils.is_cv3() == True _, R, t, mask_rc = cv2.recoverPose( E, pts1, pts2, K) # this can have the determiate results, so we choose it # for m in mask_rc: # print(m.ravel()) # We select only inlier points pts1_rc = pts1[mask_rc.ravel() != 0] #pts2_rc = pts2[mask_rc.ravel() != 0] print("In recoverPoseFromE_cv3, points:{} -> inliner:{}".format( len(pts1), len(pts1_rc))) return R, t
def upload_file(): if request.method == 'POST': # check if the post request has the files part if 'files[]' not in request.files: flash('No file part') return redirect(request.url) files = request.files.getlist('files[]') for file in files: if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) flash('File(s) successfully uploaded') print("[INFO] loading images...") images = [] for imagePath in files: print(type(imagePath)) print(imagePath) for imagePath in files: image = cv2.imread(imagePath) cv2.imshow("Image", image) # image=cv2.resize(image, (64,64)) images.append(image) print("[INFO] stitching images...") stitcher = cv2.createStitcher() if imutils.is_cv3( ) else cv2.Stitcher_create() (status, stitched) = stitcher.stitch(images) if status == 0: # cv2.imwrite(args["output"], stitched) cv2.imshow("Stitched", stitched) cv2.waitKey(0) else: print("[INFO] image stitching failed ({})".format(status)) return redirect('/')
def img_stitch(): # Used for image stitching: grab the paths to the input images and initialize our images list imgPaths = sorted(list(paths.list_images(this_path + "imgToStitch"))) imgs = [] # loop over the image paths, load each one, and add them to our # images to stitch list for imgPath in imgPaths: img = cv2.imread(imgPath) imgs.append(img) # initialize OpenCV's image stitcher object and then perform the image # stitching stitcher = cv2.createStitcher() if imutils.is_cv3() else cv2.Stitcher_create() (status, stitched) = stitcher.stitch(imgs) # if the status is '0', then OpenCV successfully performed image # stitching if status == 0: # create a 10 pixel border surrounding the stitched image stitched = cv2.copyMakeBorder(stitched, 10, 10, 10, 10, cv2.BORDER_CONSTANT, (0, 0, 0)) # get number of images already in the ./stitchUnlabel directory imgCount = len([name for name in os.listdir(this_path + 'stitchUnlabel') if os.path.isfile(os.path.join(this_path + 'stitchUnlabel', name))]) - 1 # write the output stitched image to disk (adding 1 to imgCount bc first image is gsv1 not gsv0) cv2.imwrite(this_path + 'stitchUnlabel' + '/gsv'+ str(imgCount+1) + '.jpg', stitched) # otherwise the stitching failed, likely due to not enough keypoints) # being detected else: print("[INFO] image stitching failed ({})".format(status))
def run_contours(fileName): # load image, change color spaces, and smoothing img = cv2.imread(fileName) img_HSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) img_HSV = cv2.GaussianBlur(img_HSV, (9, 9), 3) # detect objects img_H, img_S, img_V = cv2.split(img_HSV) _thre, img_flowers = cv2.threshold(img_H, 140, 255, cv2.THRESH_BINARY) # if you want to check the mask file uncomment here ---> cv2.imwrite('/mnt/c/linuxmirror/tulips_mask.jpg', img_flowers) # find objects cv3 returns 3 parameters rather than 2 in cv2 and cv4 # if imutils.is_cv3(): labels, contours, hierarchy = cv2.findContours(img_flowers, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) else: contours, hierarchy = cv2.findContours(img_flowers, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) for i in range(0, len(contours)): if len(contours[i]) > 0: # remove small objects if cv2.contourArea(contours[i]) < 500: continue cv2.polylines(img, contours[i], True, (255, 55, 255), 5) # save fileElements = split('.', fileName) newWriteFileName = fileElements[0] + "_boundingbox." + fileElements[1] cv2.imwrite(newWriteFileName, img)
def detectAndDescribe(self, image): # convert the image to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) print("version:", imutils.is_cv4()) # check to see if we are using OpenCV 3.X if (imutils.is_cv3() or imutils.is_cv4()): # detect and extract features from the image descriptor = cv2.xfeatures2d.SIFT_create() #descriptor = cv2.FeatureDetector_create("SIFT") (kps, features) = descriptor.detectAndCompute(image, None) # otherwise, we are using OpenCV 2.4.X #else: # detect keypoints in the image #detector = cv2.xfeatures2d.SIFT_create() # detector = cv2.FeatureDetector_create("SIFT") # kps = cv2.xfeatures2d.SIFT_create().detect(gray) # extract features from the image #extractor = cv2.xfeatures2d.SIFT_create() # extractor = cv2.DescriptorExtractor_create("SIFT") # (kps, features) = extractor.compute(gray, kps) # convert the keypoints from KeyPoint objects to NumPy # arrays kps = np.float32([kp.pt for kp in kps]) # return a tuple of keypoints and features return (kps, features)
def preProcess(image): ratio = image.shape[0] / 500.0 image = imutils.resize(image, height=500) grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gaussImage = cv2.GaussianBlur(grayImage, (5, 5), 0) edgedImage = cv2.dilate(gaussImage, numpy.ones((3, 3), numpy.uint8), iterations=2) edgedImage = cv2.erode(edgedImage, numpy.ones((2, 2), numpy.uint8), iterations=3) edgedImage = cv2.Canny(edgedImage, 70, 200) cnts = cv2.findContours(edgedImage.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[1] if imutils.is_cv3() else cnts[0] cnts = sorted(cnts, key=cv2.contourArea, reverse=True) screenCnt = {} for c in cnts: peri = cv2.arcLength(c, True) # Calculating contour circumference approx = cv2.approxPolyDP(c, 0.02 * peri, True) if len(approx) == 4: if (abs(round( (approx[0][0][0] - approx[1][0][0]) / image.shape[0])) == 1 or abs( round((approx[2][0][1] - approx[0][0][1]) / image.shape[1])) == 1): screenCnt = approx break return screenCnt, ratio
def update(self, hsvFrame): if self.x and self.y and self.radius: self.lastX, self.lastY, self.lastRadius = self.x, self.y, self.radius self.x, self.y, self.radius = None, None, None # Determine which pixels fall within the color boundaries and then blur the binary image self.colorMask = self.createColorTrackingMask(hsvFrame, self.trackingColor) # cv2 or cv4 ! if not imutils.is_cv3(): self.contours, _ = cv2.findContours(self.colorMask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) else: #if imutils.is_cv3(): _, self.contours, _ = cv2.findContours(self.colorMask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Check to see if any contours ere found if self.contours is not None and len(self.contours) > 0: contour = sorted(self.contours, key=cv2.contourArea, reverse=True)[0] # Get the radius of the enclosing circle around the found contour ((self.x, self.y), self.radius) = cv2.minEnclosingCircle(contour) else: # only delete the last position if the tracker has been dead fora while # this accounts for flicker self.skipLastCount += 1 if self.skipLastCount > 5: self.skipLastCount = 0 self.lastX, self.lastY, self.lastRadius = None, None, None
def stitch(images_path, output_path): print("[INFO] loading images...") imagePaths = sorted(list(paths.list_images(images_path))) images = [] # loop over the image paths, load each one, and add them to our # images to stitch list for imagePath in imagePaths: image = cv2.imread(imagePath) images.append(image) # initialize OpenCV's image stitcher object and then perform the image # stitching print("[INFO] stitching images...") stitcher = cv2.createStitcher() if imutils.is_cv3( ) else cv2.Stitcher_create() (status, stitched) = stitcher.stitch(images) # if the status is '0', then OpenCV successfully performed image # stitching if status == 0: # write the output stitched image to disk cv2.imwrite(output_path, stitched) return True # otherwise the stitching failed, likely due to not enough keypoints) # being detected else: print("[INFO] image stitching failed ({})".format(status)) return False
def solve_captcha(img_array): image = cv2.imdecode(img_array, cv2.IMREAD_ANYCOLOR) image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours = contours[1] if imutils.is_cv3() else contours[0] char_image_regions = [] for contour in contours: (x, y, w, h) = cv2.boundingRect(contour) if w / h > 1: # 1 - best value so far (hyperparam) half_width = int(w / 2) char_image_regions.append((x, y, half_width, h)) char_image_regions.append((x + half_width, y, half_width, h)) else: char_image_regions.append((x, y, w, h)) char_image_regions = sorted(char_image_regions, key=lambda x: x[0]) predictions = [] for char_bounding_box in char_image_regions: x, y, w, h = char_bounding_box char_image = image[y:y + h, x:x + w] char_image = resize_to_fit(char_image, 20, 20) char_image = np.expand_dims(char_image, axis=2) char_image = np.expand_dims(char_image, axis=0) prediction = model.predict(char_image) char = lb.inverse_transform(prediction)[0] predictions.append(char) return "".join(predictions)
def cutLetters(self,image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (7, 5), 0) thresh = cv2.adaptiveThreshold(blurred,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C , cv2.THRESH_BINARY_INV,45,10) #45 edges = cv2.Canny(thresh,50,150,apertureSize = 5) if imutils.is_cv2(): (cnts, _) = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) # check to see if we are using OpenCV 3 elif imutils.is_cv3(): (_, cnts, _) = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) image2 = image.copy() cv2.namedWindow("Letters", cv2.WINDOW_NORMAL); cv2.imshow("Letters", edges) cv2.waitKey(0) delta = 8 Deltas = [[[delta,delta]],[[delta,-delta]],[[-delta,-delta]],[[-delta,+delta]]] for c in cnts: peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.1 * peri, True) if len(approx) == 4: idx = self.sortPoints(approx[:,0]) approx = approx[idx] approxFinal = approx+Deltas cv2.drawContours(image2, [c], -1, (0, 0, 255), 2) cv2.drawContours(image2, approxFinal, -1, (0, 255, 0), 3) cv2.imshow("Letters", image2) cv2.waitKey(0) return approxFinal
def get_features(imageDirectory): image = cv2.imread(imageDirectory) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # check to see if we are using OpenCV 3.X if imutils.is_cv3(): # detect and extract features from the image descriptor = cv2.xfeatures2d.SIFT_create() (kps, features) = descriptor.detectAndCompute(gray, None) # otherwise, we are using OpenCV 2.4.X else: # detect keypoints in the image detector = cv2.FeatureDetector_create("SIFT") kps = detector.detect(gray) # extract features from the image extractor = cv2.DescriptorExtractor_create("SIFT") (kps, features) = extractor.compute(gray, kps) # convert the keypoints from KeyPoint objects to NumPy # arrays kps = np.float32([kp.pt for kp in kps]) # return a tuple of keypoints and features return (kps, features)
def stich_images(img_dir="./shelf"): """ stitch images in a given directory """ print("[INFO] loading images...") imagePaths = sorted(list(paths.list_images(img_dir))) images = [] # loop over the image paths, load each one, and add them to our # images to stitch list for imagePath in imagePaths: image = cv2.imread(imagePath) images.append(image) print("[INFO] stitching images...") stitcher = cv2.createStitcher() if imutils.is_cv3( ) else cv2.Stitcher_create() (status, stitched) = stitcher.stitch(images) # if the status is 0, then OpenCV successfully performed image stitching if status == 0: # write the output stitched image to disk cv2.imwrite("./pano.jpg", stitched) # display the output stitched image to our screen # cv2.imshow("Stitched", stitched) # cv2.waitKey(0) # otherwise the stitching failed, likely due to not enough keypoints) being detected else: print("[INFO] image stitching failed ({})".format(status))
def __init__(self): # Initialize argume # Initialize the saved homography matrix self.isv3 = imutils.is_cv3() self.Homography = None
def stitch(image_path, output_path, output_path1, crop): print("[INFO] loading images...") imagePaths = sorted(list(paths.list_images(image_path))) images = [] # loop over the image paths, load each one, and add them to our # images to stitch list for imagePath in imagePaths: image = cv2.imread(imagePath) images.append(image) # initialize OpenCV's image sticher object and then perform the image # stitching print("[INFO] stitching images...") stitcher = cv2.createStitcher() if imutils.is_cv3( ) else cv2.Stitcher_create() (status, stitched) = stitcher.stitch(images) if status == 0: if crop: print("cropping") cropped = crop(stitched) cv2.imwrite(output_path, stitched) cv2.imwrite(output_path1, cropped) else: cv2.imwrite(output_path, stitched) print('done') else: print('not enough features')
def stitch_images(self, directory, output_path): # grab the paths to the input images and initialize our images list print("[INFO] loading images...") imagePaths = sorted(list(paths.list_images(directory))) images = [] # loop over the image paths, load each one, and add them to our # images to stich list for imagePath in imagePaths: image = cv2.imread(imagePath) images.append(image) # initialize OpenCV's image sticher object and then perform the image # stitching print("[INFO] stitching images...") stitcher = cv2.createStitcher() if imutils.is_cv3( ) else cv2.Stitcher_create() (status, stitched) = stitcher.stitch(images) # if the status is '0', then OpenCV successfully performed image # stitching if status == 0: # write the output stitched image to disk cv2.imwrite(output_path, stitched) # display the output stitched image to our screen cv2.imshow("Stitched", stitched) cv2.waitKey(0) # otherwise the stitching failed, likely due to not enough keypoints) # being detected else: print("[INFO] image stitching failed ({})".format(status))
def get_outer_box_contour(original_image): original_image_copy = original_image.copy() # show_image(original_image_copy , title='original_image', delayed_show=True) gray = cv2.cvtColor(original_image_copy, cv2.COLOR_BGR2GRAY) blurred = cv2.medianBlur(gray, 7) blurred = cv2.blur(blurred, ksize=(7, 7)) thresh = cv2.adaptiveThreshold( blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) # show_image(thresh, title='thresh', delayed_show=True) kernel = np.ones((13, 13), np.uint8) eroded = cv2.erode(thresh, kernel, iterations=1) dilated = cv2.dilate(eroded, kernel, iterations=1) # show_image(dilated, title='after erosion-dilation', delayed_show=True) edged_image = cv2.Canny( dilated, threshold1=180, threshold2=230, L2gradient=True, apertureSize=3) # show_image(edged_image, title='edged_image', delayed_show=True) cnts = cv2.findContours(edged_image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) if imutils.is_cv3(): cnts = cnts[1] elif imutils.is_cv4(): cnts = cnts[0] else: raise ImportError( 'must have opencv version 3 or 4, yours is {}'.format( cv2.__version__)) contour_image = edged_image.copy() cv2.drawContours(contour_image, cnts, -1, (255, 0, 0), 3) # show_image(contour_image, title='contoured_image', delayed_show=False) # validate image_perim = 2 * sum(edged_image.shape) docCnt = None assert len(cnts) > 0, 'no contours found when looking for outer box' for c in sorted(cnts, key=cv2.contourArea, reverse=True): perim = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.05 * perim, True) if len(approx) == 4: docCnt = approx break min_acceptable_perim = image_perim * 0.66 if (type(docCnt) != np.ndarray or perim < min_acceptable_perim) and debug_mode(): temp_image = cv2.cvtColor(edged_image, cv2.COLOR_GRAY2RGB) cv2.drawContours(temp_image, [docCnt], -1, (255, 0, 0), 3) show_image(temp_image) raise OmrException( 'no suitable outer contour found, ' 'biggest outer contour had perim of {}, needs to be bigger than {}' .format(perim, min_acceptable_perim)) return docCnt
def stitch(images): stitcher = cv2.createStitcher() if imutils.is_cv3( ) else cv2.Stitcher_create() status, stitched = stitcher.stitch(images) # 四周填充黑色像素,再得到阈值图 stitched = cv2.copyMakeBorder(stitched, 10, 10, 10, 10, cv2.BORDER_CONSTANT, (0, 0, 0)) gray = cv2.cvtColor(stitched, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY) cnts, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnt = max(cnts, key=cv2.contourArea) mask = np.zeros(thresh.shape, dtype="uint8") x, y, w, h = cv2.boundingRect(cnt) cv2.rectangle(mask, (x, y), (x + w, y + h), 255, -1) minRect = mask.copy() sub = mask.copy() # 开始while循环,直到sub中不再有前景像素 while cv2.countNonZero(sub) > 0: minRect = cv2.erode(minRect, None) sub = cv2.subtract(minRect, thresh) cnts, hierarchy = cv2.findContours(minRect.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnt = max(cnts, key=cv2.contourArea) x, y, w, h = cv2.boundingRect(cnt) # 使用边界框坐标提取最终的全景图 return stitched[y:y + h, x:x + w]
def create_stitich(): in_path = os.path.join('..', 'data', 'interim', 'frames', '2019-08-13-1700') # 4Dec18 Ubir Geese out_path = os.path.join('..', 'data', 'interim', 'stitched') image_paths = sorted(list(paths.list_images(in_path))) print(image_paths) images = [] for image_path in image_paths: #if 'DJI_0455' in image_path or 'DJI_0456' in image_path: image = cv2.imread(image_path) images.append(image) stitcher = cv2.createStitcher() if imutils.is_cv3( ) else cv2.Stitcher_create() (status, stitched) = stitcher.stitch(images) file_name = '{0}.jpg'.format(in_path.split(os.sep)[-1]) out_path = os.path.join(out_path, file_name) if status == 0: cv2.imwrite(out_path, stitched) cv2.imshow("Stitched", stitched) cv2.waitKey(0) else: print("[INFO] image stitching failed ({})".format(status))
def findRedBagOpen(screen): results = [] if screen.size: img2 = cv2.split(screen) ret, dst1 = cv2.threshold(img2[0], 200, 255, cv2.THRESH_BINARY_INV) ret, dst2 = cv2.threshold(img2[1], 160, 240, cv2.THRESH_BINARY) ret, dst3 = cv2.threshold(img2[2], 170, 255, cv2.THRESH_BINARY_INV) img2 = dst1&dst2&dst3 # 相与 cnts = cv2.findContours(img2, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[1] if imutils.is_cv3() else cnts[0] dstPoints = [] if len(cnts): for c in cnts: # 获取中心点 M = cv2.moments(c) if not M["m00"]: continue area = cv2.contourArea(c) if area < 30000: continue cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) # dstPoints.append((cX,cY)) return dstPoints else: raise Exception('Screen process is unsuccessful')
def markOnImg(img, width, height): '''在四点标记的图片上,将涂黑的选项标记,并返回(图片,坐标)''' docCnt = getFourPtTrans(img) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) paper = four_point_transform(img, docCnt) warped = four_point_transform(gray, docCnt) # 灰度图二值化 thresh = cv2.adaptiveThreshold(warped, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 15, 2) # resize thresh = cv2.resize(thresh, (width, height), cv2.INTER_LANCZOS4) paper = cv2.resize(paper, (width, height), cv2.INTER_LANCZOS4) warped = cv2.resize(warped, (width, height), cv2.INTER_LANCZOS4) ChQImg = cv2.blur(thresh, (13, 13)) # 二值化,120是阈值 ChQImg = cv2.threshold(ChQImg, 120, 225, cv2.THRESH_BINARY)[1] # cv2.imwrite("paper.jpg",paper) Answer = [] # 二值图中找答案轮廓 cnts = cv2.findContours(ChQImg, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[1] if imutils.is_cv3() else cnts[0] for c in cnts: x, y, w, h = cv2.boundingRect(c) if w > 50 and h > 20 and w < 100 and h < 100: M = cv2.moments(c) cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) cv2.drawContours(paper, c, -1, (0, 0, 255), 5) cv2.circle(paper, (cX, cY), 7, (255, 255, 255), 2) Answer.append((cX, cY)) return paper, Answer
def __init__(self): # determine if we are using OpenCV v3.X self.isv3 = imutils.is_cv3(or_better=True) self.stitched_result = [] self.stitched_depth_result = [] self.depth_images_path = [] self.i = 0
def extractFeatures(self, image): # convert the image to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # detect and extract features from the image # --> need SIFT because the mosaic can be made from camera that takes images with different Scale and Rotation # if we are using opencv 2.4.9 if imutils.is_cv2(): # the default parameters for cv2.SIFT() nfeatures = 0 nOctaveLayers = 3 contrastTreshold = 0.04 edgeTreshold = 10 sigma = 1.6 sift = cv2.SIFT(nfeatures, nOctaveLayers, contrastTreshold, edgeTreshold, sigma) (kps, features) = sift.detectAndCompute(gray, None) # (image, None) # check to see if we are using OpenCV 3 elif imutils.is_cv3(): descriptor = cv2.xfeatures2d.SIFT_create() (kps, features) = descriptor.detectAndCompute(gray, None) self.logger.info("Found {} keypoints in frame".format(len(kps))) # convert the keypoints from KeyPoint objects to np kps = np.float32([kp.pt for kp in kps]) # return a tuple of keypoints and features return (kps, features)
def __init__(self, imageList_, dataMatrix_): ''' :param imageList_: List of all images in dataset. :param dataMatrix_: Matrix with all pose data in dataset. :return: ''' self.imageList = [] self.dataMatrix = dataMatrix_ detector = None if imutils.is_cv2(): detector = cv2.ORB() elif imutils.is_cv3(): detector = cv2.ORB_create() for i in range(0, len(imageList_)): image = imageList_[ i][::2, :: 2, :] #downsample the image to speed things up. 4000x3000 is huge! M = gm.computeUnRotMatrix(self.dataMatrix[i, :]) #Perform a perspective transformation based on pose information. #Ideally, this will mnake each image look as if it's viewed from the top. #We assume the ground plane is perfectly flat. correctedImage = gm.warpPerspectiveWithPadding(image, M) self.imageList.append( correctedImage ) #store only corrected images to use in combination self.resultImage = self.imageList[0]
def main(): # 加载参数 args = parser_args() print(args.images) # 获取图像列表 imagePaths = sorted(list(paths.list_images(args.images))) images = [] # 加载图像 for imagePath in imagePaths: image = cv2.imread(imagePath) images.append(image) # 拼接 print("[INFO] stitching images...") stitcher = cv2.createStitcher() if imutils.is_cv3( ) else cv2.Stitcher_create() (status, stitched) = stitcher.stitch(images) if status == 0: # status=0时,表示拼接成功 if args.crop > 0: # 边界填充 stitched = cv2.copyMakeBorder(stitched, 10, 10, 10, 10, cv2.BORDER_CONSTANT, (0, 0, 0)) # 转为灰度图进行并二值化 gray = cv2.cvtColor(stitched, cv2.COLOR_BGR2GRAY) thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)[1] # 获取轮廓 cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) c = max(cnts, key=cv2.contourArea) mask = np.zeros(thresh.shape, dtype="uint8") (x, y, w, h) = cv2.boundingRect(c) cv2.rectangle(mask, (x, y), (x + w, y + h), 255, -1) minRect = mask.copy() sub = mask.copy() while cv2.countNonZero(sub) > 0: minRect = cv2.erode(minRect, None) sub = cv2.subtract(minRect, thresh) cnts = cv2.findContours(minRect.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) c = max(cnts, key=cv2.contourArea) (x, y, w, h) = cv2.boundingRect(c) # 取出图像区域 stitched = stitched[y:y + h, x:x + w] cv2.imwrite(args.output, stitched) else: print("[INFO] image stitching failed ({})".format(status))
def process_par(image, output, listBigBox, listResult): if len(listBigBox) > 0: listBigBox.sort(key=lambda x: x[1]) # print(listBigBox[0][1]) results = [] gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) _, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) # assign a rectangle kernel size kernel = np.ones((5, 5), 'uint8') par_img = cv2.dilate(thresh, kernel, iterations=5) if imutils.is_cv2() or imutils.is_cv4(): (contours, hierarchy) = cv2.findContours(par_img.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) elif imutils.is_cv3(): (_, contours, hierarchy) = cv2.findContours(par_img.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if len(contours) > 0: sorted_contours = sorted(contours, key=lambda ctr: cv2.boundingRect(ctr)[0] + cv2 .boundingRect(ctr)[1] * image.shape[1]) for i, cnt in enumerate(sorted_contours): x, y, w, h = cv2.boundingRect(cnt) cv2.rectangle(output, (x, y), (x + w, y + h), (0, 255, 0), 1) cv2.imwrite("rs.jpg", output) k = 1 for i, cnt in enumerate(sorted_contours): x, y, w, h = cv2.boundingRect(cnt) cv2.rectangle(output, (x, y), (x + w, y + h), (0, 255, 0), 1) # printImage(output) crop = output[y:y + h, x:x + w] if len(listBigBox) > k - 1: if y > listBigBox[0][1]: # string_coordinate, listBigBox = appendListBigBox(listBigBox, output, listResult) results.append( ('', listBigBox[k - 1][0], listBigBox[k - 1][1], listBigBox[k - 1][2], listBigBox[k - 1][3], k)) k += 1 # string_coordinate.sort(key=lambda k: (k[2],k[1])) # string_coordinate.sort(key=functools.cmp_to_key(compare_table)) # string_coordinate.sort(key=functools.cmp_to_key(compare_table)) # for st in string_coordinate: # results.append(st) cv2.imwrite("temp.jpg", crop) output_tesseract = pytesseract.image_to_string( Image.open('temp.jpg'), lang='vie') if output_tesseract == '': continue temp = (output_tesseract, x, y, w, h, 0) # print(i , " " , temp) # print("###########") results.append(temp) # results.append(pytesseract.image_to_string(Image.open('temp.jpg'), # lang='vie')) return output, results
def __init__(self, videoPath, ratio, reprojThresh): self.videoPath = videoPath self.vidcap = cv2.VideoCapture(videoPath) initialFrame = int(self.vidcap.get(cv2.CAP_PROP_FRAME_COUNT)) self.videoSize = (int(self.vidcap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(self.vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT))) self.vidcap.set(cv2.CAP_PROP_POS_FRAMES, initialFrame / 6) self.ratio = ratio self.reprojThresh = reprojThresh self.isv3 = imutils.is_cv3()
def draw_str(dst, target, s, fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.5, color=(255, 255, 255), bgcolor=(0, 0, 0), thickness=1): x, y = target if imutils.is_cv3(): line_type = cv2.LINE_AA if imutils.is_cv2(): line_type = cv2.cv.CV_AA cv2.putText(dst, s, (x + 1, y + 1), fontFace, fontScale, bgcolor, thickness=thickness + 1, lineType=line_type) cv2.putText(dst, s, (x, y), fontFace, fontScale, color, thickness=thickness, lineType=line_type)
def scaleAndCrop(self, img, out_width): # scale resized = imutils.resize(img, width=out_width) # crop where? grey = cv2.cvtColor(resized,cv2.COLOR_BGR2GRAY) [ret, thresh] = cv2.threshold(grey,10,255,cv2.THRESH_BINARY) #TODO: solve isssue : x,y,w,h = cv2.boundingRect(cnt) , TypeError: points is not a numpy array, neither a scalar # check to see if we are using OpenCV 2.X if imutils.is_cv2(): [contours, hierarchy] = cv2.findContours(thresh, 1, 2) cnt = contours[0] # check to see if we are using OpenCV 3 elif imutils.is_cv3(): (_, cnts, _) = cv2.findContours(thresh, 1, 2) cnt = cnts[0] #x,y,w,h = cv2.boundingRect(cnt) [x, y, w, h] = cv2.boundingRect(cnt) crop = resized[y:y + h, x:x + w] return crop
def init_props(cap, config): ''' set and store the real camera properties ''' prop = None if imutils.is_cv3(): if hasattr(cv2, "CAP_PROP_FPS"): prop = cv2.CAP_PROP_FPS elif imutils.is_cv2(): if hasattr(cv2.cv, "CV_CAP_PROP_FPS"): prop = cv2.cv.CV_CAP_PROP_FPS if prop is None: print("OpenCV not compiled with camera framerate property!") else: try: cap.set(prop, config.camera.fps) config.camera.fps = cap.get(prop) except: print( "Unable to set framerate to {:.1f}!".format(config.camera.fps)) config.camera.fps = cap.get(prop) finally: print("-- framerate: {}".format(config.camera.fps)) # set the resolution as specified at the top prop = [None, None] if imutils.is_cv3(): if hasattr(cv2, "CAP_PROP_FRAME_WIDTH"): prop = [cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT] elif imutils.is_cv2(): if hasattr(cv2.cv, "CV_CAP_PROP_FRAME_WIDTH"): prop = [cv2.cv.CV_CAP_PROP_FRAME_WIDTH, cv2.cv.CV_CAP_PROP_FRAME_HEIGHT] if any(p is None for p in prop): print("OpenCV not compiled with camera resolution properties!") else: try: for i in range(1, 2): cap.set(prop[i], config.camera.res[i]) config.camera.res[i] = int(cap.get(prop[i])) except: print( "Unable to set resolution to {}x{}!".format(*config.camera.res)) config.camera.res = [int(cap.get(p)) for p in prop] finally: print("-- resolution: {}x{}".format(*config.camera.res)) # try to find the fourcc of the attached camera prop = None if imutils.is_cv3(): if hasattr(cv2, "CAP_PROP_FOURCC"): prop = cv2.CAP_PROP_FOURCC elif imutils.is_cv2(): if hasattr(cv2.cv, "CV_CAP_PROP_FOURCC"): prop = cv2.cv.CV_CAP_PROP_FOURCC if prop is None: print("OpenCV not compiled with fourcc property!") else: try: config.camera.fourcc = cap.get(prop) except: print("Unable to read camera's codec!") finally: print("-- fourcc: {}".format(config.camera.fourcc))
# object was detected - computed from the frame width for simplicity contourThresh = int( 2100 * (float(config.computing.width) / config.camera.res[0])) # display the window if it's enabled in the config if config.window.enabled: # create the window cv2.namedWindow(config.window.name, cv2.WINDOW_NORMAL) cv2.namedWindow("Background Model", cv2.WINDOW_AUTOSIZE) def update_processing_width(x): config.computing.width = x # porting all this into opencv2.4 is a real pain, so just cv3 if imutils.is_cv3(): def update_sub_hist(x): config.computing.bg_sub_hist = x def updatebg_sub_thresh(x): config.computing.bg_sub_thresh = x # trackbars for the window gui cv2.createTrackbar( 'Motion Hist.', config.window.name, 0, 800, update_sub_hist) cv2.createTrackbar( 'Motion Thresh.', config.window.name, 0, 40, updatebg_sub_thresh) # set initial positions cv2.setTrackbarPos( 'Motion Hist.', config.window.name, config.computing.bg_sub_hist)
def __init__(self): # determine if we are using OpenCV v3.X and initialize the # cached homography matrix self.isv3 = imutils.is_cv3() self.cachedH = None
def __init__(self): # determine if we are using OpenCV v3.X self.isv3 = imutils.is_cv3() print "instanting stitcher"
def __init__(self): # determine if we are using OpenCV v3.X self.isv3 = imutils.is_cv3()
def __init__(self): self.isv3 = imutils.is_cv3() self.cachedH = None
# import the necessary packages from __future__ import print_function import imutils import cv2 # print the current OpenCV version on your system print("Your OpenCV version: {}".format(cv2.__version__)) # check to see if you are using OpenCV 2.X print("Are you using OpenCV 2.X? {}".format(imutils.is_cv2())) # check to see if you are using OpenCV 3.X print("Are you using OpenCV 3.X? {}".format(imutils.is_cv3()))
def __init__(self): self.isv3 = imutils.is_cv3()
# author: Adrian Rosebrock # website: http://www.pyimagesearch.com # import the necessary packages from __future__ import print_function import imutils import cv2 # print the current OpenCV version on your system print("Your OpenCV version: {}".format(cv2.__version__)) # check to see if you are using OpenCV 2.X print("Are you using OpenCV 2.X? {}".format(imutils.is_cv2())) # check to see if you are using OpenCV 3.X print("Are you using OpenCV 3.X? {}".format(imutils.is_cv3(or_better=False))) # check to see if you are using OpenCV 4.X print("Are you using OpenCV 4.X? {}".format(imutils.is_cv4(or_better=False))) # check to see if you are using *at least* OpenCV 2.X print("Are you using at least OpenCV 3.X? {}".format(imutils.is_cv3())) # check to see if you are using *at least* OpenCV 3.X print("Are you using at least OpenCV 3.X? {}".format(imutils.is_cv3())) # check to see if you are using *at least* OpenCV 4.X print("Are you using at least OpenCV 4.X? {}".format(imutils.is_cv4())) # should throw a deprecation warning print("Checking for OpenCV 3: {}".format(imutils.check_opencv_version("3")))
def __init__(self): # check if OpenCV v3.X is being used self.isv3 = imutils.is_cv3()