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

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

最佳答案

cvPutText() is the way to go

error message很有趣,您对cvPutText()的调用没有问题,因此问题必须出在代码的其他部分。确保您正在导入:

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

10-04 10:18