概述
老师布置了一个任务,说根据程序判断下摄像头有没有坏掉,让我先写两个函数做一个大概,并提出建议说比如画面出现纯色这样摄像头肯定是坏了,再比如很模糊摄像头也可能坏了,我在网上按需求搜了一下,然后自己 改了下,运行没有错误,我把代码贴出来希望对大家有帮助。
第一个是检测图片纯色的:
//图片颜色最大值与最小值之差小于3,则返回1 否则返回0 输入为灰度图
int VideoColorDetect(IplImage* src)
{
//图片高和宽
int width = src->width;
int height = src->height;
uchar *ptr = (uchar*)src->imageData;
int step = src->widthStep/sizeof(uchar);
int Iij = 0;
int Imax = 0, Imin = 255;
//遍历图片像素 取最大值,最小值
for(int i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{
Iij = (int) ptr[i*step+j];
if(Iij > Imax)
Imax = Iij;
if(Iij < Imin)
Imin = Iij;
// Iave = Iave + Iij;
}
}
//判断条件 根据实际情况调试
if ((Imax-Imin)<3)
{
return 1;
}else{
return 0;
}
}
第二个是检测图像模糊度的:
//检测模糊度 值越大越模糊 输入为灰度图 具体阈值根据实际情况决定
int VideoBlurDetect(IplImage* data)
{
//图片每行字节数及高
int width=data->widthStep;
int height=data->height;
ushort* sobelTable = new ushort[width*height];
memset(sobelTable, 0, width*height*sizeof(ushort));
int i, j, mul;
//指向图像首地址
uchar* udata = (uchar*)data->imageData;
for(i = 1, mul = i*width; i < height - 1; i++, mul += width)
for(j = 1; j < width - 1; j++)
sobelTable[mul+j]=abs(udata[mul+j-width-1] + 2*udata[mul+j-1] + udata[mul+j-1+width] -
udata[mul+j+1-width] - 2*udata[mul+j+1] - udata[mul+j+width+1]);
for(i = 1, mul = i*width; i < height - 1; i++, mul += width)
for(j = 1; j < width - 1; j++)
if(sobelTable[mul+j] < 50 || sobelTable[mul+j] <= sobelTable[mul+j-1] ||
sobelTable[mul+j] <= sobelTable[mul+j+1]) sobelTable[mul+j] = 0;
int totLen = 0;
int totCount = 1;
uchar suddenThre = 50;
uchar sameThre = 3;
//遍历图片
for(i = 1, mul = i*width; i < height - 1; i++, mul += width)
{
for(j = 1; j < width - 1; j++)
{
if(sobelTable[mul+j])
{
int count = 0;
uchar tmpThre = 5;
uchar max = udata[mul+j] > udata[mul+j-1] ? 0 : 1;
for(int t = j; t > 0; t--)
{
count++;
if(abs(udata[mul+t] - udata[mul+t-1]) > suddenThre)
break;
if(max && udata[mul+t] > udata[mul+t-1])
break;
if(!max && udata[mul+t] < udata[mul+t-1])
break;
int tmp = 0;
for(int s = t; s > 0; s--)
{
if(abs(udata[mul+t] - udata[mul+s]) < sameThre)
{
tmp++;
if(tmp > tmpThre) break;
}
else break;
}
if(tmp > tmpThre) break;
}
max = udata[mul+j] > udata[mul+j+1] ? 0 : 1;
for(int t = j; t < width; t++)
{
count++;
if(abs(udata[mul+t] - udata[mul+t+1]) > suddenThre)
break;
if(max && udata[mul+t] > udata[mul+t+1])
break;
if(!max && udata[mul+t] < udata[mul+t+1])
break;
int tmp = 0;
for(int s = t; s < width; s++)
{
if(abs(udata[mul+t] - udata[mul+s]) < sameThre)
{
tmp++;
if(tmp > tmpThre) break;
}
else break;
}
if(tmp > tmpThre) break;
}
count--;
totCount++;
totLen += count;
}
}
}
//模糊度
float result = (float)totLen/totCount;
delete[] sobelTable;
//阈值具体情况 具体定
if(result >5)
{
return 1;
}else{
return 0;
}
}
具体的测试代码我打包了一下,地址是
http://download.csdn.net/detail/zhinengshiyanshi/9326855,大家可以测试一下,其实就多了头文件和main函数,大家可以自己动手试一下。
最后
以上就是英俊雨为你收集整理的基于opencv的摄像头清晰度检测的全部内容,希望文章能够帮你解决基于opencv的摄像头清晰度检测所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复