[問題] Windows Form with OpenCV膚色檢測
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
VC++ 2005
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
OpenCV
問題(Question):
程式執行過慢
程式碼(Code):(請善用置底文網頁, 記得排版)
//-----------以下程式碼放在timer裡面-----------//
深綠色部分可先忽略不看
frame = cvQueryFrame(capture);
cvSaveImage("webcam.bmp",frame);
image = cvLoadImage("webcam.bmp",1);
img = cvLoadImage("webcam.bmp",1);
dst = cvCreateImage(cvSize(img->width ,img->height ),IPL_DEPTH_8U,1);
dst_img = cvCreateImage(cvSize(img->width,img->height),IPL_DEPTH_8U,3);
int height,width,step,channels;//定義1
uchar *data;
height = dst_img->height;
width = dst_img->width;
step = dst_img->widthStep;
channels = dst_img->nChannels;
data = (uchar *)dst_img->imageData;
cvCvtColor(img,dst_img,CV_BGR2YCrCb);
double c[4]={0.0077,-0.0041,-0.0041,0.0047};
CvMat* max=cvCreateMat(height,width,CV_64FC1);
CvMat* mat=cvCreateMat(2,2,CV_64FC1);
CvMat* a=cvCreateMat(1,2,CV_64FC1);
CvMat* a1=cvCreateMat(2,1,CV_64FC1);
CvMat* res=cvCreateMat(1,1,CV_64FC1);
cvInitMatHeader(mat,2,2,CV_64FC1,c);
double res1[1]={0};
cvInitMatHeader(res,1,1,CV_64FC1,res1);
int resi1,resj1;
double resb1,resr1,rescb1,rescr1,resu1;
for(resi1=0;resi1<height;resi1++)
{
for(resj1=0;resj1<width;resj1++)
{
resb1=data[resi1*step+resj1*channels+2];
resr1=data[resi1*step+resj1*channels+1];
rescb1=resb1-103.0056;
rescr1=resr1-140.1309;
double p1[2]={rescb1,rescr1};
cvInitMatHeader(a,1,2,CV_64FC1,p1);
cvMatMulAdd(a,mat,0,a);
cvInitMatHeader(a1,2,1,CV_64FC1,p1);
cvMatMulAdd(a,a1,0,res);
resu1=CV_MAT_ELEM(*res,double,0,0);
resu1=exp(-0.5*resu1);
cvmSet(max,resi1,resj1,resu1);
}
}
double max1=0;
double mm=0;
cvMinMaxLoc(max,NULL,&max1,NULL,NULL);
for(resi1=0;resi1<height;resi1++)
{
for(resj1=0;resj1<width;resj1++)
{
mm=CV_MAT_ELEM(*max,double,resi1,resj1)/max1;
cvmSet(max,resi1,resj1,mm);
((dst->imageData + dst->widthStep*resi1))[resj1]=255*mm;
}
}
cvThreshold(dst,dst,250,255,CV_THRESH_BINARY);
補充說明(Supplement):
上面這段程式如果再Win32 主控台應用程式搭配OpenCV(未加綠色括號)執行速度非常的快;
可是在windows form裡面(未加綠色括號)執行速度有點慢,大概要執行10秒左右。
順便想請問一下,深綠色的大括號加與不加對程式速度影響非常大,這是為什麼呢?
加與不加的差別是在哪裡呢?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 120.96.224.186
推
05/10 00:41, , 1F
05/10 00:41, 1F
推
05/10 01:30, , 2F
05/10 01:30, 2F
→
05/10 01:30, , 3F
05/10 01:30, 3F
→
05/10 01:31, , 4F
05/10 01:31, 4F
括號那個問題是我"順便"想請問的,因為我的習慣都會加上括號...
可是加上之後速度整個變很慢,我也是覺得括號有加跟沒加應該是沒差
但是,"主要"問題還是想請問環境轉換執行速度差異很大,這是為什麼呢?
※ 編輯: pttdna 來自: 111.249.0.172 (05/10 12:25)