2016-10-27 64 views
-1

這是顏色檢測的源代碼,請幫我在Core.inRangeCore.putText,誤差在新的點「矩形」(rect.x + rect.width,rect.y + rect.height),如何使用OpenCV通過顏色檢測在對象上添加文本?

公共無效過程(太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

你什麼錯誤?請更新您的問題與更多細節。 – sorifiend

回答

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)