Python cv2 模块,THRESH_OTSU 实例源码

我们从Python开源项目中,提取了以下40个代码示例,用于说明如何使用cv2.THRESH_OTSU

项目:bib-tagger    作者:KateRita    | 项目源码 | 文件源码
def find_bibs(image):
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY);
  binary = cv2.GaussianBlur(gray,(5,5),0)
  ret,binary = cv2.threshold(binary, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU);
  #binary = cv2.adaptiveThreshold(binary, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
  #ret,binary = cv2.threshold(binary, 190, 255, cv2.THRESH_BINARY);

  #lapl = cv2.Laplacian(image,cv2.CV_64F)
  #gray = cv2.cvtColor(lapl, cv2.COLOR_BGR2GRAY);
  #blurred = cv2.GaussianBlur(lapl,(5,5),0)
  #ret,binary = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU);
  #cv2.imwrite("lapl.jpg", lapl)

  edges = cv2.Canny(image,175,200)
  cv2.imwrite("edges.jpg", edges)
  binary = edges

  cv2.imwrite("binary.jpg", binary)
  contours,hierarchy = find_contours(binary)

  return get_rectangles(contours)
项目:CE264-Computer_Vision    作者:RobinCPC    | 项目源码 | 文件源码
def find_contour(self, img_src, Rxmin, Rymin, Rxmax, Rymax):
        cv2.rectangle(img_src, (Rxmax, Rymax), (Rxmin, Rymin), (0, 255, 0), 0)
        crop_res = img_src[Rymin: Rymax, Rxmin:Rxmax]
        grey = cv2.cvtColor(crop_res, cv2.COLOR_BGR2GRAY)

        _, thresh1 = cv2.threshold(grey, 127, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

        cv2.imshow('Thresh', thresh1)
        contours, hierchy = cv2.findContours(thresh1.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

        # draw contour on threshold image
        if len(contours) > 0:
            cv2.drawContours(thresh1, contours, -1, (0, 255, 0), 3)

        return contours, crop_res


# Check ConvexHull  and Convexity Defects
项目:ConditionalGAN    作者:seungjooli    | 项目源码 | 文件源码
def detect_edges(images):
        def blur(image):
            return cv2.GaussianBlur(image, (5, 5), 0)

        def canny_otsu(image):
            scale_factor = 255
            scaled_image = np.uint8(image * scale_factor)

            otsu_threshold = cv2.threshold(
                cv2.cvtColor(scaled_image, cv2.COLOR_RGB2GRAY), 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[0]
            lower_threshold = max(0, int(otsu_threshold * 0.5))
            upper_threshold = min(255, int(otsu_threshold))
            edges = cv2.Canny(scaled_image, lower_threshold, upper_threshold)
            edges = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)

            return np.float32(edges) * (1 / scale_factor)

        blurred = [blur(image) for image in images]
        canny_applied = [canny_otsu(image) for image in blurred]

        return canny_applied
项目:bib-tagger    作者:KateRita    | 项目源码 | 文件源码
def find_bib(image):
  width, height, depth = image.shape

  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY);
  #gray = cv2.equalizeHist(gray)
  blurred = cv2.GaussianBlur(gray,(5,5),0)

  debug_output("find_bib_blurred", blurred)
  #binary = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, blockSize=25, C=0);
  ret,binary = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU);
  #ret,binary = cv2.threshold(blurred, 170, 255, cv2.THRESH_BINARY);
  debug_output("find_bib_binary", binary)
  threshold_contours,hierarchy = find_contours(binary)

  debug_output("find_bib_threshold", binary)

  edges = cv2.Canny(gray,175,200, 3)
  edge_contours,hierarchy = find_contours(edges)

  debug_output("find_bib_edges", edges)

  contours = threshold_contours + edge_contours
  debug_output_contours("find_bib_threshold_contours", image, contours)

  rectangles = get_rectangles(contours)

  debug_output_contours("find_bib_rectangles", image, rectangles)

  potential_bibs = [rect for rect in rectangles if is_potential_bib(rect, width*height)]

  debug_output_contours("find_bib_potential_bibs", image, potential_bibs)

  ideal_aspect_ratio = 1.0
  potential_bibs = sorted(potential_bibs, key = lambda bib: abs(aspect_ratio(bib) - ideal_aspect_ratio))

  return potential_bibs[0] if len(potential_bibs) > 0 else np.array([[(0,0)],[(0,0)],[(0,0)],[(0,0)]])

#
# Checks that the size and aspect ratio of the contour is appropriate for a bib.
#
项目:Yugioh-bot    作者:will7200    | 项目源码 | 文件源码
def test_initial_pass_through_compare(self):
        original = cv2.imread(os.path.join(self.provider.assets, "start_screen.png"))
        against = self.provider.get_img_from_screen_shot()
        wrong = cv2.imread(os.path.join(self.provider.assets, "battle.png"))

        # convert the images to grayscale
        original = mask_image([127], [255], cv2.cvtColor(original, cv2.COLOR_BGR2GRAY), True)
        against = mask_image([127], [255], cv2.cvtColor(against, cv2.COLOR_BGR2GRAY), True)
        wrong = mask_image([127], [255], cv2.cvtColor(wrong, cv2.COLOR_BGR2GRAY), True)
        # initialize the figure
        (score, diff) = compare_ssim(original, against, full=True)
        diff = (diff * 255).astype("uint8")
        self.assertTrue(score > .90, 'If this is less then .90 the initial compare of the app will fail')
        (score, nothing) = compare_ssim(original, wrong, full=True)
        self.assertTrue(score < .90)
        if self.__debug_pictures__:
            # threshold the difference image, followed by finding contours to
            # obtain the regions of the two input images that differ
            thresh = cv2.threshold(diff, 0, 255,
                                   cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
            cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                                    cv2.CHAIN_APPROX_SIMPLE)
            cnts = cnts[0]
            # loop over the contours
            for c in cnts:
                # compute the bounding box of the contour and then draw the
                # bounding box on both input images to represent where the two
                # images differ
                (x, y, w, h) = cv2.boundingRect(c)
                cv2.rectangle(original, (x, y), (x + w, y + h), (0, 0, 255), 2)
                cv2.rectangle(against, (x, y), (x + w, y + h), (0, 0, 255), 2)
            # show the output images
            diffs = ("Original", original), ("Modified", against), ("Diff", diff), ("Thresh", thresh)
            images = ("Original", original), ("Against", against), ("Wrong", wrong)
            self.setup_compare_images(diffs)
            self.setup_compare_images(images)
