Created
September 5, 2012 11:31
-
-
Save yanunon/3635330 to your computer and use it in GitHub Desktop.
加权迭代重建背景,测试常规循环,SSE和OpenCV的速度
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <opencv2/opencv.hpp> | |
//#include <stdlib.h> | |
#include <mmintrin.h> | |
//#include <xmmintrin.h> | |
#include <time.h> | |
using namespace cv; | |
#define CALC_RUN_TIME(fun_line, func_name) \ | |
{\ | |
clock_t start,stop;\ | |
float temp;\ | |
start = clock();\ | |
fun_line;\ | |
stop = clock();\ | |
temp=((double)(stop-start)*1000.0)/CLOCKS_PER_SEC;\ | |
char format_str[64];\ | |
strcpy(format_str, (func_name));\ | |
strcat(format_str, ": %.4fms\n");\ | |
printf(format_str, temp);\ | |
}; | |
void mk_bg_sse(Mat &gray, Mat &bg, Mat &gray32, Mat &bg32, float alpha){ //release:3.5ms | |
gray.convertTo(gray32, CV_32FC1); | |
bg.convertTo(bg32, CV_32FC1); | |
int Ni = gray.rows * gray.cols / 4; | |
float *pGray = (float*)gray32.data; | |
float *pBg = (float*)bg32.data; | |
__m128 _m_gray, _m_bg; | |
__m128 *_p_m_gray = (__m128*)gray32.data; | |
__m128 *_p_m_bg = (__m128*)bg32.data; | |
__m128 _m_alpha_1, _m_alpha_2; | |
_m_alpha_1 = _mm_set_ps1(alpha); | |
_m_alpha_2 = _mm_set_ps1(1 - alpha); | |
for (int i = 0; i < Ni; i++) | |
{ | |
/*_m_gray = _mm_load_ps(pGray); | |
_m_bg = _mm_load_ps(pBg); | |
_m_bg = _mm_add_ps(_mm_mul_ps(_m_gray, _m_alpha_1), _mm_mul_ps(_m_bg, _m_alpha_2)); | |
_mm_store_ps(pBg, _m_bg); | |
pGray += 4; | |
pBg += 4;*/ | |
*_p_m_bg = _mm_add_ps(_mm_mul_ps(*_p_m_gray, _m_alpha_1), _mm_mul_ps(*_p_m_bg, _m_alpha_2)); | |
_p_m_bg++; | |
_p_m_gray++; | |
} | |
bg32.convertTo(bg, CV_8UC1); | |
} | |
void mk_bg_cv(Mat &gray, Mat &bg, Mat &gray32, Mat &bg32, float alpha){ //release:2.5ms | |
//gray.convertTo(gray32, CV_32FC1); | |
bg.convertTo(bg32, CV_32FC1); | |
accumulateWeighted(gray, bg32, alpha); | |
bg32.convertTo(bg, CV_8UC1); | |
} | |
void mk_bg_normal(Mat &gray, Mat &bg, Mat &gray32, Mat &bg32, float alpha){ //release:5ms | |
int Ni = gray.rows * gray.cols; | |
uchar *pGray = gray.data; | |
uchar *pBg = bg.data; | |
float alpha1 = 1 - alpha; | |
for (int i = 0; i < Ni; i++) | |
{ | |
*pBg = *pBg * alpha1 + *pGray * alpha; | |
pGray++; | |
pBg++; | |
} | |
} | |
int main(int argc, char **argv){ | |
VideoCapture capture("test.mov"); | |
Mat colorMat; | |
capture >> colorMat; | |
Mat grayMat; | |
grayMat.create(colorMat.rows, colorMat.cols, CV_8UC1); //768*576 | |
Mat backgroundMat; | |
backgroundMat.create(colorMat.rows, colorMat.cols, CV_8UC1); | |
backgroundMat.setTo(Scalar(0)); | |
Mat grayMat32; | |
grayMat32.create(colorMat.rows, colorMat.cols, CV_32FC1); | |
Mat backgroundMat32; | |
backgroundMat32.create(colorMat.rows, colorMat.cols, CV_32FC1); | |
char keyborad = 0; | |
namedWindow("Gray"); | |
namedWindow("BG"); | |
while (keyborad != 27) | |
{ | |
capture >> colorMat; | |
if (colorMat.empty()) | |
{ | |
break; | |
} | |
cvtColor(colorMat, grayMat, CV_BGR2GRAY); | |
CALC_RUN_TIME(mk_bg_normal(grayMat, backgroundMat, grayMat32, backgroundMat32, 0.1), "mk_bg_normal"); | |
//mk_bg_sse(grayMat, backgroundMat, grayMat32, backgroundMat32, 0.1); | |
imshow("Gray", grayMat); | |
imshow("BG", backgroundMat); | |
keyborad = waitKey(3); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment