copyTo don't work

asked 2019-08-16 16:38:00 -0500

student_CS gravatar image

updated 2019-08-20 10:16:00 -0500

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,

    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());
       temp.copyTo( picMat.submat( rects.get(0)) );

Bitmap img = Bitmap.createBitmap(picMat.cols(), picMat.rows(),Bitmap.Config.ARGB_8888);
matToBitmap(picMat, img);
edit retag flag offensive close merge delete



don't work.

does never work as an error description

berak gravatar imageberak ( 2019-08-17 02:31:31 -0500 )edit

Also where is the code that initializes rects? Could you please provide all the code or at least a minimal runnable sample to reproduce your issues?

StevenPuttemans gravatar imageStevenPuttemans ( 2019-08-19 09:03:23 -0500 )edit

I edited the code, that is the full function .

student_CS gravatar imagestudent_CS ( 2019-08-20 10:16:47 -0500 )edit

and the problem is ?

berak gravatar imageberak ( 2019-08-20 13:40:31 -0500 )edit

temp is the matrix of the face, and I wanted to copy temp on small part in the matrix of the big picture,Instead it replace the big picture with temp. I have a picture of group, and I wanted to crop face of person -X, and paste on a face of person -Y, instead I got the picture of the group replaced by the picture of the face I cropped.

student_CS gravatar imagestudent_CS ( 2019-08-20 13:53:51 -0500 )edit