Opencv Error:
ret2, th2 = cv.threshold(img, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-c2l3r8zm\opencv\modules\imgproc\src\thresh.cpp:1557: error: (-2:Unspecified error) in function ‘double __cdecl cv::threshold(const class cv::_InputArray &,const class cv::_OutputArray &,double,double,int)’
THRESH_OTSU mode:
‘src_type == CV_8UC1 || src_type == CV_16UC1’
where
‘src_type’ is 16 (CV_8UC3)
Original Codes:
img = cv.imread('noisy.jpg')
# Fixed threshold method
ret1, th1 = cv.threshold(img, 105, 255, cv.THRESH_BINARY)
# Otsu threshold method
ret2, th2 = cv.threshold(img, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
# Perform Gaussian filtering first, and then use Otsu threshold method
blur = cv.GaussianBlur(img, (5, 5), 0)
ret3, th3 = cv.threshold(blur, 105, 255, cv.THRESH_BINARY)
ret4, th4 = cv.threshold(blur, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
Problem analysis
The CV2.Threshold() function inputs graphics in the form of single channel, while the image in the above code is input in the form of color three channels, resulting in an error.
Solution:
Convert the color image into a single channel gray image for input.
img = cv.imread('noisy.jpg',0)
Rerun the program
Run successfully
Read More:
- How to Solve OpenCV CVUI Error: LINK2019
- [Solved] Cmake compile opencv open-source project Error: but it set OpenCV_FOUND to FALSE so package “OpenCV” is considered to be…
- [Solved] Unity Package Error: Failed running D:\Program Files\Unity2018.4.1\Editor\Data\il2cpp/build/il2cpp.exe
- OpenCV4 Error: imread(argv[1], CV_LOAD_IMAGE_COLOR);
- [Solved] Opencv3. X fatal error: opencv2/nonfree/nonfree.hpp: there is no such file or directory
- [Solved] Opencv Calls PB Model Error: cv2.error: OpenCV(4.2.0)…
- [Solved] GDAL+OPENCV often reports errors when processing remote sensing impacts
- [Solved] OpenCV error: #error “This header with legacy C API declarations has been removed from OpenCV.
- Opencv Can not Find opencv2/gpu/gpu.hpp [How to Solve]
- How to Solve Vector Variable Error in sub thread
- How to Solve Docker failed to initialize Error
- How to Solve Node GYP Rebuild Error
- Android 10: How to Solve Systemui Error
- R Language: How to Solve featureplot function Error
- How to Solve Error: Type mismatch: cannot convert from Object to Car
- VScode: How to Solve golong plug-in install Error
- How to Solve bootstrap.sh error during the construction of fabric environment
- How to Solve Huawei ensp40 Error
- How to Solve Error: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
- How to Solve Jetpack room use error