项目:idmatch    作者:maddevsio    | 项目源码 | 文件源码
def recognize_text(original):
    idcard = original
    gray = cv2.cvtColor(idcard, cv2.COLOR_BGR2GRAY)

    # Morphological gradient:
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
    opening = cv2.morphologyEx(gray, cv2.MORPH_GRADIENT, kernel)

    # Binarization
    ret, binarization = cv2.threshold(opening, 0.0, 255.0, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

    # Connected horizontally oriented regions
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 1))
    connected = cv2.morphologyEx(binarization, cv2.MORPH_CLOSE, kernel)

    # find countours
    _, contours, hierarchy = cv2.findContours(
        connected, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE
    )
    return contours, hierarchy
项目:HandwritingRecognition    作者:eng-tsmith    | 项目源码 | 文件源码
def thresholding(img_grey):
    """
    This functions creates binary images using thresholding
    :param img_grey: greyscale image
    :return: binary image
    """
    # # Adaptive Gaussian
    # img_binary = cv.adaptiveThreshold(img_grey, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 11, 2)

    # Otsu's thresholding after Gaussian filtering
    blur = cv.GaussianBlur(img_grey, (5, 5), 0)
    ret3, img_binary = cv.threshold(blur, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)

    # invert black = 255
    ret, thresh1 = cv.threshold(img_binary, 127, 255, cv.THRESH_BINARY_INV)

    return thresh1
