【问题标题】:Why is adaptive threshold image smaller than the original?为什么自适应阈值图像小于原始图像?
【发布时间】:2020-05-27 05:38:17
【问题描述】:

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

这是我在每个窗口中运行程序时看到的:

#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()

【问题讨论】:

  • 您需要发布您的原始图像并说明您想从中提取什么。然后发布简单的阈值图像,显示您得到的和想要的以及自适应结果。可能是自适应阈值不适合您的输入图像。
  • 看看shape detection。可以使用Otsu's threshold自动计算阈值
  • 没有原图。这是摄像机的实时流。

标签: python opencv gaussian threshold


【解决方案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]

在没有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)

这是因为正常阈值处理返回两个值,而自适应阈值处理只返回一个值。

【讨论】:

  • 很高兴听到这个消息。
猜你喜欢
  • 1970-01-01
  • 2012-11-03
  • 2016-02-03
  • 1970-01-01
  • 1970-01-01
  • 2013-09-08
  • 1970-01-01
  • 2017-09-28
  • 2017-02-15
相关资源
最近更新 更多