按照海思3559上移植OpenCV3.4.1 (一) 的步骤
已经有 了库和头文件了。
库文件添加
将所有的so库打包到根文件系统的lib路径下。
代码
参考
http://bbs.ebaina.com/thread-4858-1-1.html
代码如下:
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/reboot.h>
#include <sys/time.h>
#include "cv.h"
#include "highgui.h"
#include "highgui.hpp"
using namespace cv;
using namespace std;
#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* __cplusplus */
struct timeval tpstart,tpend;
unsigned long timeuses;
void timeRec()
{
gettimeofday(&tpstart,0);
}
int timeRep()
{
gettimeofday(&tpend,0);
timeuses=(tpend.tv_sec-tpstart.tv_sec)*1000000+tpend.tv_usec-tpstart.tv_usec;
printf("use time: %luus\n",timeuses);
return timeuses;
}
int g_bRunning = 1;
void exit_process()
{
g_bRunning = 0;
}
static void exit_sig_process(int signo)
{
if (SIGINT == signo || SIGTERM == signo)
{
exit_process();
}
return;
}
int main(int argc, char* argv[])
{
signal(SIGINT, exit_sig_process);
signal(SIGTERM, exit_sig_process);
printf("%s \n", __FUNCTION__);
IplImage* img = NULL;
// IplImage* cutImg = NULL;
CvMemStorage* storage = cvCreateMemStorage(0);
Mat dest;
CvRect *pface_res;
printf("%s cvLoad \n", __FUNCTION__);
//加载检测器
CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*)cvLoad("/app/haarcascade_frontalface_alt2.xml", 0, 0, 0);
CvSeq* faces;
printf("%s cvLoadImage\n", __FUNCTION__);
img = cvLoadImage(argv[1], 0);
timeRec();
printf("%s cvLoadImage %s \n", __FUNCTION__,argv[1]);
faces = cvHaarDetectObjects(img, cascade, storage, 1.2, 2, 0, cvSize(25,25) );
timeRep();
if (faces->total == 0){
printf("no face!\n");
}
printf("faces total is :%d \n",faces->total);
pface_res = (CvRect*)cvGetSeqElem( faces, 0);
printf("(%d,%d),(%d,%d), \n",pface_res->x,pface_res->y,pface_res->width,pface_res->height);
printf("cvSetImageROI! start \n");
cvSetImageROI(img, *(pface_res));
printf("cvSaveImage! start \n");
// cvSaveImage("face.bmp", img);
dest = cvarrToMat(img);
imwrite("face.bmp", dest);
printf("cvResetImageROI! start \n");
cvResetImageROI(img);
printf("face detected! in face.bmp!\n");
return 0;
}
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
编译之后就可以进行测试了,
使用的测试照片如下:
使用命令:
./OpenCV_Face test.jpg
进行测试
出现错误一:
/OpenCV_Face test.jpg
main
main cvLoad
OpenCV(3.4.1) Error: Unspecified error (The node does not represent a user object (unknown type?)) in cvRead, file /home/lie/third_party/opencv-3.4.1/modules/core/src/persistence_c.cpp, line 1422
terminate called after throwing an instance of ‘cv::Exception’
what(): OpenCV(3.4.1) /home/lie/third_party/opencv-3.4.1/modules/core/src/persistence_c.cpp:1422: error: (-2) The node does not represent a user object (unknown type?) in function cvRead
Aborted (core dumped)
换成
中的
haarcascade_frontalface_alt2.xml
没有出现问题
继续往下
照片的人脸坐标能够打印出来。
用的时间是:1秒多
use time: 1398728us
识别出的人脸数是 :1
坐标点(157,100), 人脸大小(50,50),
出现错误二:
OpenCV(3.4.1) Error: Assertion failed ((flags & FIXED_TYPE) != 0) in type, file /home/lie/third_party/opencv-3.4.1/modules/core/src/matrix_wrap.cpp, line 807
terminate called after throwing an instance of ‘cv::Exception’
what(): OpenCV(3.4.1) /home/lie/third_party/opencv-3.4.1/modules/core/src/matrix_wrap.cpp:807: error: (-215) (flags & FIXED_TYPE) != 0 in function type
这个是在执行
cvSaveImage("/app/face.bmp", img);
时出现的
目前不知道什么原因。
网上有的说是3.4.1的问题,
转换成Mat格式.
先把这个cvSaveImage注释掉;
添加以下代码:
dest = cvarrToMat(img);
imwrite("face.bmp", dest);
可以生产人脸的照片了。是50X50的灰度照片。