1

我正在探索 javacv 中的人脸检测程序,它运行良好。它捕获视频,以 20fps 的速度将其传递给 FaceDetection 类以检测人脸。然后将处理后的图像发送回实时源。如果检测到人脸,则会在人脸周围绘制一个矩形。我需要在人脸检测图像中添加一些文本以及矩形。我尝试使用 cvPutText 方法。但它显示一个错误,“cvPutText 未为 FaceDetection 类型定义”。

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; 
            }

}

有没有其他方法可以向 iplimage 添加文本?我正在使用 opencv 2.4.4

4

1 回答 1

2

cvPutText() 是 要走的路

错误消息很有趣,您调用 没有问题cvPutText(),所以问题一定出在代码的其他部分。确保您正在导入:

import static com.googlecode.javacv.cpp.opencv_core.cvPutText;
于 2013-04-01T18:28:12.657 回答