1
votes

I have written a method to do some vision processing in openCV for a robot that I am building for a competition. The frame and process objects are mat objects and instance fields and are taken from a usb camera stream by another method that I know for a fact works. When I display the frame object with another method, the frame does not display if I have run the process method, but it does if I have not. The frame mat contains the unprocessed image over which I draw markers and contours and the process mat is used for storing the processed images before markers and contours are drawn onto the frame. [EDIT]: I have found that it is being caused by the cvtColor method. It is printing error: (-215) (scn == 3 || scn == 4) && (depth == CV_8U || depth == 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

1 Answers

0
votes

This error comes up when the format of the source or destination Mat is not right for the given conversion. According to the error, the function is looking for an array of Uint8 or Float32.

Using cvtColor "in-place" can sometimes create problems. I suggest creating a new Mat for the HSV image.

You can also check this example for color conversion.