Search code examples
opencvjavacv

How to write a text over an IplImage?


I was a exploring a face detection program in javacv and it works perfectly. It catches the video,takes 20fps and pass it to FaceDetection class to detect the faces. The processed image is then send back to the live feed. If a face is detected, an rectangle is drawn around the face. I need to add the some text in the face detected image along with the rectangle. I tried using cvPutText method. But it shows an error that, "cvPutText is undefined for the type FaceDetection".

The code of FaceDetection.java :

import static com.googlecode.javacv.cpp.opencv_core.CV_AA;
import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_8U;
import static com.googlecode.javacv.cpp.opencv_core.cvClearMemStorage;
import static com.googlecode.javacv.cpp.opencv_core.cvGetSeqElem;
import static com.googlecode.javacv.cpp.opencv_core.cvLoad;
import static com.googlecode.javacv.cpp.opencv_core.cvPoint;
import static com.googlecode.javacv.cpp.opencv_core.cvRectangle;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_BGR2GRAY;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_INTER_LINEAR;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvCvtColor;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvEqualizeHist;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvResize;
import static com.googlecode.javacv.cpp.opencv_objdetect.CV_HAAR_DO_CANNY_PRUNING;
import static com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects;
import static com.googlecode.javacv.cpp.opencv_objdetect.cvReleaseHaarClassifierCascade;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.googlecode.javacv.cpp.opencv_core.CvFont;
import com.googlecode.javacv.cpp.opencv_core.CvMemStorage;
import com.googlecode.javacv.cpp.opencv_core.CvRect;
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.CvSeq;
import com.googlecode.javacv.cpp.opencv_core.Cv_iplCreateImageHeader;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_imgproc.CvDistanceFunction;
import com.googlecode.javacv.cpp.opencv_objdetect.CvHaarClassifierCascade;

@SuppressWarnings("unused")
public class FaceDetection 
{
     private static final int SCALE = 2;
                            public int j=0,k=0,no,total;
                            public String timeStamp;
                            public int w=0,h=0,distance=0;
                            public String viewers,dist;
                            FileWriter out;

     public IplImage FaceDetections(IplImage origImg,int no) throws IOException 
     {          
                out = new FileWriter("D:/log.csv",true);               
                String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss").format(Calendar.getInstance().getTime());
                if(no==1)
                {
                   out.append("\n From "+timeStamp+"\n");
                   out.append("Serial No,Face Coordinates,Face No,TimeStamp,Distance,count\n");
                }
                    String CASCADE_FILE ="C:/opencv/data/haarcascades/haarcascade_frontalface_alt2.xml"; 
                            try
                            {                              
                                            IplImage grayImg = IplImage.create(origImg.width(),origImg.height(), IPL_DEPTH_8U, 1);
                                            cvCvtColor(origImg, grayImg, CV_BGR2GRAY);
                                            IplImage smallImg = IplImage.create(grayImg.width()/SCALE,grayImg.height()/SCALE, IPL_DEPTH_8U, 1); 
                                            cvResize(grayImg, smallImg, CV_INTER_LINEAR);                                             
                                            IplImage equImg = IplImage.create(smallImg.width(),smallImg.height(), IPL_DEPTH_8U, 1);
                                            cvEqualizeHist(smallImg, equImg);                                           
                                            CvMemStorage storage = CvMemStorage.create();
                                            CvHaarClassifierCascade cascade =new CvHaarClassifierCascade(cvLoad(CASCADE_FILE));                                                 
                                            CvSeq faces = cvHaarDetectObjects(equImg, cascade, storage,1.1, 3, CV_HAAR_DO_CANNY_PRUNING);                                        
                                            cvClearMemStorage(storage);    
                                            cvReleaseHaarClassifierCascade(cascade);
                                            total = faces.total();                                                            
                                            for (int i = 1; i <= total; i++) 
                                            {
                                                            CvRect r = new CvRect(cvGetSeqElem(faces, i));                       
                                                            cvRectangle(origImg, cvPoint( r.x()*SCALE, r.y()*SCALE ),cvPoint( (r.x() + r.width())*SCALE,(r.y() + r.height())*SCALE ),CvScalar.BLUE, 2, CV_AA, 0);         
                                                            String strRect = String.format("%d-%d-%d-%d ", r.x(), r.y(), r.width(), r.height());                                                                                                                                                                             
                                                            out.append(no+","+strRect+","+i+" ,"+timeStamp+","+distance+","+total+"\n");
                                                            System.out.println(" "+strRect); 

                                                            CvFont font;
                                                            cvInitFont(font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5);
                                                            cvPutText(origImg,"male",cvPoint(100,200),&font,CvScalar.BLUE);                                                    
                                            }
                                            out.flush();
                                            out.close();
                                            CvSeq.deallocateReferences();
                            }

                catch(Exception e)
                {
                            System.out.println("Exception FD"+e);

                }
                            IplImage equImg = null;
                            IplImage smallImg=null;
                            IplImage grayImg =null;
                            return origImg; 
            }

}

Is there any other way to add text to the iplimage?? i'm using opencv 2.4.4


Solution

  • cvPutText() is the way to go.

    The error message is funny, there's no problem with your call to cvPutText(), so the problem must be in some other part of your code. Make sure you are importing:

    import static com.googlecode.javacv.cpp.opencv_core.cvPutText;