Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

copyTo don't work

I want to make app which swap faces in picture. The mat of the picture is picMat, and I want to copy a mat of detected face on picmat, but the function copyTo don't work. here is the code:

    Bitmap faceBitmap;
    SparseArray<Face> faces = faceDetector.detect(frame);
    for(int i=0; i<faces.size(); i++) {
        Face thisFace = faces.valueAt(i);
        faceBitmap = Bitmap.createBitmap(frame.getBitmap(), (int) thisFace.getPosition().x,
                (int) thisFace.getPosition().y, (int) thisFace.getWidth(), (int) thisFace.getHeight());

        Mat temp = new Mat();
        faceBitmap = faceBitmap.copy(Bitmap.Config.ARGB_8888, true);
        bitmapToMat(faceBitmap, temp);
        float x1 = thisFace.getPosition().x;
        float y1 = thisFace.getPosition().y;
        float x2 = x1 + thisFace.getWidth();
        float y2 = y1 + thisFace.getHeight();
        Rect temp_rect = new Rect((int)x1,(int)y1,temp.cols(),temp.rows());
        rects.add(temp_rect);
        if(i==3){
           temp.copyTo( picMat.submat( rects.get(0)) );
        }
    }

  Bitmap img = Bitmap.createBitmap(picMat.cols(), picMat.rows(),Bitmap.Config.ARGB_8888);
    matToBitmap(picMat, img);
    imageView.setImageBitmap(img);
click to hide/show revision 2
retagged

updated 2019-08-17 02:30:47 -0600

berak gravatar image

copyTo don't work

I want to make app which swap faces in picture. The mat of the picture is picMat, and I want to copy a mat of detected face on picmat, but the function copyTo don't work. here is the code:

    Bitmap faceBitmap;
    SparseArray<Face> faces = faceDetector.detect(frame);
    for(int i=0; i<faces.size(); i++) {
        Face thisFace = faces.valueAt(i);
        faceBitmap = Bitmap.createBitmap(frame.getBitmap(), (int) thisFace.getPosition().x,
                (int) thisFace.getPosition().y, (int) thisFace.getWidth(), (int) thisFace.getHeight());

        Mat temp = new Mat();
        faceBitmap = faceBitmap.copy(Bitmap.Config.ARGB_8888, true);
        bitmapToMat(faceBitmap, temp);
        float x1 = thisFace.getPosition().x;
        float y1 = thisFace.getPosition().y;
        float x2 = x1 + thisFace.getWidth();
        float y2 = y1 + thisFace.getHeight();
        Rect temp_rect = new Rect((int)x1,(int)y1,temp.cols(),temp.rows());
        rects.add(temp_rect);
        if(i==3){
           temp.copyTo( picMat.submat( rects.get(0)) );
        }
    }

  Bitmap img = Bitmap.createBitmap(picMat.cols(), picMat.rows(),Bitmap.Config.ARGB_8888);
    matToBitmap(picMat, img);
    imageView.setImageBitmap(img);

copyTo don't work

I want to make app which swap faces in picture. The mat of the picture is picMat, and I want to copy a mat of detected face on picmat, but the function copyTo don't work. here is the code:

 private void detectFace(BitmapFactory.Options options) {
    Bitmap bitmap = BitmapFactory.decodeResource(
            this.getResources(), R.drawable.test2,
            options
    );

    Mat picMat = new Mat();
    bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
    bitmapToMat(bitmap, picMat);

   ArrayList<Rect> rects = new ArrayList<>(); // opencv Rect
   Bitmap faceBitmap;
 SparseArray<Face> faces = faceDetector.detect(frame);
 for(int i=0; i<faces.size(); i++) {
     Face thisFace = faces.valueAt(i);
     faceBitmap = Bitmap.createBitmap(frame.getBitmap(), (int) thisFace.getPosition().x,
             (int) thisFace.getPosition().y, (int) thisFace.getWidth(), (int) thisFace.getHeight());

     Mat temp = new Mat();
     faceBitmap = faceBitmap.copy(Bitmap.Config.ARGB_8888, true);
     bitmapToMat(faceBitmap, temp);
     float x1 = thisFace.getPosition().x;
     float y1 = thisFace.getPosition().y;
     float x2 = x1 + thisFace.getWidth();
     float y2 = y1 + thisFace.getHeight();
     Rect temp_rect = new Rect((int)x1,(int)y1,temp.cols(),temp.rows());
     rects.add(temp_rect);
     if(i==3){
        temp.copyTo( picMat.submat( rects.get(0)) );
     }
 }

 Bitmap img = Bitmap.createBitmap(picMat.cols(), picMat.rows(),Bitmap.Config.ARGB_8888);
 matToBitmap(picMat, img);
 imageView.setImageBitmap(img);
}