python - 为什么自适应阈值图像比原始图像小?

标签 python opencv gaussian threshold

我正在尝试在实时流上使用 adapativeThreshold,最终将用于形状检测。常规阈值不足以显示我想看到的内容。当我使用下面的代码时,常规阈值按预期显示,但由于某种原因,自适应阈值比原始阈值要薄得多,我在 View 中看不到任何内容。看起来好像有什么事情正在发生,但我又说不出来是什么。关于如何使自适应阈值窗口全尺寸有什么想法吗?

这是我在每个窗口中运行程序时看到的内容: enter image description here

#import packages
from documentscanner.pyimagesearch.transform import four_point_transform
from pyimagesearch.shapedetector import ShapeDetector
from skimage.filters import threshold_local
import numpy as np
import cv2
import imutils


def draw_Contours(screen, points):
    cv2.drawContours(screen, [points], -1, (0, 255, 0), 2)
    cv2.imshow("Outline", screen)


def nothing(x):
    #any operation
    pass

#access video camera
cap = cv2.VideoCapture(0)

cv2.namedWindow('Trackbars')
cv2.createTrackbar('min_edge', 'Trackbars', 75, 100, nothing)
cv2.createTrackbar('max_edge', 'Trackbars', 110,300, nothing)

while True:
    _, frame = cap.read()       #read video camera data

    minedge = cv2.getTrackbarPos('min_edge', 'Trackbars')
    maxedge = cv2.getTrackbarPos('max_edge', 'Trackbars')

    #convert image to gray scale
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (5, 5), 0)
    #blur = cv2.GaussianBlur(frame, (5, 5), 0)
    #edged = cv2.Canny(gray, minedge, maxedge)

    #threshhold instead of edging
    thresh = cv2.threshold(gray, 60, 255, cv2.THRESH_BINARY)[1]
    thresh2 = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
                                    cv2.THRESH_BINARY, 11, 2)[1]
    thresh3 = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,\
                                    cv2.THRESH_BINARY, 11, 2)[1]

    #find contours in edges image, keeping the largest ones, and initialize the screen contour/shapedetect
    cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    sd = ShapeDetector()
    cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]

    #loop over contours
    for c in cnts:
        #approximate the contour points
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02*peri, True)

        #check points in contour
        if len(approx) == 4:
            print("rectangle found: ")
            print(approx)
            draw_Contours(frame, approx)



        if len(approx) == 3:
            print("triangle found: ")
            print(approx)
            draw_Contours(frame, approx)

        if len(approx) == 2:
            print("line found: ")
            print(approx)
            draw_Contours(frame, approx)

        #show the countour(outline) of the shapes


    #show original frame and gray frame
    cv2.imshow('Frame', frame)
    #cv2.imshow('Copy', gray)
    #cv2.imshow('Edged', edged)
    cv2.imshow('Threshold', thresh)
    cv2.imshow('ThresholdGaussian', thresh2)
    cv2.imshow('ThresholdMean', thresh3)

    #detect key press and exit with escape key
    key = cv2.waitKey(1)
    if key == 27:
        break

#close the program
cap.release()
cv2.destroyAllWindows()

最佳答案

而不是使用

thresh2 = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
                                cv2.THRESH_BINARY, 11, 2)[1]
thresh3 = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,\
                                cv2.THRESH_BINARY, 11, 2)[1]

在没有 numpy 索引的情况下使用它,就不会出现此错误。

thresh2 = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
                                cv2.THRESH_BINARY, 11, 2) # don't use [1] 
thresh3 = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,\
                                cv2.THRESH_BINARY, 11, 2)

这是因为正常阈值返回两个值,而自适应阈值仅返回单个值。

关于python - 为什么自适应阈值图像比原始图像小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60175194/

相关文章:

python - Pandas .agg 中的过滤计数

python - 我应该如何将 blob 从 BlobStore 移动到 Google Cloud Storage?

android - 使用 opencv 进行图像分类

python - 导入 opencv 与导入 cv2

c++ - 不规则形状的 OpenCV 直方图

syntax-error - 尝试在Scilab中做高斯钟声

Python:我可以在字典中将字符串作为键并将函数作为值吗?

ios - 图像的高斯混合

python-2.7 - 高斯NB :- ValueError: The sum of the priors should be 1

python - 如果 Pandas 中没有重复项,则将行值从一个 df append 到另一个