项目:HandwritingRecognition    作者:eng-tsmith    | 项目源码 | 文件源码
def thresholding(img_grey):
    """
    This functions creates binary images using thresholding
    :param img_grey: greyscale image
    :return: binary image
    """
    # # Global
    # ret1, thresh1 = cv.threshold(img_grey, 127, 255, cv.THRESH_BINARY_INV)
    # show_img(thresh1)
    #
    # # Adaptive Mean
    # img_binary = cv.adaptiveThreshold(img_grey, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 11, 2)
    # ret2, thresh2 = cv.threshold(img_binary, 127, 255, cv.THRESH_BINARY_INV)
    # show_img(thresh2)
    #
    # # Adaptive Gaussian
    # img_binary = cv.adaptiveThreshold(img_grey, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 11, 2)
    # ret3, thresh3 = cv.threshold(img_binary, 127, 255, cv.THRESH_BINARY_INV)
    # show_img(thresh3)

    # Otsu's thresholding after Gaussian filtering
    blur = cv.GaussianBlur(img_grey, (5, 5), 0)
    ret4, img_otsu = cv.threshold(blur, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
    ret4, thresh4 = cv.threshold(img_otsu, 127, 255, cv.THRESH_BINARY_INV)
    # show_img(thresh4)

    return thresh4
项目:FaceDetect    作者:PCJohn    | 项目源码 | 文件源码
def localize(img,model_path):
    sess = tfac.start_sess()
    y,x_hold,y_hold,keep_prob = build_net(sess)
    saver = tf.train.Saver()
    saver.restore(sess,model_path)

    #Run all detection at a fixed size
    img = cv2.resize(img,DET_SIZE)
    mask = np.zeros(img.shape)
    #Run sliding windows of different sizes
    for bx in range(WIN_MIN,WIN_MAX,WIN_STRIDE):
        by = bx
        for i in xrange(0, img.shape[1]-bx, X_STEP):
            for j in xrange(0, img.shape[0]-by, Y_STEP):
                sub_img = cv2.resize(img[i:i+bx,j:j+by],face_ds.IN_SIZE)
                X = sub_img.reshape((1,tfac.dim_prod(face_ds.IN_SIZE)))
                out = y.eval(session=sess,feed_dict={x_hold:X,keep_prob:1})[0]
                if out[0] >= CONF_THRESH:
                    mask[i:i+bx,j:j+by] = mask[i:i+bx,j:j+by]+1

    sess.close()
    mask = np.uint8(255*mask/np.max(mask))
    faces = img*(cv2.threshold(cv2.blur(mask,BLUR_DIM),0,255,cv2.THRESH_OTSU)[1]/255)
    return (faces,mask)
项目:sia-cog    作者:deepakkumar1984    | 项目源码 | 文件源码
def extracttext(imgpath, preprocess):
    if imgpath.startswith('http://') or imgpath.startswith('https://') or imgpath.startswith('ftp://'):
        image = url_to_image(imgpath)
    else:
        image = cv2.imread(imgpath)

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    if preprocess == "thresh":
        gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    elif preprocess == "blur":
        gray = cv2.medianBlur(gray, 3)

    filename = "{}.png".format(os.getpid())
    cv2.imwrite(filename, gray)
    text = pytesseract.image_to_string(Image.open(filename))

    os.remove(filename)
    return {"text": text}
项目:Robo-Plot    作者:JackBuck    | 项目源码 | 文件源码
def process_image(image):
    """

    Args:
        image: The image to process

    Returns:
        sub_image: The rotated and extracted.

    """

    # Convert image to black and white - we cannot take the photos in black and white as we
    # must first search for the red triangle.

    if len(image.shape) == 3:
        processed_img = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    else:
        processed_img = image

    if config.real_hardware:
        num_iterations = 8
    else:
        num_iterations = 8

        processed_img = cv2.GaussianBlur(processed_img, (21, 21), 0)
    _, processed_img = cv2.threshold(processed_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

    # Put a border around the image to stop the edges of the images creating artifacts.
    padded_image = np.zeros((processed_img.shape[0] + 10, processed_img.shape[1] + 10), np.uint8)
    padded_image[5:processed_img.shape[0]+5, 5:processed_img.shape[1]+5] = processed_img

    kernel = np.array([[0, 1, 1, 1, 0],
                       [1, 1, 1, 1, 1],
                       [1, 1, 1, 1, 1],
                       [1, 1, 1, 1, 1],
                       [0, 1, 1, 1, 0]], np.uint8)

    padded_image = cv2.erode(padded_image, kernel, iterations=num_iterations)
    processed_img = padded_image[25:padded_image.shape[0] - 25, 25:padded_image.shape[1] - 25]

    #cv2.imshow('Padded Image', padded_image)
    #cv2.imshow('Processed image', processed_img)
    #cv2.waitKey(0)



    # Debugging code - useful to show the images are being eroded correctly.
    #spacer = processed_img[:, 0:2].copy()
    #spacer.fill(100)
    #combined_image = np.concatenate((processed_img, spacer), axis=1)
    #combined_image = np.concatenate((combined_image, image), axis=1)
    #cv2.imshow('PreProcessed and Processed Image', combined_image)
    #cv2.waitKey(0)

    # Save sub_image to debug folder if required.
    if __debug__:
        iadebug.save_processed_image(processed_img)

    return processed_img
项目:virtual-dressing-room    作者:akash0x53    | 项目源码 | 文件源码
def detect_shirt(self):


        #self.dst=cv2.inRange(self.norm_rgb,np.array([self.lb,self.lg,self.lr],np.uint8),np.array([self.b,self.g,self.r],np.uint8))
        self.dst=cv2.inRange(self.norm_rgb,np.array([20,20,20],np.uint8),np.array([255,110,80],np.uint8))
        cv2.threshold(self.dst,0,255,cv2.THRESH_OTSU+cv2.THRESH_BINARY)
        fg=cv2.erode(self.dst,None,iterations=2)
        #cv2.imshow("fore",fg)  
        bg=cv2.dilate(self.dst,None,iterations=3)
        _,bg=cv2.threshold(bg, 1,128,1)
        #cv2.imshow("back",bg)

        mark=cv2.add(fg,bg)
        mark32=np.int32(mark)
        cv2.watershed(self.norm_rgb,mark32)
        self.m=cv2.convertScaleAbs(mark32)
        _,self.m=cv2.threshold(self.m,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
        #cv2.imshow("final_tshirt",self.m)

        cntr,h=cv2.findContours(self.m,cv2.cv.CV_RETR_EXTERNAL,cv2.cv.CV_CHAIN_APPROX_SIMPLE)

        return self.m,cntr
项目:pyku    作者:dubvulture    | 项目源码 | 文件源码
def create_model(self, train_folder):
        """
        Return the training set, its labels and the trained model
        :param train_folder: folder where to retrieve data
        :return: (train_set, train_labels, trained_model)
        """
        digits = []
        labels = []
        for n in range(1, 10):
            folder = train_folder + str(n)
            samples = [pic for pic in os.listdir(folder)
                       if os.path.isfile(os.path.join(folder, pic))]

            for sample in samples:
                image = cv2.imread(os.path.join(folder, sample))
                # Expecting black on white
                image = 255 - cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
                _, image = cv2.threshold(image, 0, 255,
                                         cv2.THRESH_BINARY + cv2.THRESH_OTSU)
                feat = self.feature(image)
                digits.append(feat)
                labels.append(n)

        digits = np.array(digits, np.float32)
        labels = np.array(labels, np.float32)
        if cv2.__version__[0] == '2':
            model = cv2.KNearest()
            model.train(digits, labels)
        else:
            model = cv2.ml.KNearest_create()
            model.train(digits, cv2.ml.ROW_SAMPLE, labels)
        return digits, labels, model
项目:ATX    作者:NetEaseGame    | 项目源码 | 文件源码
def do_touch(self):
        width, height = 1080, 1920

        screen = self.device.screenshot_cv2()
        h, w = screen.shape[:2]
        img = cv2.resize(screen, (w/2, h/2))
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        edges = cv2.Canny(gray, 80, 200)
        _, thresh = cv2.threshold(edges, 0, 255, cv2.THRESH_OTSU)
        contours, _ = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
        contours.sort(key=lambda cnt: len(cnt), reverse=True)

        rects = []
        for cnt in contours:
            hull = cv2.convexHull(cnt)
            hull_area = cv2.contourArea(hull)
            x,y,w,h = cv2.boundingRect(cnt)
            rect_area = float(w*h)
            if w<20 or h<20 or rect_area<100:
                continue
            if hull_area/rect_area < 0.50:
                continue
            rects.append((x, y, x+w, y+h))
            cv2.rectangle(img, (x, y), (x+w, y+h), 255, 2)

        if not rects:
            x, y = randint(1, width), randint(1, height)
        else:
            x1, y1, x2, y2 = choice(rects)
            x, y = randint(x1, x2), randint(y1, y2)
            cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)

        x, y = self.device.screen2touch(x*2, y*2)
        self.device.touch(x, y)
        cv2.imshow('img', img)
        cv2.waitKey(1)
项目:identidade_ocr    作者:Ronald-TR    | 项目源码 | 文件源码
def binarize(img):  #definindo binarização dos cortes
    a = np.asarray(img)
    a = cv2.cvtColor(a, cv2.COLOR_RGB2GRAY)
    ret, imbin = cv2.threshold(a, 127, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY)
    return Image.fromarray(imbin)
项目:cervix-roi-segmentation-by-unet    作者:scottykwok    | 项目源码 | 文件源码
def to_binary_mask(mask, t=0.00001):
    mask = inverse_preprocessing(mask)

    ### Threshold the RGB image  - This step increase sensitivity
    mask[mask > t] = 255
    mask[mask <= t] = 0

    ### To grayscale and normalize
    mask_gray = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
    mask_gray = cv2.normalize(src=mask_gray, dst=None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)

    ### Auto binary threshold
    (thresh, mask_binary) = cv2.threshold(mask_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    return mask_binary
项目:tefla    作者:litan    | 项目源码 | 文件源码
def crop_image(fname,target_size):
    print('Processing image: %s' % fname)

    #otsu thresholding
    img = Image.open(fname)
    blurred = img.filter(ImageFilter.BLUR)
    ba = np.array(blurred)
    gray_image = cv2.cvtColor(ba, cv2.COLOR_BGR2GRAY)
    retval2, threshold2 = cv2.threshold(gray_image, 125, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

    #storing white pixel in  each row and column in two arrays
    #these arrays are later used to find boundaries for cropping image
    row_white_pixel_count=np.count_nonzero(threshold2,axis=1)
    col_white_pixel_count=np.count_nonzero(threshold2,axis=0)

    #find x,y,w,h for cropping image
    y=find_boundary(row_white_pixel_count,col_white_pixel_count.size)
    h=find_boundary_reverse(row_white_pixel_count,col_white_pixel_count.size)
    x=find_boundary(col_white_pixel_count,row_white_pixel_count.size)
    w=find_boundary_reverse(col_white_pixel_count,row_white_pixel_count.size)
    crop_array = ba[y:h, x:w]

    #resize the image
    crop_img=Image.fromarray(crop_array)
    resized = crop_img.resize([target_size, target_size])


    #uncomment below line to see histogram of both white pixel vs rows and white pixel vs columns
    subplots(threshold2, row_white_pixel_count, col_white_pixel_count, crop_img)
    return resized
项目:AutomatorX    作者:xiaoyaojjian    | 项目源码 | 文件源码
def do_touch(self):
        width, height = 1080, 1920

        screen = self.device.screenshot_cv2()
        h, w = screen.shape[:2]
        img = cv2.resize(screen, (w/2, h/2))
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        edges = cv2.Canny(gray, 80, 200)
        _, thresh = cv2.threshold(edges, 0, 255, cv2.THRESH_OTSU)
        contours, _ = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
        contours.sort(key=lambda cnt: len(cnt), reverse=True)

        rects = []
        for cnt in contours:
            hull = cv2.convexHull(cnt)
            hull_area = cv2.contourArea(hull)
            x,y,w,h = cv2.boundingRect(cnt)
            rect_area = float(w*h)
            if w<20 or h<20 or rect_area<100:
                continue
            if hull_area/rect_area < 0.50:
                continue
            rects.append((x, y, x+w, y+h))
            cv2.rectangle(img, (x, y), (x+w, y+h), 255, 2)

        if not rects:
            x, y = randint(1, width), randint(1, height)
        else:
            x1, y1, x2, y2 = choice(rects)
            x, y = randint(x1, x2), randint(y1, y2)
            cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)

        x, y = self.device.screen2touch(x*2, y*2)
        self.device.touch(x, y)
        cv2.imshow('img', img)
        cv2.waitKey(1)
项目:Comicolorization    作者:DwangoMediaVillage    | 项目源码 | 文件源码
def convert_to_linedrawing(self, luminous_image_data):
        linedrawing = cv2.threshold(luminous_image_data, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
        return linedrawing
项目:doc2text    作者:jlsutherland    | 项目源码 | 文件源码
def process_skewed_crop(image):
    theta = compute_skew(estimate_skew(image))
    ret, thresh = cv2.threshold(image.copy(), 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    rotated = rotate(thresh, theta)
    return (rotated, theta)
项目:BAR4Py    作者:bxtkezhan    | 项目源码 | 文件源码
def poolFrame(self, frame, pool_size=(28, 28)):
        '''
        frame is OpenCV image frame
        return pooled frame
        '''
        if len(frame.shape) == 2:
            gray_frame = frame
        else:
            gray_frame = bgr2gray(frame)
        pool_frame = cv2.resize(gray_frame, pool_size)
        _, pool_frame = cv2.threshold(pool_frame, pool_frame.mean(), 1, cv2.THRESH_OTSU)
        return pool_frame
项目:BAR4Py    作者:bxtkezhan    | 项目源码 | 文件源码
def poolFrame(self, frame, pool_size=(28, 28)):
        '''
        frame is OpenCV image frame
        return pooled frame
        '''
        if len(frame.shape) == 2:
            gray_frame = frame
        else:
            gray_frame = bgr2gray(frame)
        pool_frame = cv2.resize(gray_frame, pool_size)
        _, pool_frame = cv2.threshold(pool_frame, pool_frame.mean(), 1, cv2.THRESH_OTSU)
        return pool_frame
项目:BAR4Py    作者:bxtkezhan    | 项目源码 | 文件源码
def poolFrame(self, frame, pool_size=(28, 28)):
        '''
        frame is OpenCV image frame
        return pooled frame
        '''
        if len(frame.shape) == 2:
            gray_frame = frame
        else:
            gray_frame = bgr2gray(frame)
        pool_frame = cv2.resize(gray_frame, pool_size)
        _, pool_frame = cv2.threshold(pool_frame, pool_frame.mean(), 1, cv2.THRESH_OTSU)
        return pool_frame
项目:ghetto_omr    作者:pohzhiee    | 项目源码 | 文件源码
def outlining(img):
    #kernel size
    kernel_size=3
    #-------------------------------------------------
    #bilateral filter, sharpen, thresh image
    biblur=cv2.bilateralFilter(img,20,175,175)
    sharp=cv2.addWeighted(img,1.55,biblur,-0.5,0)
    ret1,thresh1 = cv2.threshold(sharp,127,255,cv2.THRESH_OTSU)

    #negative and closed image
    inv=cv2.bitwise_not(thresh1)
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kernel_size, kernel_size))
    closed = cv2.morphologyEx(inv, cv2.MORPH_CLOSE, kernel)
    return closed
项目:opencv-helpers    作者:abarrak    | 项目源码 | 文件源码
def otsu_threshold(image, above_thresh_assigned=255, thresh_style=cv.THRESH_BINARY_INV):
  ''' apply otsu's binarization algorithm to find optimal threshold value. '''
  ret, thresholded = cv.threshold(image, 0, above_thresh_assigned, thresh_style  + cv.THRESH_OTSU)
  return { 'otsu_thresh': ret, 'image': thresholded }
项目:ImageSteganography    作者:AhmedAtef07    | 项目源码 | 文件源码
def convert_to_binary_image(img_path, preview):
    img = cv2.imread(img_path)
    if preview: _preview_image("Original Message Image", img, keep_open=True)

    img_gray = cv2.imread(img_path, cv2.CV_LOAD_IMAGE_GRAYSCALE)
    if preview: _preview_image("Gray Scale Message Image", img_gray, keep_open=True)

    (thresh, img_bw) = cv2.threshold(img_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    if preview:  _preview_image("Black & White Message Image", img_bw)

    return img_bw
项目:pynephoscope    作者:neXyon    | 项目源码 | 文件源码
def detect(self, image, mask = None):
        floatimage = np.float32(image)

        fb,fg,fr = cv2.split(floatimage)

        # red-to-blue channel operation
        ra = fr + fb
        rb = fr - fb
        rb[ra > 0] /= ra[ra > 0]
        #mi = np.min(rb)
        #ma = np.max(rb)
        #rb = np.uint8((rb - mi) / (ma - mi) * 255)

        # morphology open
        if self.kernel is None or self.kernel.shape[0] != Configuration.background_rect_size:
            self.kernel = np.ones((Configuration.background_rect_size, Configuration.background_rect_size), np.uint8) * 255

        result = cv2.morphologyEx(rb, cv2.MORPH_OPEN, self.kernel)

        # background subtraction
        # homogeneous background image V
        result = rb - result

        mi = np.min(result)
        ma = np.max(result)
        result = np.uint8((result - mi) / (ma - mi) * 255)

        # adaptive threshold T
        T, _ = cv2.threshold(result[mask == 0], 0, 1, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

        # V(i, j) > T
        return np.uint8((T - np.float32(result)) <= 0)
项目:image_text_reader    作者:yardstick17    | 项目源码 | 文件源码
def image_smoothening(img):
    ret1, th1 = cv2.threshold(img, BINARY_THREHOLD, 255, cv2.THRESH_BINARY)
    ret2, th2 = cv2.threshold(th1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    blur = cv2.GaussianBlur(th2, (1, 1), 0)
    ret3, th3 = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    return th3
项目:Simple-deCAPTCHA    作者:BLKStone    | 项目源码 | 文件源码
def th1(self,img):
        # ????
        img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        ret, thresh = cv2.threshold(img_gray, 0, 255, 
            cv2.THRESH_BINARY + cv2.THRESH_OTSU)
        return thresh

    # ????????2 ?????
项目:captcha-python-test    作者:hanc00l    | 项目源码 | 文件源码
def clear_noise(image):
    clear_horizotal_line_noise(image)
    clear_color(image)

    image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    clear_pix_noise(image)
    ret, image = cv2.threshold(image,127,255,cv2.THRESH_BINARY)#+cv2.THRESH_OTSU)

    return image
项目:CVMazeRunner    作者:M-Niedoba    | 项目源码 | 文件源码
def getOtsuBinarizedImage(self,isInverted):
        image = self.blurImage()
        threshType = cv2.THRESH_BINARY_INV if isInverted else cv2.THRESH_BINARY
        retval, threshed = cv2.threshold(image,0,255,threshType + cv2.THRESH_OTSU)
        return threshed
项目:virtual-dressing-room    作者:akash0x53    | 项目源码 | 文件源码
def detect_shirt2(self):
        self.hsv=cv2.cvtColor(self.norm_rgb,cv.CV_BGR2HSV)
        self.hue,s,_=cv2.split(self.hsv)

        _,self.dst=cv2.threshold(self.hue,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
        self.fg=cv2.erode(self.dst,None,iterations=3)
        self.bg=cv2.dilate(self.dst,None,iterations=1)
        _,self.bg=cv2.threshold(self.bg,1,128,1)
        mark=cv2.add(self.fg,self.bg)
        mark32=np.int32(mark)
        cv2.watershed(self.norm_rgb,mark32)

        m=cv2.convertScaleAbs(mark32)
        _,m=cv2.threshold(m,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

        cntr,h=cv2.findContours(m,cv.CV_RETR_EXTERNAL,cv.CV_CHAIN_APPROX_SIMPLE)
        print len(cntr)
        #print cntr[0].shape
        #cntr[1].dtype=np.float32
        #ret=cv2.contourArea(np.array(cntr[1]))
        #print ret
        #cntr[0].dtype=np.uint8
        cv2.drawContours(m,cntr,-1,(255,255,255),3)
        cv2.imshow("mask_fg",self.fg)
        cv2.imshow("mask_bg",self.bg)
        cv2.imshow("mark",m)
项目:Automatic-Plate-Number-Recognition-APNR    作者:kagan94    | 项目源码 | 文件源码
def find_contours(img):
    '''
    :param img: (numpy array)
    :return: all possible rectangles (contours)
    '''
    img_blurred = cv2.GaussianBlur(img, (5, 5), 1)  # remove noise
    img_gray = cv2.cvtColor(img_blurred, cv2.COLOR_BGR2GRAY)  # greyscale image
    # cv2.imshow('', img_gray)
    # cv2.waitKey(0)

    # Apply Sobel filter to find the vertical edges
    # Find vertical lines. Car plates have high density of vertical lines
    img_sobel_x = cv2.Sobel(img_gray, cv2.CV_8UC1, dx=1, dy=0, ksize=3, scale=1, delta=0, borderType=cv2.BORDER_DEFAULT)
    # cv2.imshow('img_sobel', img_sobel_x)

    # Apply optimal threshold by using Oslu algorithm
    retval, img_threshold = cv2.threshold(img_sobel_x, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)
    # cv2.imshow('s', img_threshold)
    # cv2.waitKey(0)

    # TODO: Try to apply AdaptiveThresh
    # Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on.
    # gaus_threshold = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 115, 1)
    # cv2.imshow('or', img)
    # cv2.imshow('gaus', gaus_threshold)
    # cv2.waitKey(0)

    # Define a stuctural element as rectangular of size 17x3 (we'll use it during the morphological cleaning)
    element = cv2.getStructuringElement(shape=cv2.MORPH_RECT, ksize=(17, 3))

    # And use this structural element in a close morphological operation
    morph_img_threshold = deepcopy(img_threshold)
    cv2.morphologyEx(src=img_threshold, op=cv2.MORPH_CLOSE, kernel=element, dst=morph_img_threshold)
    # cv2.dilate(img_threshold, kernel=np.ones((1,1), np.uint8), dst=img_threshold, iterations=1)
    # cv2.imshow('Normal Threshold', img_threshold)
    # cv2.imshow('Morphological Threshold based on rect. mask', morph_img_threshold)
    # cv2.waitKey(0)

    # Find contours that contain possible plates (in hierarchical relationship)
    contours, hierarchy = cv2.findContours(morph_img_threshold,
                                           mode=cv2.RETR_EXTERNAL,  # retrieve the external contours
                                           method=cv2.CHAIN_APPROX_NONE)  # all pixels of each contour

    plot_intermediate_steps = False
    if plot_intermediate_steps:
        plot(plt, 321, img, "Original image")
        plot(plt, 322, img_blurred, "Blurred image")
        plot(plt, 323, img_gray, "Grayscale image", cmap='gray')
        plot(plt, 324, img_sobel_x, "Sobel")
        plot(plt, 325, img_threshold, "Threshold image")
        # plot(plt, 326, morph_img_threshold, "After Morphological filter")
        plt.tight_layout()
        plt.show()

    return contours
项目:BAR4Py    作者:bxtkezhan    | 项目源码 | 文件源码
def recognize(self, points, frame, dictionary=None, limit=0.80, side_length=28, batch_size=3):
        '''
        Inputs:
        points is marker.points param
        frame is image frame
        dictionary is Dictionary object
        ...

        Outputs:
        marker_id, rotations

        >>> marker_i, rotations = detector.recognize(points, frame, dictionary=dictionary)
        '''
        dictionary = dictionary or self.dictionary
        if dictionary is None: raise TypeError('recognize nead dictionary')

        # To Gray
        gray = frame
        if len(gray.shape) == 3: gray = bgr2gray(frame)

        # Convert the points, gray to local_points, local_gray
        rect = self.localRect(points)
        gray = self.localFrame(rect, gray)
        points = self.localCorners(rect, points)

        # Define src_points and dst_points, src: 0,1,2,3 -> dst: 1,0,3,2
        points_src = np.float32(points)
        points_dst = np.float32([[0,side_length],[0,0], 
                                 [side_length,0],[side_length,side_length]])


        # Calc transform matrix and perspective dst map
        M = cv2.getPerspectiveTransform(points_src, points_dst)
        dst = cv2.warpPerspective(gray, M, (side_length, side_length))

        # Begin recognize
        _, dst = cv2.threshold(dst, dst.mean(), 1, cv2.THRESH_OTSU)
        # Probables
        probables = []
        marker_dict = dictionary.getDict()
        # for marker_id, hash_map in dictionary.getDict():
        for marker_id in marker_dict:
            hash_map = marker_dict[marker_id]
            if dst.shape != hash_map.shape: hash_map = cv2.resize(hash_map, dst.shape[::-1])
            deviation = rotations = 0
            for i in range(4):
                now_deviation = np.sum((dst == hash_map).astype(int)) / (side_length**2)
                if now_deviation > deviation: deviation, rotations = now_deviation, i
                hash_map = np.rot90(hash_map)
            if deviation > limit:
                probables.append((deviation, marker_id, rotations))
                if len(probables) > batch_size: break
        # Best of marker_id and rotations
        if len(probables) > 0:
            return max(probables, key=lambda item:item[0])[1:]
项目:BAR4Py    作者:bxtkezhan    | 项目源码 | 文件源码
def recognize(self, points, frame, dictionary=None, limit=0.80, side_length=28, batch_size=3):
        '''
        Inputs:
        points is marker.points param
        frame is image frame
        dictionary is Dictionary object
        ...

        Outputs:
        marker_id, rotations

        >>> marker_i, rotations = detector.recognize(points, frame, dictionary=dictionary)
        '''
        dictionary = dictionary or self.dictionary
        if dictionary is None: raise TypeError('recognize nead dictionary')

        # To Gray
        gray = frame
        if len(gray.shape) == 3: gray = bgr2gray(frame)

        # Convert the points, gray to local_points, local_gray
        rect = self.localRect(points)
        gray = self.localFrame(rect, gray)
        points = self.localCorners(rect, points)

        # Define src_points and dst_points, src: 0,1,2,3 -> dst: 1,0,3,2
        points_src = np.float32(points)
        points_dst = np.float32([[0,side_length],[0,0], 
                                 [side_length,0],[side_length,side_length]])


        # Calc transform matrix and perspective dst map
        M = cv2.getPerspectiveTransform(points_src, points_dst)
        dst = cv2.warpPerspective(gray, M, (side_length, side_length))

        # Begin recognize
        _, dst = cv2.threshold(dst, dst.mean(), 1, cv2.THRESH_OTSU)
        # Probables
        probables = []
        marker_dict = dictionary.getDict()
        # for marker_id, hash_map in dictionary.getDict():
        for marker_id in marker_dict:
            hash_map = marker_dict[marker_id]
            if dst.shape != hash_map.shape: hash_map = cv2.resize(hash_map, dst.shape[::-1])
            deviation = rotations = 0
            for i in range(4):
                now_deviation = np.sum((dst == hash_map).astype(int)) / (side_length**2)
                if now_deviation > deviation: deviation, rotations = now_deviation, i
                hash_map = np.rot90(hash_map)
            if deviation > limit:
                probables.append((deviation, marker_id, rotations))
                if len(probables) > batch_size: break
        # Best of marker_id and rotations
        if len(probables) > 0:
            return max(probables, key=lambda item:item[0])[1:]
项目:BAR4Py    作者:bxtkezhan    | 项目源码 | 文件源码
def recognize(self, points, frame, dictionary=None, limit=0.80, side_length=28, batch_size=3):
        '''
        Inputs:
        points is marker.points param
        frame is image frame
        dictionary is Dictionary object
        ...

        Outputs:
        marker_id, rotations

        >>> marker_i, rotations = detector.recognize(points, frame, dictionary=dictionary)
        '''
        dictionary = dictionary or self.dictionary
        if dictionary is None: raise TypeError('recognize nead dictionary')

        # To Gray
        gray = frame
        if len(gray.shape) == 3: gray = bgr2gray(frame)

        # Convert the points, gray to local_points, local_gray
        rect = self.localRect(points)
        gray = self.localFrame(rect, gray)
        points = self.localCorners(rect, points)

        # Define src_points and dst_points, src: 0,1,2,3 -> dst: 1,0,3,2
        points_src = np.float32(points)
        points_dst = np.float32([[0,side_length],[0,0], 
                                 [side_length,0],[side_length,side_length]])


        # Calc transform matrix and perspective dst map
        M = cv2.getPerspectiveTransform(points_src, points_dst)
        dst = cv2.warpPerspective(gray, M, (side_length, side_length))

        # Begin recognize
        _, dst = cv2.threshold(dst, dst.mean(), 1, cv2.THRESH_OTSU)
        # Probables
        probables = []
        marker_dict = dictionary.getDict()
        # for marker_id, hash_map in dictionary.getDict():
        for marker_id in marker_dict:
            hash_map = marker_dict[marker_id]
            if dst.shape != hash_map.shape: hash_map = cv2.resize(hash_map, dst.shape[::-1])
            deviation = rotations = 0
            for i in range(4):
                now_deviation = np.sum((dst == hash_map).astype(int)) / (side_length**2)
                if now_deviation > deviation: deviation, rotations = now_deviation, i
                hash_map = np.rot90(hash_map)
            if deviation > limit:
                probables.append((deviation, marker_id, rotations))
                if len(probables) > batch_size: break
        # Best of marker_id and rotations
        if len(probables) > 0:
            return max(probables, key=lambda item:item[0])[1:]
项目:BAR4Py    作者:bxtkezhan    | 项目源码 | 文件源码
def recognize(self, points, frame, dictionary=None, limit=0.80, side_length=28, batch_size=3):
        '''
        Inputs:
        points is marker.points param
        frame is image frame
        dictionary is Dictionary object
        ...

        Outputs:
        marker_id, rotations

        >>> marker_i, rotations = detector.recognize(points, frame, dictionary=dictionary)
        '''
        dictionary = dictionary or self.dictionary
        if dictionary is None: raise TypeError('recognize nead dictionary')

        # To Gray
        gray = frame
        if len(gray.shape) == 3: gray = bgr2gray(frame)

        # Convert the points, gray to local_points, local_gray
        rect = self.localRect(points)
        gray = self.localFrame(rect, gray)
        points = self.localCorners(rect, points)

        # Define src_points and dst_points, src: 0,1,2,3 -> dst: 1,0,3,2
        points_src = np.float32(points)
        points_dst = np.float32([[0,side_length],[0,0], 
                                 [side_length,0],[side_length,side_length]])


        # Calc transform matrix and perspective dst map
        M = cv2.getPerspectiveTransform(points_src, points_dst)
        dst = cv2.warpPerspective(gray, M, (side_length, side_length))

        # Begin recognize
        _, dst = cv2.threshold(dst, dst.mean(), 1, cv2.THRESH_OTSU)
        # Probables
        probables = []
        marker_dict = dictionary.getDict()
        # for marker_id, hash_map in dictionary.getDict():
        for marker_id in marker_dict:
            hash_map = marker_dict[marker_id]
            if dst.shape != hash_map.shape: hash_map = cv2.resize(hash_map, dst.shape[::-1])
            deviation = rotations = 0
            for i in range(4):
                now_deviation = np.sum((dst == hash_map).astype(int)) / (side_length**2)
                if now_deviation > deviation: deviation, rotations = now_deviation, i
                hash_map = np.rot90(hash_map)
            if deviation > limit:
                probables.append((deviation, marker_id, rotations))
                if len(probables) > batch_size: break
        # Best of marker_id and rotations
        if len(probables) > 0:
            return max(probables, key=lambda item:item[0])[1:]
项目:OpenCV2    作者:SarathM1    | 项目源码 | 文件源码
def checkButton(self, img, x1, y1, x2, y2):
        btn1 = img[y1:y2, x1:x2]
        btn1 = cv2.cvtColor(btn1, cv2.COLOR_BGR2GRAY)

        if self.thresh_change_trigger:
            ret, mask = cv2.threshold(btn1, 0, 255, cv2.THRESH_BINARY_INV +
                                      cv2.THRESH_OTSU)
            self.thresh_val.setText(str(ret))
            self.THRESH = ret
        else:
            ret, mask = cv2.threshold(btn1, self.THRESH, 255,
                                      cv2.THRESH_BINARY_INV)
        try:
            (_, cnts, _) = cv2.findContours(mask, cv2.RETR_EXTERNAL,
                                            cv2.CHAIN_APPROX_SIMPLE)
        except Exception, e:
            (cnts, _) = cv2.findContours(mask, cv2.RETR_EXTERNAL,
                                            cv2.CHAIN_APPROX_SIMPLE)

        ci = 0
        max_area = 0

        if cnts:
            for i in range(len(cnts)):
                cnt = cnts[i]
                area = cv2.contourArea(cnt)
                if(area > max_area):
                    max_area = area
                    ci = i
            cnt = cnts[ci]

        else:
            cnt = None

        self.flags.isSet_prev = self.flags.isSet_cur
        if cnt is not None:
            cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 0), 1)
            hull = cv2.convexHull(cnt)
            cv2.drawContours(btn1, [hull], 0, (0, 0, 255), 1)
            self.flags.isSet_cur = True
        else:
            cv2.rectangle(img, (x1, y1), (x2, y2), (188, 188, 137), 1)
            self.flags.isSet_cur = False
        return img
项目:baxter    作者:destrygomorphous    | 项目源码 | 文件源码
def find_black_center(cv_img, msk):

    """
    Given an opencv image containing a dark object on a light background
    and a mask of objects to ignore (a gripper, for instance),
    return the coordinates of the centroid of the largest object
    (excluding those touching edges) and its simplified contour.
    If none detected or problem with centroid, return [(-1, -1), False].
    """

    # Convert to black and white
    (rows, cols, _) = cv_img.shape
    grey_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2GRAY)
    grey_img = cv2.bilateralFilter(grey_img, 11, 17, 17)
    _, outlines = cv2.threshold(
        grey_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

    # Subtract gripper
    msk_out = cv2.subtract(cv2.bitwise_not(outlines), msk)

    # Remove objects touching edges
    flood_fill_edges(msk_out, 30)

    # Find contours
    _, contours, _ = cv2.findContours(
        msk_out, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    if len(contours) == 0:
        return [(-1, -1), False]

    # Find largest contour
    max_area = 0
    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area > max_area:
            contour = cnt
            max_area = area

    # Approximate contour
    epsilon = 0.025 * cv2.arcLength(contour, True)
    approx = cv2.approxPolyDP(contour, epsilon, True)

    # Find centroid
    try:
        M = cv2.moments(approx)
        cx = int(M['m10']/M['m00'])
        cy = int(M['m01']/M['m00'])
        return [(cx, cy), approx]
    except ZeroDivisionError:
        return [(-1, -1), False]
项目:CNNGestureRecognizer    作者:asingh33    | 项目源码 | 文件源码
def binaryMask(frame, x0, y0, width, height ):
    global guessGesture, visualize, mod, lastgesture, saveImg

    cv2.rectangle(frame, (x0,y0),(x0+width,y0+height),(0,255,0),1)
    roi = frame[y0:y0+height, x0:x0+width]

    gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray,(5,5),2)
    #blur = cv2.bilateralFilter(roi,9,75,75)

    th3 = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,11,2)
    ret, res = cv2.threshold(th3, minValue, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
    #ret, res = cv2.threshold(blur, minValue, 255, cv2.THRESH_BINARY +cv2.THRESH_OTSU)

    if saveImg == True:
        saveROIImg(res)
    elif guessGesture == True:
        retgesture = myNN.guessGesture(mod, res)
        if lastgesture != retgesture :
            lastgesture = retgesture
            #print lastgesture

            ## Checking for only PUNCH gesture here
            ## Run this app in Prediction Mode and keep Chrome browser on focus with Internet Off
            ## And have fun :) with Dino
            if lastgesture == 3:
                jump = ''' osascript -e 'tell application "System Events" to key code 49' '''
                #jump = ''' osascript -e 'tell application "System Events" to key down (49)' '''
                os.system(jump)
                print myNN.output[lastgesture] + "= Dino JUMP!"

            #time.sleep(0.01 )
            #guessGesture = False
    elif visualize == True:
        layer = int(raw_input("Enter which layer to visualize "))
        cv2.waitKey(1)
        myNN.visualizeLayers(mod, res, layer)
        visualize = False

    return res

#%%