I am trying to do a FaceDetection over an Image displayed over Android ImageView. I have made the ImageView as fulls screen mode, then I loaded the Image from storage as-
IplImage image = cvLoadImage(strImageFilePath, // filename
CV_LOAD_IMAGE_GRAYSCALE);
Then I ran the classifiers and then tried to draw the rectangle but its coming some other region-
canvas.drawRect(new Rect(x, y, x + w, y + h), paint);
Following is the code sample-
public void process(String strImageFilePath) {
ClassifierLoader classifierLoader = ClassifierLoader.getInstance(getContext());
classifier = classifierLoader.getClassifier_face();
classifier_mouth = classifierLoader.getClassifier_mouth();
IplImage image = cvLoadImage(strImageFilePath, // filename
CV_LOAD_IMAGE_GRAYSCALE);
cvClearMemStorage(storage);
Log.i("imageimage ", ""+image.height()+" Width "+image.width());
cvClearMemStorage(storage);
faces = cvHaarDetectObjects(image, classifier, storage, 1.1, 4,
CV_HAAR_FIND_BIGGEST_OBJECT | CV_HAAR_DO_ROUGH_SEARCH);
mouth = cvHaarDetectObjects(image, classifier_mouth, storage, 1.1, 3,
CV_HAAR_FIND_BIGGEST_OBJECT | CV_HAAR_DO_ROUGH_SEARCH);
invalidate();
Log.i("AYA ", "IMAGE AAYA " + image+" FAC "+faces.total()+" mouth "+mouth.total()+ " THIS "+this.getWidth() );
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setTextSize(20);
paint.setStrokeWidth(8);
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(20, 40, 5, paint);
if(faces != null){
if (faces.total() > 0) {
String s = "Processed Face";
float textWidth = paint.measureText(s);
canvas.drawText(s, (getWidth() - textWidth) / 2, 20, paint);
CvRect r = new CvRect(cvGetSeqElem(faces, 0));
int x = r.x(), y = r.y(), w = r.width(), h = r.height();
Log.i("IMAGE VIEW DRAW ", "x "+x+" y "+y+" w "+w+" h "+h);
canvas.drawRect(new Rect(x, y, x + w, y + h), paint);
if(mouth.total() > 0){
CvRect rct = new CvRect(cvGetSeqElem(mouth, 0));
canvas.drawRect(new Rect(rct.x(),rct.y(),rct.x()+rct.width(),rct.y()+rct.height()), paint);
}
}
}
}
I believe I need to do something with the height of IplImage but since I am directly loading the same from storage , it might be the issue. I actually wanted to load the image from image viewer but I couldn;t get it :( http://answers.opencv.org/question/22636/how-to-load-an-iplimage-from-android-imageview/?comment=22662#comment-22662
This is how algorithm is currently drawing the rectangle-