基于opencv的摄像头清晰度检测

来源:互联网 发布:淘宝新店有流量扶持吗 编辑:程序博客网 时间:2024/04/30 13:52

老师布置了一个任务,说根据程序判断下摄像头有没有坏掉,让我先写两个函数做一个大概,并提出建议说比如画面出现纯色这样摄像头肯定是坏了,再比如很模糊摄像头也可能坏了,我在网上按需求搜了一下,然后自己 改了下,运行没有错误,我把代码贴出来希望对大家有帮助。

第一个是检测图片纯色的:

//图片颜色最大值与最小值之差小于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函数,大家可以自己动手试一下。

3 0