2017-05-04 45 views
1

我写了一个方法来在openCV中为我正在为竞赛而建的机器人做一些视觉处理。帧和过程对象是mat对象和实例字段,并通过我知道的一个事实工作的另一种方法从usb相机流中获取。当我用另一种方法显示框架对象时,如果我已经运行了处理方法,框架就不会显示,但如果我没有运行,框架就会显示。框架垫包含未处理的图像,通过该图像绘制标记和轮廓,并且在将标记和轮廓绘制到框架上之前,处理垫用于存储处理的图像。 [编辑]:我发现它是由cvtColor方法造成的。这是打印错误: (-215)(scn == 3 || scn == 4)& &(depth == CV_8U || depth == CV_32F)。OpenCV计算机视觉处理代码不起作用

//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; 

回答

0

当源或目标Mat的格式对于给定转换不正确时出现此错误。根据错误,该函数正在寻找一个Uint8或Float32的数组。

使用cvtColor“in-place”有时会产生问题。我建议为HSV图像创建一个新的垫子。

您还可以检查this example进行颜色转换。

+0

谢谢。我发现了这个错误。事实证明,在自定义摄像头服务器的启动期间,我正在抓取框架,框架还没有被抓住。如果框架为空,我会在开始处添加一点检查。由于这是我的第一篇文章,我如何将其标记为已解决? – Nomaxx117