我一直在尝试将inRange函数应用于我的图像。在没有InRange的情况下,向HSV的转换效果很好,但是当我尝试应用它时,可以得到所需的颜色(在这种情况下为紫色)。我遇到了错误。
我正在使用OpenCV 4.0.3
Mat plate_img = imread(imagePath, IMREAD_COLOR);
Mat plate_gradient = new Mat();
GaussianBlur(plate_img, plate_img,new Size(7,7),0);
Mat boosted_plate_gradient = new Mat(plate_img.rows(), plate_img.cols(), CvType.CV_8U, new Scalar(3));
morphologyEx(plate_img, plate_gradient, MORPH_GRADIENT, new Mat());
plate_gradient.convertTo(plate_gradient, -1, 9.5, -70);
cvtColor(plate_gradient,plate_gradient, COLOR_BGR2HSV);
Scalar low = new Scalar(151,48.6,85.5);
Scalar high = new Scalar(150,100,50.2);
Core.inRange(plate_gradient, low, high, boosted_plate_gradient);
E/cv::error(): OpenCV(4.0.1) Error: Unspecified error (> Invalid number of channels in input image:
> 'VScn::contains(scn)'
> where
> 'scn' is 1
) in cv::CvtHelper<VScn, VDcn, VDepth, sizePolicy>::CvtHelper(cv::InputArray, cv::OutputArray, int) [with VScn = cv::Set<3, 4>; VDcn = cv::Set<1>; VDepth = cv::Set<0, 2, 5>; cv::SizePolicy sizePolicy = (cv::SizePolicy)2u; cv::InputArray = const cv::_InputArray&; cv::OutputArray = const cv::_OutputArray&], file ...\opencv\sources\modules\imgproc\src\color.hpp, line 259
E/org.opencv.imgproc: imgproc::cvtColor_11() caught cv::Exception: OpenCV(4.0.1) ...\opencv\sources\modules\imgproc\src\color.hpp:259: error: (-2:Unspecified error) in function 'cv::CvtHelper<VScn, VDcn, VDepth, sizePolicy>::CvtHelper(cv::InputArray, cv::OutputArray, int) [with VScn = cv::Set<3, 4>; VDcn = cv::Set<1>; VDepth = cv::Set<0, 2, 5>; cv::SizePolicy sizePolicy = (cv::SizePolicy)2u; cv::InputArray = const cv::_InputArray&; cv::OutputArray = const cv::_OutputArray&]'
> Invalid number of channels in input image:
> 'VScn::contains(scn)'
> where
> 'scn' is 1
最佳答案
显然,我的另一个cvtcolor(在BGR2HSV之后)是喷出错误的,因为我正在尝试从BGR2GRAY转换它,但是当我未实现inRange函数时它确实起作用了