求opencv视频中行人检测和追踪的c++代码

作者&投稿:聊清 (若有异议请与网页底部的电邮联系)
求opencv中Hog行人检测视频中实现的C++代码!~

整个项目的结构图:

编写DetectFaceDemo.java,代码如下:

[java] view
plaincopyprint?

package com.njupt.zhb.test;

import org.opencv.core.Core;

import org.opencv.core.Mat;

import org.opencv.core.MatOfRect;

import org.opencv.core.Point;

import org.opencv.core.Rect;

import org.opencv.core.Scalar;

import org.opencv.highgui.Highgui;

import org.opencv.objdetect.CascadeClassifier;

//

// Detects faces in an image, draws boxes around them, and writes the results

// to "faceDetection.png".

//

public class DetectFaceDemo {

public void run() {

System.out.println("
Running DetectFaceDemo");

System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());

// Create a face detector from the cascade file in the resources

// directory.

//CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());

//Mat image = Highgui.imread(getClass().getResource("lena.png").getPath());

//注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误

/*

* Detected 0 faces Writing faceDetection.png libpng warning: Image

* width is zero in IHDR libpng warning: Image height is zero in IHDR

* libpng error: Invalid IHDR data

*/

//因此,我们将第一个字符去掉

String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);

CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);

Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));

// Detect faces in the image.

// MatOfRect is a special container class for Rect.

MatOfRect faceDetections = new MatOfRect();

faceDetector.detectMultiScale(image, faceDetections);

System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));

// Draw a bounding box around each face.

for (Rect rect : faceDetections.toArray()) {

Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));

}

// Save the visualized detection.

String filename = "faceDetection.png";

System.out.println(String.format("Writing %s", filename));

Highgui.imwrite(filename, image);

}

}
package com.njupt.zhb.test;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.objdetect.CascadeClassifier;

//
// Detects faces in an image, draws boxes around them, and writes the results
// to "faceDetection.png".
//
public class DetectFaceDemo {
public void run() {
System.out.println("
Running DetectFaceDemo");
System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());
// Create a face detector from the cascade file in the resources
// directory.
//CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());
//Mat image = Highgui.imread(getClass().getResource("lena.png").getPath());
//注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误
/*
* Detected 0 faces Writing faceDetection.png libpng warning: Image
* width is zero in IHDR libpng warning: Image height is zero in IHDR
* libpng error: Invalid IHDR data
*/
//因此,我们将第一个字符去掉
String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);
CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);
Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);

System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));

// Draw a bounding box around each face.
for (Rect rect : faceDetections.toArray()) {
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
}

// Save the visualized detection.
String filename = "faceDetection.png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, image);
}
}

3.编写测试类:

[java] view
plaincopyprint?

package com.njupt.zhb.test;

public class TestMain {

public static void main(String[] args) {

System.out.println("Hello, OpenCV");

// Load the native library.

System.loadLibrary("opencv_java246");

new DetectFaceDemo().run();

}

}

//运行结果:

//Hello, OpenCV

//

//Running DetectFaceDemo

///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml

//Detected 8 faces

//Writing faceDetection.png
package com.njupt.zhb.test;
public class TestMain {
public static void main(String[] args) {
System.out.println("Hello, OpenCV");
// Load the native library.
System.loadLibrary("opencv_java246");
new DetectFaceDemo().run();
}
}
//运行结果:
//Hello, OpenCV
//
//Running DetectFaceDemo
///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml
//Detected 8 faces
//Writing faceDetection.png

记得给我分,急需

#include "cv.h"
#include
#include "highgui.h"
#include
#include
#include
#include
#include
// various tracking parameters (in seconds)
const double MHI_DURATION = 0.5;
const double MAX_TIME_DELTA = 0.5;
const double MIN_TIME_DELTA = 0.05;
const int N = 3;
//
const int CONTOUR_MAX_AERA = 16;
// ring image buffer
IplImage **buf = 0;
int last = 0;
// temporary images
IplImage *mhi = 0;
// MHI: motion history image
int filter = CV_GAUSSIAN_5x5;
CvConnectedComp *cur_comp, min_comp;
CvConnectedComp comp;
CvMemStorage *storage; CvPoint pt[4];
// 参数:
// img – 输入视频帧
// dst – 检测结果
void update_mhi( IplImage* img, IplImage* dst, int diff_threshold )
{
double timestamp = clock()/100.;
// get current time in seconds
CvSize size = cvSize(img->width,img->height);
// get current frame size
int i, j, idx1, idx2;
IplImage* silh;
uchar val;
float temp;
IplImage* pyr = cvCreateImage( cvSize((size.width & -2)/2, (size.height & -2)/2), 8, 1 );
CvMemStorage *stor;
CvSeq *cont, *result, *squares;
CvSeqReader reader;
if( !mhi || mhi->width != size.width || mhi->height != size.height )
{
if( buf == 0 )
{
buf = (IplImage**)malloc(N*sizeof(buf[0]));
memset( buf, 0, N*sizeof(buf[0]));
}
for( i = 0; i < N; i++ )
{
cvReleaseImage( &buf[i] );
buf[i] = cvCreateImage( size, IPL_DEPTH_8U, 1 );
cvZero( buf[i] );
}
cvReleaseImage( &mhi );
mhi = cvCreateImage( size, IPL_DEPTH_32F, 1 );
cvZero( mhi );
// clear MHI at the beginning
}
// end of if(mhi)
cvCvtColor( img, buf[last], CV_BGR2GRAY );
// convert frame to grayscale
idx1 = last;
idx2 = (last + 1) % N;
// index of (last - (N-1))th frame
last = idx2;
// 做帧差
silh = buf[idx2];
cvAbsDiff( buf[idx1], buf[idx2], silh );
// get difference between frames
// 对差图像做二值化
cvThreshold( silh, silh, 30, 255, CV_THRESH_BINARY );
// and threshold it
cvUpdateMotionHistory( silh, mhi, timestamp, MHI_DURATION );
// update MHI
cvCvtScale( mhi, dst, 255./MHI_DURATION,
(MHI_DURATION - timestamp)*255./MHI_DURATION );
cvCvtScale( mhi, dst, 255./MHI_DURATION, 0 );
// 中值滤波,消除小的噪声
cvSmooth( dst, dst, CV_MEDIAN, 3, 0, 0, 0 );
// 向下采样,去掉噪声
cvPyrDown( dst, pyr, 7 );
cvDilate( pyr, pyr, 0, 1 );
// 做膨胀操作,消除目标的不连续空洞
cvPyrUp( pyr, dst, 7 );
//
// 下面的程序段用来找到轮廓
//
// Create dynamic structure and sequence.
stor = cvCreateMemStorage(0);
cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor);
// 找到所有轮廓
cvFindContours( dst, stor, &cont, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
// 直接使用CONTOUR中的矩形来画轮廓
for(;cont;cont = cont->h_next)
{
CvRect r = ((CvContour*)cont)->rect;
if(r.height * r.width > CONTOUR_MAX_AERA) // 面积小的方形抛弃掉
{
cvRectangle( img, cvPoint(r.x,r.y),
cvPoint(r.x + r.width, r.y + r.height),
CV_RGB(255,0,0), 1, CV_AA,0);
}
} // free memory
cvReleaseMemStorage(&stor);
cvReleaseImage( &pyr );
}
int main(int argc, char** argv)
{
IplImage* motion = 0;
CvCapture* capture = 0; //视频获取结构
if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
//原型:extern int isdigit(char c); //用法:#include 功能:判断字符c是否为数字 说明:当c为数字0-9时,返回非零值,否则返回零。
capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 1 );
else if( argc == 2 )
capture = cvCaptureFromAVI( argv[1] );
if( capture )
{
cvNamedWindow( "Motion", 1 );
for(;;)
{
IplImage* image;
if( !cvGrabFrame( capture )) //从摄像头或者视频文件中抓取帧
break;
image = cvRetrieveFrame( capture );
//取回由函数cvGrabFrame抓取的图像,返回由函数cvGrabFrame 抓取的图像的指针
if( image )
{
if( !motion )
{
motion = cvCreateImage( cvSize(image->width,image->height), 8, 1 );
cvZero( motion );
motion->origin = image->origin;
///* 0 - 顶—左结构, 1 - 底—左结构 (Windows bitmaps 风格) */
}
}
update_mhi( image, motion, 60 );
cvShowImage( "Motion", image );
if( cvWaitKey(10) >= 0 )
break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "Motion" );
}
return 0;
}

整个项目的结构图:

编写DetectFaceDemo.java,代码如下:

[java] view
plaincopyprint?

package com.njupt.zhb.test;

import org.opencv.core.Core;

import org.opencv.core.Mat;

import org.opencv.core.MatOfRect;

import org.opencv.core.Point;

import org.opencv.core.Rect;

import org.opencv.core.Scalar;

import org.opencv.highgui.Highgui;

import org.opencv.objdetect.CascadeClassifier;

//

// Detects faces in an image, draws boxes around them, and writes the results

// to "faceDetection.png".

//

public class DetectFaceDemo {

public void run() {

System.out.println("\nRunning DetectFaceDemo");

System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());

// Create a face detector from the cascade file in the resources

// directory.

//CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());

//Mat image = Highgui.imread(getClass().getResource("lena.png").getPath());

//注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误

/*

* Detected 0 faces Writing faceDetection.png libpng warning: Image

* width is zero in IHDR libpng warning: Image height is zero in IHDR

* libpng error: Invalid IHDR data

*/

//因此,我们将第一个字符去掉

String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);

CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);

Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));

// Detect faces in the image.

// MatOfRect is a special container class for Rect.

MatOfRect faceDetections = new MatOfRect();

faceDetector.detectMultiScale(image, faceDetections);

System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));

// Draw a bounding box around each face.

for (Rect rect : faceDetections.toArray()) {

Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));

}

// Save the visualized detection.

String filename = "faceDetection.png";

System.out.println(String.format("Writing %s", filename));

Highgui.imwrite(filename, image);

}

}
package com.njupt.zhb.test;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.objdetect.CascadeClassifier;

//
// Detects faces in an image, draws boxes around them, and writes the results
// to "faceDetection.png".
//
public class DetectFaceDemo {
public void run() {
System.out.println("\nRunning DetectFaceDemo");
System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());
// Create a face detector from the cascade file in the resources
// directory.
//CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());
//Mat image = Highgui.imread(getClass().getResource("lena.png").getPath());
//注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误
/*
* Detected 0 faces Writing faceDetection.png libpng warning: Image
* width is zero in IHDR libpng warning: Image height is zero in IHDR
* libpng error: Invalid IHDR data
*/
//因此,我们将第一个字符去掉
String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);
CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);
Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);

System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));

// Draw a bounding box around each face.
for (Rect rect : faceDetections.toArray()) {
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
}

// Save the visualized detection.
String filename = "faceDetection.png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, image);
}
}

3.编写测试类:

[java] view
plaincopyprint?

package com.njupt.zhb.test;

public class TestMain {

public static void main(String[] args) {

System.out.println("Hello, OpenCV");

// Load the native library.

System.loadLibrary("opencv_java246");

new DetectFaceDemo().run();

}

}

//运行结果:

//Hello, OpenCV

//

//Running DetectFaceDemo

///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml

//Detected 8 faces

//Writing faceDetection.png
package com.njupt.zhb.test;
public class TestMain {
public static void main(String[] args) {
System.out.println("Hello, OpenCV");
// Load the native library.
System.loadLibrary("opencv_java246");
new DetectFaceDemo().run();
}
}
//运行结果:
//Hello, OpenCV
//
//Running DetectFaceDemo
///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml
//Detected 8 faces
//Writing faceDetection.png


opencv保存视频掉帧花屏
可以尝试使用H.264或MPEG-4等常见的编解码器,或者根据自己的需求选择合适的编码方式。3、检查电脑硬件设备的性能:如果电脑的硬件设备性能较低,也可能会导致视频保存过程中出现掉帧、花屏等问题。可以尝试升级电脑的硬件设备或者减轻电脑的负荷,以优化视频保存的效果。4、检查OpenCV代码:可能存在代码逻辑问...

OpenCV视频篇——YCrCb与YUV
在HEVC标准中,RGB空间首先在encoder(编码)处,被转换为YCbCr颜色空间作为输入,经过压缩,最后还原成图像时,再次转换回RGB空间(值得注意的是,标准化软件中并不提供这个转换的功能,这是属于Preprocessing(在encoder)或者Proprocessing (在decoder))。HEVE压缩是针对YUV颜色空间。YUV颜色空间中的Y代表...

opencv如何计算出视频中目标的移动速度(快或慢)和方向?求代码_百度...
计算出帧间目标的质心位移速度和方向

求opencv视频中行人检测和追踪的c++代码
package com.njupt.zhb.test;import org.opencv.core.Core;import org.opencv.core.Mat;import org.opencv.core.MatOfRect;import org.opencv.core.Point;import org.opencv.core.Rect;import org.opencv.core.Scalar;import org.opencv.highgui.Highgui;import org.opencv.objdetect.CascadeClassifier;\/\/ ...

想用opencv打开笔记本电脑摄像头并从视频中提取图像,单摄像头打开后图像...
有一种方法比较简单,效果效率都可靠。就是用播放视频的软件捕获(捕获和截图有质的不同,截图是抓屏幕的画面,捕获是提取视频的画面),有这个功能的播放器很多,值得推荐的是kmplayer这个播放器。播放视频有右键点击画面在菜单中就有捕获选项,捕获》画面高级捕获》按需设定然后播放视频》点开始。

armopencv录视频异常
录制视频异常的原因可能有很多,以下是几种可能的原因和解决方案:1. 硬件问题:如果使用的摄像头或其他视频设备存在问题,可能会导致录制视频异常。您可以尝试更换摄像头或其他设备,看看问题是否仍然存在。2. 软件配置问题:如果您的ARM开发板上的OpenCV安装不正确或配置有误,可能导致录制视频异常。您可以...

