如何使用OpenCV通过颜色检测在对象上添加文本?

时间:2016-10-27 03:39:51

标签: java opencv

这是颜色检测源代码,请在Core.inRangeCore.putText帮助我,错误在" rect"在新的Point(rect.x + rect.width,rect.y + rect.height),

public void process(Mat rgbaImage){

    Imgproc.pyrDown(rgbaImage, mPyrDownMat);
    Imgproc.pyrDown(mPyrDownMat, mPyrDownMat);

    Imgproc.cvtColor(mPyrDownMat, mHsvMat, Imgproc.COLOR_RGB2HSV_FULL);

    Core.inRange(mHsvMat, mLowerBound, mUpperBound, mMask);
    Imgproc.dilate(mMask, mDilatedMask, new Mat());

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    Imgproc.findContours(mDilatedMask, contours, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

    // Find max contour area

    double maxArea = 0;
    Iterator<MatOfPoint> each = contours.iterator();
    while (each.hasNext()) {
        MatOfPoint wrapper = each.next();
        double area = Imgproc.contourArea(wrapper);
        if (area > maxArea)
            maxArea = area;
    }

    // Filter contours by area and resize to fit the original image size
    mContours.clear();
    each = contours.iterator();
    while (each.hasNext()) {
        MatOfPoint contour = each.next();
        if (Imgproc.contourArea(contour) > mMinContourArea*maxArea) {
            Core.multiply(contour, new Scalar(4,4), contour);
            mContours.add(contour);

        }
    }

//在框架中绘制矩形

    MatOfPoint2f approxCurve=new MatOfPoint2f();
    for(int i=0;i<contours.size();i++)
    {
        MatOfPoint2f countour2f = new MatOfPoint2f(contours.get(i).toArray());
        double approxDistance = Imgproc.arcLength(countour2f, true)*0.02;
        Imgproc.approxPolyDP(countour2f, approxCurve, approxDistance, true);

        // Convert back to Contour
        MatOfPoint points=new MatOfPoint(approxCurve.toArray());
        //Get Bounding rect of contour
        Rect rect=Imgproc.boundingRect(points);
        //show text on object       }    }
public List<MatOfPoint> getContours() {
    return mContours;
}
public boolean checkColor(Scalar hsvColor, String color){
    if(hsvColor.val[0] >= 142 && hsvColor.val[0] <= 4 && hsvColor.val[1] >= 59 && hsvColor.val[1] <= 62 && hsvColor.val[2] >= 53 && hsvColor.val[2] <= 55){
            colorHit = true;
            color = RED;

    }else if(hsvColor.val[0] >= 171 && hsvColor.val[0] <= 356 && hsvColor.val[1] >= 79 && hsvColor.val[1] <= 53 && hsvColor.val[2] >= 84 && hsvColor.val[2] <= 67){
        colorHit = true;
        color = RED2; 
    }else if(hsvColor.val[0] >= 169 && hsvColor.val[0] <= 5 && hsvColor.val[1] >= 96 && hsvColor.val[1] <= 47 && hsvColor.val[2] >= 89 && hsvColor.val[2] <= 66){
        colorHit = true;
        color = RED3; 
    }else if(hsvColor.val[0] >= 90 && hsvColor.val[0] <= 140 && hsvColor.val[1] >= 120 && hsvColor.val[1] <= 255 && hsvColor.val[2] >= 80 && hsvColor.val[2] <= 160){
        colorHit = true;
        color = GREEN;

        }else{
            colorHit = false;
        }
        return colorHit;
    }    //converts an input image from YUV to RGB to HSV color space
public static void cvt_YUVtoRGBtoHSV(Mat src, Mat dst) {
    Mat mSrc = new Mat(); 
    src.copyTo(mSrc); 
    Imgproc.cvtColor(mSrc, dst, Imgproc.COLOR_YUV420sp2RGB); 
    Imgproc.cvtColor(dst, dst, Imgproc.COLOR_RGB2HSV);    }
    public static void getRedMat(Mat src, Mat dst){   Core.inRange(src, new Scalar(142, 59, 53), new Scalar(4, 62, 55), dst);
 Core.putText(src, "Matang ", 
 new Point(rect.x+rect.width,rect.y+rect.height), //this is error
  Core.FONT_HERSHEY_SIMPLEX, 2.6f, new Scalar(255, 255, 0))   ;
              } 
}

1 个答案:

答案 0 :(得分:1)

看起来你的论点太多了。

以下是Core.putText接受的内容:

putText(Mat img, java.lang.String text, Point org, int fontFace, double fontScale, Scalar color)

但是在通话结束时你还有一个额外的。从此行的末尾删除,5

Core.FONT_HERSHEY_SIMPLEX, 2.6f, new Scalar(255, 255, 0),5)

有关API的更多信息,请参阅此处:

http://docs.opencv.org/java/2.4.2/org/opencv/core/Core.html#putText(org.opencv.core.Mat,%20java.lang.String,%20org.opencv.core.Point,%20int,%20double,%20org.opencv.core.Scalar)