如何使每个rect_pnt所需的蒙版都出现在roi中?

时间:2019-10-15 15:40:48

标签: java opencv computer-vision

我必须为每个rect_pnt打印一个绿色球的图像,所以我定义了roi,mask,但是当我启动该应用程序时,我无法仅显示pallino架子上矩形的轮廓

你能帮我吗?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    cameraBridgeViewBase = (JavaCameraView) 
findViewById(R.id.CameraView);
    cameraBridgeViewBase.setVisibility(SurfaceView.VISIBLE);
    cameraBridgeViewBase.setCvCameraViewListener(this);
    image= BitmapFactory.decodeResource(this.getResources(), 
R.drawable.pv);
}

@Override
public void onCameraViewStarted(int width, int height) {
    gray= new Mat(height,width, CvType.CV_8UC1);
    hierarchy=new Mat();
    mask=new Mat();
    src_roi=new Mat();

}

@Override
public void onCameraViewStopped() {
    gray.release();
    hierarchy.release();

}
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrameinputFrame) {

  Mat img=inputFrame.rgba();
  Utils.bitmapToMat(image, mask);
  Imgproc.cvtColor(img,gray,Imgproc.COLOR_BGR2GRAY,0);
  Imgproc.Canny(gray,gray,100,255,3,true);
    Mat kernel = 
Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE,kernelSize);
    Imgproc.dilate(gray, gray, kernel);
    Imgproc.GaussianBlur(gray, gray, new Size(3,3), 0);
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    //trova contorni. Ogni contorno è memorizzato come un vettore dipunti
 Imgproc.findContours(gray,contours,hierarchy, Imgproc.RETR_TREE, 
 Imgproc.CHAIN_APPROX_SIMPLE);
    MatOfPoint2f approxCurve = new MatOfPoint2f();
    //P+++++++
    List<Rect> rect_pnt= new ArrayList<>();
    Comparator<Rect> comp=new Comparator<Rect>() {
        @Override
        public int compare(Rect o1, Rect o2) {
            int result= new Integer((int) o1.tl().x).compareTo((int) 
 o2.tl().x);
            if(result==0){
                result = new Integer((int)o1.tl().y).compareTo((int) 
 o2.tl().y);
            }
            return result;
        }
    };
    //++++++++

    //For each contour found
    for(int i=0;i<contours.size();i++){
        int j=0;
        //convert contours(i) from MatOfPoint to MatOfPoint2f
        MatOfPoint2f contour2f= new 
 MatOfPoint2f(contours.get(i).toArray());
        double approxDistance=Imgproc.arcLength(contour2f,true)*0.05;
        Imgproc.approxPolyDP(contour2f,approxCurve,approxDistance,true);
        //convert back to MatOfPoint
        MatOfPoint points= new MatOfPoint(approxCurve.toArray());
        //Get bounding rect of contour
        Rect rect = Imgproc.boundingRect(points);

        //draw enclosing rectangle

 if(rect.height>60&&rect.width>180&&rect.height<260&&rect.width<400) {
            //P++++++++
            rect_pnt.add(j,rect);
            //++++++++
            Imgproc.rectangle(img, rect.tl(), rect.br(), 
new Scalar(0,0,255), 4);
            j++;
        }
    }
    Collections.sort(rect_pnt,comp);
    System.out.println("Rectangle sorted:" + rect_pnt);
    Mat mask_gray=new Mat();
    for (int i = 0; i < rect_pnt.size(); i++) {

       Imgproc.resize(mask,mask_gray,new Size((int) 
rect_pnt.get(i).width/2,(int) rect_pnt.get(i).height/4));
       Imgproc.cvtColor(mask_gray,mask_gray,Imgproc.COLOR_RGB2GRAY);

       Rect roi = new Rect((int) rect_pnt.get(i).tl().x, (int) 
 rect_pnt.get(i).tl().y, (int) rect_pnt.get(i).width/2, (int) 
 rect_pnt.get(i).height/4);
       gray.submat(roi).copyTo(src_roi);
       Core.addWeighted(src_roi,1.0,mask_gray,1.0,0,src_roi);
        src_roi.copyTo(new Mat(img, roi));
    }

  return img;
 }

首先我选择感兴趣的矩形,然后在伪造器中尝试通过将点阵图放在OnCreate()中并将其放入图像中来打印点。 使用Utils.bitmapToMat(图像,遮罩);我把它放在面具里,然后在里面做la roi和addWeighted。 有问题的伪造是最后的伪造。

我希望我很清楚!在实践中,启动相机时我看不到绿球,而且我也不明白自己的错。

0 个答案:

没有答案