-1

我的情况很cv2.THRESH_TRUNC适合我,我想申请为自适应阈值,但我面临一个未知错误。这是我的基本代码:

from PIL import Image
import pytesseract
import cv2

image = cv2.imread("/home/anees/Desktop/passport.png",0)
thresh =cv2.adaptiveThreshold(image,170,
cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_TRUNC, 3, 5)

我得到的错误如下:

openCV(4.5.2) /tmp/pip-req-build-yw7uvgqm/opencv/modules/imgproc/src/thresh.cpp:1723: error: (-206:Bad flag (parameter or structure field)) Unknown/unsupported threshold type in function 'adaptiveThreshold'
4

1 回答 1

0

这是该cv2.adaptiveThreshold()方法的文档,可通过调用内置help()方法访问:

>>> import cv2
>>> help(cv2.adaptiveThreshold)
Help on built-in function adaptiveThreshold:

adaptiveThreshold(...)
    adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst]) -> dst
    .   @brief Applies an adaptive threshold to an array.
    .   
    .   The function transforms a grayscale image to a binary image according to the formulae:
    .   -   **THRESH_BINARY**
    .       \f[dst(x,y) =  \fork{\texttt{maxValue}}{if \(src(x,y) > T(x,y)\)}{0}{otherwise}\f]
    .   -   **THRESH_BINARY_INV**
    .       \f[dst(x,y) =  \fork{0}{if \(src(x,y) > T(x,y)\)}{\texttt{maxValue}}{otherwise}\f]
    .   where \f$T(x,y)\f$ is a threshold calculated individually for each pixel (see adaptiveMethod parameter).
    .   
    .   The function can process the image in-place.
    .   
    .   @param src Source 8-bit single-channel image.
    .   @param dst Destination image of the same size and the same type as src.
    .   @param maxValue Non-zero value assigned to the pixels for which the condition is satisfied
    .   @param adaptiveMethod Adaptive thresholding algorithm to use, see #AdaptiveThresholdTypes.
    .   The #BORDER_REPLICATE | #BORDER_ISOLATED is used to process boundaries.
    .   @param thresholdType Thresholding type that must be either #THRESH_BINARY or #THRESH_BINARY_INV,
    .   see #ThresholdTypes.
    .   @param blockSize Size of a pixel neighborhood that is used to calculate a threshold value for the
    .   pixel: 3, 5, 7, and so on.
    .   @param C Constant subtracted from the mean or weighted mean (see the details below). Normally, it
    .   is positive but may be zero or negative as well.
    .   
    .   @sa  threshold, blur, GaussianBlur

>>> 

重点介绍这部分:

@param thresholdType Thresholding type that must be either #THRESH_BINARY or #THRESH_BINARY_INV

因此,您只需将您的更改cv2.THRESH_TRUNCcv2.THRESH_BINARYor之一cv2.THRESH_BINARY_INV

help()方法是无需上网即可获得有关方法的更多信息的好工具!

于 2021-06-01T12:31:44.470 回答