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);
}
does never work as an error description
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?I edited the code, that is the full function .
and the problem is ?
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.