温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

opencv平均背景法详解

发布时间:2020-09-22 04:49:32 来源:脚本之家 阅读:188 作者:元气少女缘结神 栏目:编程语言

本文实例为大家分享了opencv平均背景法的具体代码,供大家参考,具体内容如下

#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
IplImage *IavgF, *IdiffF, *IprevF, *IhiF, *IlowF;
IplImage *Iscratch, *Iscratch3;
IplImage *Igray1, *Igray2, *Igray3;
IplImage *Ilow1, *Ilow2, *Ilow3;
IplImage *Ihi1, *Ihi2, *Ihi3;
IplImage *Imaskt;
float Icount;
void AllocateImages(IplImage* I){
 CvSize sz = cvGetSize(I);
 IavgF = cvCreateImage(sz, IPL_DEPTH_32F, 3);
 IdiffF = cvCreateImage(sz, IPL_DEPTH_32F, 3);
 IprevF = cvCreateImage(sz, IPL_DEPTH_32F, 3);
 IhiF = cvCreateImage(sz, IPL_DEPTH_32F, 3);
 IlowF = cvCreateImage(sz, IPL_DEPTH_32F, 3);
 Ilow1 = cvCreateImage(sz, IPL_DEPTH_32F, 1);
 Ilow2 = cvCreateImage(sz, IPL_DEPTH_32F, 1);
 Ilow3 = cvCreateImage(sz, IPL_DEPTH_32F, 1);
 Ihi1 = cvCreateImage(sz, IPL_DEPTH_32F, 1);
 Ihi2 = cvCreateImage(sz, IPL_DEPTH_32F, 1);
 Ihi3 = cvCreateImage(sz, IPL_DEPTH_32F, 1);
 cvZero(IavgF);
 cvZero(IdiffF);
 cvZero(IprevF);
 cvZero(IhiF);
 cvZero(IlowF);
 Icount = 0.00001;
 Iscratch = cvCreateImage(sz, IPL_DEPTH_32F, 3);
 Iscratch3 = cvCreateImage(sz, IPL_DEPTH_32F, 3);
 Igray1 = cvCreateImage(sz, IPL_DEPTH_32F, 1);
 Igray2 = cvCreateImage(sz, IPL_DEPTH_32F, 1);
 Igray3 = cvCreateImage(sz, IPL_DEPTH_32F, 1);
 Imaskt = cvCreateImage(sz, IPL_DEPTH_8U, 1);
 cvZero(Iscratch);
 cvZero(Iscratch3);
}
void accumulateBackground(IplImage *I){
 static int first = 1;
 cvCvtScale(I, Iscratch, 1, 0);
 if (!first){
 cvAcc(Iscratch, IavgF);
 cvAbsDiff(Iscratch, IprevF, Iscratch3);
 cvAcc(Iscratch3, IdiffF);
 Icount += 1.0;
 }
 first = 0;
 cvCopy(Iscratch, IprevF);
}
void setHighThreshold(float scale){
 cvConvertScale(IdiffF, Iscratch, scale);
 cvAdd(Iscratch, IavgF, IhiF);
 cvSplit(IhiF, Ihi1, Ihi2, Ihi3, 0);
}
void setLowThreshold(float scale){
 cvConvertScale(IdiffF, Iscratch, scale);
 cvAdd(IavgF, Iscratch, IlowF);
 cvSplit(IlowF,Ilow1,Ilow2,Ilow3, 0);
}
 
void createModelsfromStats(){
 cvConvertScale(IavgF, IavgF, (double)(1.0 / Icount));
 cvConvertScale(IdiffF, IdiffF, (double)(1.0 / Icount));
 cvAddS(IdiffF, cvScalar(1.0, 1.0, 1.0), IdiffF);
 setHighThreshold(10.0);
 setLowThreshold(4.0);
}
void backgroundDiff(IplImage* I, IplImage* Imask){
 cvCvtScale(I, Iscratch, 1, 0);
 cvSplit(Iscratch, Igray1, Igray2, Igray3, 0);
 cvInRange(Igray1, Ilow1, Ihi1, Imask);
 cvInRange(Igray2, Ilow2, Ihi2, Imaskt);
 cvOr(Imask, Imaskt, Imask);
 cvInRange(Igray3, Ilow3, Ihi3, Imaskt);
 cvOr(Imask, Imaskt, Imask);
 cvSubRS(Imask, Scalar(255), Imask);
}
void DeallocateImages(){
 cvReleaseImage(&IavgF);
 cvReleaseImage(&IdiffF);
 cvReleaseImage(&IprevF);
 cvReleaseImage(&IhiF);
 cvReleaseImage(&IlowF);
 cvReleaseImage(&Ilow1);
 cvReleaseImage(&Ilow2);
 cvReleaseImage(&Ilow3);
 cvReleaseImage(&Ihi1);
 cvReleaseImage(&Ihi2);
 cvReleaseImage(&Ihi3);
 cvReleaseImage(&Iscratch);
 cvReleaseImage(&Iscratch3);
 cvReleaseImage(&Igray1);
 cvReleaseImage(&Igray2);
 cvReleaseImage(&Igray3);
 cvReleaseImage(&Imaskt);
}
 
char filename[100];
char newcontour[100];
void main()
{
 TickMeter tm;
 tm.start();
 //many imgs
 IplImage* src = cvLoadImage("待处理背面图\\55124.bmp");
 AllocateImages(src);
 for (int i = 55124; i <= 56460; i++)
 {
 sprintf(filename, "待处理背面图\\%d.bmp", i);
 sprintf(newcontour, "分割前景\\%d.bmp", i);
 IplImage* src_ipl = cvLoadImage(filename);
 accumulateBackground(src_ipl);
 createModelsfromStats();
 CvSize sz = cvGetSize(src_ipl);
 IplImage* myImask = cvCreateImage(sz, IPL_DEPTH_8U, 1);;
 backgroundDiff(src_ipl, myImask);
 cvSaveImage(newcontour, myImask);
 }
 DeallocateImages();
 tm.stop();
 cout << "count=" << tm.getCounter() << ",process time=" << tm.getTimeMilli() << endl;
}

然而对我的图还是不适合分割出轮廓:

opencv平均背景法详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI