(-215:Assertion failed) src.type() == CV_8UC1 in function 'cv::threshold'
时间: 2023-11-05 20:00:51 浏览: 82
error src
This error message is related to OpenCV and it indicates that the input image passed to the threshold function is not of the expected type. The threshold function expects a single-channel (grayscale) image with pixel values of type unsigned 8-bit integer (CV_8UC1).
To fix this error, you should first check that the input image is a grayscale image with a single channel. If the input image is a color image or has more than one channel, you should convert it to grayscale using cvtColor() function.
You should also make sure that the pixel values in the image are of type unsigned 8-bit integer. If the pixel values are not of this type, you can use the convertTo() function to convert the pixel values to the correct type.
Here's an example of how to convert an input image to grayscale and convert the pixel values to unsigned 8-bit integer:
```
cv::Mat input_image = cv::imread("input_image.jpg");
cv::Mat gray_image;
cv::cvtColor(input_image, gray_image, cv::COLOR_BGR2GRAY);
cv::Mat threshold_image;
gray_image.convertTo(threshold_image, CV_8UC1);
cv::threshold(threshold_image, threshold_image, 128, 255, cv::THRESH_BINARY);
```
阅读全文