OpenCV计算机视觉处理代码不起作用
Posted
技术标签:
【中文标题】OpenCV计算机视觉处理代码不起作用【英文标题】:OpenCV computer vision processing code not working 【发布时间】:2017-05-04 14:00:51 【问题描述】:我已经编写了一种方法,可以在 openCV 中为我正在为比赛建造的机器人进行一些视觉处理。 frame 和 process 对象是 mat 对象和实例字段,它们是通过另一种我知道的方法从 USB 相机流中获取的。当我使用另一种方法显示框架对象时,如果我运行了 process 方法,则不会显示框架,但如果我没有运行它就会显示。框架垫包含未处理的图像,我在其上绘制标记和轮廓,并且处理垫用于在将标记和轮廓绘制到框架上之前存储处理后的图像。 [编辑]:我发现它是由 cvtColor 方法引起的。这是打印错误: (-215) (scn == 3 || scn == 4) && (深度 == CV_8U || 深度 == CV_32F)。
//blurs the image to remove false positives
Imgproc.GaussianBlur(frame, processed, new Size(17, 17), 3);
//we are going to use HSV, not BGR for better filtration
Imgproc.cvtColor(processed, processed, Imgproc.COLOR_BGR2HSV);
//create scalars to hold high and low thresholds if using BGR
/*Scalar lowRange = new Scalar(RobotMap.lowBlueValue, RobotMap.lowGreenValue, RobotMap.lowRedValue);
Scalar highRange = new Scalar(RobotMap.highBlueValue, RobotMap.highGreenValue, RobotMap.highRedValue);*/
//create scalars if using HSV
Scalar lowRange = new Scalar(RobotMap.lowHue, RobotMap.lowSat, RobotMap.lowVal);
Scalar highRange = new Scalar(RobotMap.highHue, RobotMap.highSat, RobotMap.highVal);
//removes everything not in our filter range
Core.inRange(processed, lowRange, highRange, processed);
//mat used to for some of the contour finding
//TODO determine if necessary
Mat hierarchy = new Mat();
//create an arraylist to hold the unfiltered contours
ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
//find the contours in our image
findContours(processed, contours, hierarchy, RETR_LIST, CHAIN_APPROX_NONE);
//list of filtered contours
ArrayList<MatOfPoint> filteredContours = new ArrayList<MatOfPoint>();
//list of filtered contours as rect objects
ArrayList<Rect> rects = new ArrayList<Rect>();
//put our contours into rectangle objects if they pass our conditions
for (MatOfPoint contour : contours)
//bounding rect objects are rectangles whose bounderies encompass all of the contour
Rect boundingRect = boundingRect(contour);
//check to see if we are a tallish rectangle with a largish area
if (boundingRect.height > boundingRect.width && boundingRect.area() > RobotMap.minimumArea)
filteredContours.add(contour);
rects.add(boundingRect);
//draw our contours
drawContours(frame, filteredContours, -1, new Scalar(0, 0xFF, 0), FILLED);
//figure out how many targets there are
numTargets = filteredContours.size();
//draw marker at center of all rects
if(rects.size() > 0)
Imgproc.drawMarker(frame, center(rects), new Scalar(0xFF, 0, 0xFF));
//draw markers to show info on each rect
for (Rect rect : rects)
Imgproc.drawMarker(frame, center(rect), new Scalar(0, 0, 0xFF));
Imgproc.drawMarker(frame, rect.br(), new Scalar(0xFF, 0, 0));
Imgproc.drawMarker(frame, rect.tl(), new Scalar(0xFF, 0, 0));
if(numTargets > 0)
center = center(rects).x;
【问题讨论】:
【参考方案1】:当源或目标 Mat 的格式不适合给定的转换时,会出现此错误。根据错误,该函数正在寻找一个Uint8或Float32的数组。
“就地”使用 cvtColor 有时会产生问题。我建议为 HSV 图像创建一个新的 Mat。
您也可以查看this example进行颜色转换。
【讨论】:
谢谢。我发现了错误。事实证明,在我正在从中抓取帧的自定义相机服务器的启动期间,该帧还无法被抓取。我在开始时添加了一个小检查,如果框架为空,则返回。由于这是我的第一篇文章,如何将其标记为已解决?以上是关于OpenCV计算机视觉处理代码不起作用的主要内容,如果未能解决你的问题,请参考以下文章