opencv实现的AVI视频中运动物体识别与追踪的程序
以前有OPENCV的官网,可以下载到源代码的,我这边贴一个基于vc2005的源代码吧。include <stdio.h> include<iostream> include <cv.h> include <cxcore.h> include <highgui.h> using namespace std;int main( int argc, char** argv ){ \/\/声明IplImage指针 IplImage* pFrame = NULL;IplImage*...

opencv中如何将从视频中抓取的帧释放掉
Mat frame;frame.release();

这段opencv识别视频中的人脸的代码为什么运行时没有错误但也没有出现...
应该是有的 人脸检测有点慢,你可以用一张图片试试看

opencv中如何提取视频图像中变化像素点
差分法或者光流法,简单的话就相邻两帧依次相减

西峰区15263551841: 如何用opencv实现人脸检测与跟踪 -
席军伤湿: 很早以前在processing官网中找到了Face Detect(这个链接需要代理才能打开)这个为processing提供人脸识别功能的lib,今天终于静下心来仔细阅读了它的说明文档,下面是其基本使用方法说明: 首先下载pFaceDetect.zip,在processing的...

西峰区15263551841: 求基于opencv,视频中的行人检测与跟踪的源代码! -
席军伤湿: 这个实现的难度不是非常大,但是编代码需要耗点时间,你可以知道些人脸检测的代码,改改就是了,人脸的分类器和人全身的分类器opencv都是自带的

西峰区15263551841: OpenCV 检测视频范围内有没有人 -
席军伤湿: 你这个要看环境怎么样的,opencv提供了人脸检测和行人检测,如果摄像头离人比较近,人脸检测的效果会好一点,如果比较远可以得到人的全身图像,就需要行人检测了,人脸检测比较容易,用的是haarcascade方法,楼上的答案就是,行人检测也可以用haarcascade,但是效果一般,比较好的是用HOG,网上仔细找找也有代码的.

西峰区15263551841: 求opencv中Hog行人检测视频中实现的C++代码! -
席军伤湿: 整个项目的结构图:编写DetectFaceDemo.java,代码如下:[java] view plaincopyprint?package com.njupt.zhb.test; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfRect; import org.opencv.core.Point; ...

西峰区15263551841: 最近在做一个关于HOG+SVM的行人检测 使用的VS2008平台上配置OPENCV2.3.1 求高手解释一个小问题 -
席军伤湿: 1.首先确保你输入的图片中包含行人,如果没有,那found为空是自然的事情.2.然后,即使你输入的图片中包含行人,OpenCV自带的hog+svm检测函数也不一定能够把行人检测出来,毕竟它的训练样本数目也是有限的,能否检测出行人受到图片背景,光线,行人姿势等多种因素影响.你可以多试试别的图片,一定会有图片可以检测出来的.3.最后,OpenCV安装目录下本身就有一个行人检测的Demo,叫做peopledetect.cpp,去看看吧.那个是可以正确运行的.希望给分,哈哈.

西峰区15263551841: 利用OpenCV开发行人检测程序,怎样提高检测速率 -
席军伤湿: 我近弄步骤致先视频进行背景建模差运物体二值化前景找轮廓遍历轮廓运物体框住定义结构体比前两帧质相差视物体(般视频2帧质都准确率比较高)跟踪功

西峰区15263551841: 求视频中行人检测程序,用VC++和Opencv实现.谢谢!可以追加悬赏~ -
席军伤湿: 我没有做过行人检测,但是这个并不简单,你可以使用haar特征用boosting分类器来做.同时需要大量的行人图片和负样本来对分类器做训练.知道有同学做过这个,光分类器就训练了几周~

西峰区15263551841: opencv运动检测与运动跟踪 -
席军伤湿: 我最近在弄这个,步骤大致是这样,先对视频进行背景建模,然后差分得出运动物体,二值化前景,找轮廓,遍历轮廓把运动物体框住,定义结构体对比前后两帧质心,相差不大的可以视为一个物体(一般视频里的2帧质心都不大,准确率比较高),跟踪成功

西峰区15263551841: OpenCV 检测视频范围内有没有人.. -
席军伤湿: 特征提取,人像识别,这个比较有难度啊,有做这个的公司,你可以找找,高校里也有专门研究这个的;还有个缩水的办法,只是有缺陷,那就是移动物体监测,这个就比较容易了.

西峰区15263551841: 有关opencv平台的HOG+SVM分类器行人检测 -
席军伤湿: hog是用于进行特征比较和归一化处理来实现识别和分类; 你是要生成特征库还是? 如果是生成特征库 请参照XML生成方法

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网