参数说明:
IplImage *workImg-当前全局变量,表示正在显示的图片。
downleft, upright- 检测出的阴影部分矩形框的两个对角顶点。
- /*********************************************/
- //阴影检测
- /*********************************************/
- CvPoint downleft,upright;
- int cnt;
- ][]={-,-,-,,-,,,,,-,,,,,,-};
- #define SHADOW 170
- #define Thres_KindNumber 20
- bool InRange(CvPoint point,IplImage* pi)
- {
- int w=pi->width;
- int h=pi->height;
- &&point.x<w&&point.y>=&&point.y<h)
- {
- ];
- ;i<;i++)
- {
- v[i]=((uchar*)(pi->imageData + pi->widthStep*point.y))[point.x*+i];
- if(v[i]<=SHADOW)
- return true;
- }
- }
- return false;
- }
- void Dye(IplImage** curimg,CvPoint s)
- {
- int i;
- queue<CvPoint>Q;
- Q.push(s);
- ;i<;i++)
- ((uchar*)((*curimg)->imageData + (*curimg)->widthStep*s.y))[s.x*+i]=SHADOW+;
- while(!Q.empty())
- {
- s=Q.front();
- Q.pop();
- if(s.x<downleft.x) downleft.x=s.x;
- if(s.y<downleft.y) downleft.y=s.y;
- if(s.x>upright.x) upright.x=s.x;
- if(s.y>upright.y) upright.y=s.y;
- //dye around
- ;i<;i++)
- {
- CvPoint now=cvPoint(s.x+dir[i][],s.y+dir[i][]);
- if(InRange(now,*curimg))
- {
- Q.push(now);
- cnt++;
- ;i<;i++)
- ((uchar*)((*curimg)->imageData + (*curimg)->widthStep*now.y))[now.x*+i]=SHADOW+;
- }
- }
- }
- }
- void CCVMFCView::OnShadowDetect()
- {
- //detect shadows,find the region with highest pixel value
- int x,y;
- srcimg=workImg;
- ;y<srcimg->height;y++)
- ;x<srcimg->width;x++)
- {
- CvPoint curp=cvPoint(x,y);
- downleft.x=srcimg->width;downleft.y=srcimg->height;
- upright.x=upright.y=;
- cnt=;
- if(InRange(curp,srcimg))
- Dye(&srcimg,curp);
- if(cnt>Thres_KindNumber)
- cvRectangle(workImg , downleft,upright,CV_RGB(,,),,CV_AA,);
- }
- Invalidate();
- }
from: http://blog.youkuaiyun.com/abcjennifer/article/details/7334043