#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <iostream>
#include "highgui.h"
#include "cvaux.h"
IplImage* praw;
//检测模糊度 值越大越模糊 输入为灰度图 具体阈值根据实际情况决定
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;
}
}
//颜色一样返回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;
}
}
void main()
{
IplImage* praw=cvLoadImage("nongfu.png",CV_LOAD_IMAGE_GRAYSCALE);
int fuzzy = VideoBlurDetect(praw);
int flag = VideoColorDetect(praw);
printf("diff = %d\n",fuzzy);
printf("diff = %d\n",flag);
cvNamedWindow("Example",CV_WINDOW_AUTOSIZE);
cvShowImage("Example",praw);
cvWaitKey(0);
cvReleaseImage(&praw);
cvDestroyWindow("Example");
}
基于opencv的摄像头清晰度检测
2星 需积分: 44 188 浏览量
2015-12-04
17:34:37
上传
评论 4
收藏 640KB ZIP 举报
嘿拜
- 粉丝: 35
- 资源: 2
最新资源
- Fortran语言教程,详细地介绍了Fortran语言
- (函数)图论中最短路径计算D算法MATLAB源代码,修改网络上D算法的错误,并编写通用的MATLAB函数.rar
- 基于matlab 2Dijkstra最短路径算法的matlab程序,希望对大家有所帮助.zip
- python入门学习,基础语法,用法等.zip
- Swift代码转换指南(Swift Swift Code Convension Guide .)
- Python入门到精通.zip
- 基于QT+C++开发的炫酷九宫格主界面+源码
- python入门代码测试.zip
- 一种基于Python实现中文编码格式转换工具的源码
- 基于matlab dijkstra算法,可以求解图论中的两点间最短路径.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页