opencv 图像阴影检测
2012-09-02 14:37
218 查看
源文:/article/1821012.html
参数说明:
IplImage *workImg-当前全局变量,表示正在显示的图片。
downleft, upright- 检测出的阴影部分矩形框的两个对角顶点。
[cpp] view
plaincopy
/*********************************************/
//阴影检测
/*********************************************/
CvPoint downleft,upright;
int cnt;
int dir[8][2]={-1,-1,-1,0,-1,1,0,1,0,-1,1,1,1,0,1,-1};
#define SHADOW 170
#define Thres_KindNumber 20
bool InRange(CvPoint point,IplImage* pi)
{
int w=pi->width;
int h=pi->height;
if(point.x>=0&&point.x<w&&point.y>=0&&point.y<h)
{
float v[3];
for(i=0;i<3;i++)
{
v[i]=((uchar*)(pi->imageData + pi->widthStep*point.y))[point.x*3+i];
if(v[i]<=SHADOW)
return true;
}
}
return false;
}
void Dye(IplImage** curimg,CvPoint s)
{
int i;
queue<CvPoint>Q;
Q.push(s);
for(i=0;i<3;i++)
((uchar*)((*curimg)->imageData + (*curimg)->widthStep*s.y))[s.x*3+i]=SHADOW+10;
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
for(i=0;i<8;i++)
{
CvPoint now=cvPoint(s.x+dir[i][0],s.y+dir[i][1]);
if(InRange(now,*curimg))
{
Q.push(now);
cnt++;
for(i=0;i<3;i++)
((uchar*)((*curimg)->imageData + (*curimg)->widthStep*now.y))[now.x*3+i]=SHADOW+10;
}
}
}
}
void CCVMFCView::OnShadowDetect()
{
//detect shadows,find the region with highest pixel value
int x,y;
srcimg=workImg;
for(y=0;y<srcimg->height;y++)
for(x=0;x<srcimg->width;x++)
{
CvPoint curp=cvPoint(x,y);
downleft.x=srcimg->width;downleft.y=srcimg->height;
upright.x=upright.y=0;
cnt=0;
if(InRange(curp,srcimg))
Dye(&srcimg,curp);
if(cnt>Thres_KindNumber)
cvRectangle(workImg , downleft,upright,CV_RGB(0,255,0),1,CV_AA,0);
}
Invalidate();
}
参数说明:
IplImage *workImg-当前全局变量,表示正在显示的图片。
downleft, upright- 检测出的阴影部分矩形框的两个对角顶点。
[cpp] view
plaincopy
/*********************************************/
//阴影检测
/*********************************************/
CvPoint downleft,upright;
int cnt;
int dir[8][2]={-1,-1,-1,0,-1,1,0,1,0,-1,1,1,1,0,1,-1};
#define SHADOW 170
#define Thres_KindNumber 20
bool InRange(CvPoint point,IplImage* pi)
{
int w=pi->width;
int h=pi->height;
if(point.x>=0&&point.x<w&&point.y>=0&&point.y<h)
{
float v[3];
for(i=0;i<3;i++)
{
v[i]=((uchar*)(pi->imageData + pi->widthStep*point.y))[point.x*3+i];
if(v[i]<=SHADOW)
return true;
}
}
return false;
}
void Dye(IplImage** curimg,CvPoint s)
{
int i;
queue<CvPoint>Q;
Q.push(s);
for(i=0;i<3;i++)
((uchar*)((*curimg)->imageData + (*curimg)->widthStep*s.y))[s.x*3+i]=SHADOW+10;
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
for(i=0;i<8;i++)
{
CvPoint now=cvPoint(s.x+dir[i][0],s.y+dir[i][1]);
if(InRange(now,*curimg))
{
Q.push(now);
cnt++;
for(i=0;i<3;i++)
((uchar*)((*curimg)->imageData + (*curimg)->widthStep*now.y))[now.x*3+i]=SHADOW+10;
}
}
}
}
void CCVMFCView::OnShadowDetect()
{
//detect shadows,find the region with highest pixel value
int x,y;
srcimg=workImg;
for(y=0;y<srcimg->height;y++)
for(x=0;x<srcimg->width;x++)
{
CvPoint curp=cvPoint(x,y);
downleft.x=srcimg->width;downleft.y=srcimg->height;
upright.x=upright.y=0;
cnt=0;
if(InRange(curp,srcimg))
Dye(&srcimg,curp);
if(cnt>Thres_KindNumber)
cvRectangle(workImg , downleft,upright,CV_RGB(0,255,0),1,CV_AA,0);
}
Invalidate();
}
相关文章推荐
- opencv 图像阴影检测
- opencv 图像阴影检测
- opencv 图像阴影检测
- 图像边缘检测--OpenCV之cvCanny函数
- 图像边缘检测--OpenCV之cvCanny函数
- OpenCV之图像轮廓检测及提取
- OpenCV图像处理车牌检测与定位应用
- opencv图像处理6--网球检测
- 使用OpenCV对图像作边缘检测(Canny、Sobel、Laplace)
- 关于OpenCv图像变换与基本图形检测
- OpenCV之彩色直方图反向投影(图像相似性检测)
- Python-OpenCV 处理图像(五):图像中边界和轮廓检测
- 将图像扭正确。利用OpenCV检测图像中的长方形画布或纸张并提取图像内容
- android openCV检测图像的基本特征,包括Canny边缘检测、Harris角点检测、霍夫直线检测-基于Android studio
- 图像特征检测描述(一):SIFT、SURF、ORB、HOG、LBP特征的原理概述及OpenCV代码实现
- Opencv 利用分割的图像掩膜来完善检测前景
- OPENCV图像轮廓检测
- opencv图像处理6--网球检测
- opencv2对于读书笔记——背投影图像的直方图来检测待处理的内容
- 四.使用OpenCv采集图像及图像边缘检测