Opencv图像处理:如何判断图片里某个颜色值占的比例
一、功能
这里的需求是,判断摄像头有没有被物体遮挡。这里只考虑用手遮挡---->判断黑色颜色的范围。
二、使用OpenCV的Mat格式图片遍历图片
下面代码里,传入的图片的尺寸是640*480,判断黑色范围。
/*
在图片里查找指定颜色的比例
*/
intWidget::Mat_color_Find(QImageqimage)
{
Matimage=QImage2cvMat(qimage);//将图片加载进来
intnum=0;//记录颜色的像素点
floatrate;//要计算的百分率
//遍历图片的每一个像素点
for(inti=0;i(i,j)[0]、image.at(i,j)
访问三通道图像的单个像素:
intb=image.at(i,j)[0];
intg=image.at(i,j)[1];
intr=image.at(i,j)[2];
对于三通道图像,每个像素存储了三个值,分别为蓝色、绿色、红色通道上的数值。
intgray_data=image.at(i,j);
用来访问灰度图像的单个像素。对于灰度图像,每个像素只存储一个值
*/
if((image.at(i,j)[0]<=120&&
image.at(i,j)[1]<=120&&
image.at(i,j)[2]<=120))
{
num++;
}
}
}
rate=(float)num/(float)(image.rows*image.cols);
//阀值为0.249255表示为全黑
if(rate>0.20)
{
qDebug()<<":Mat:故意遮挡摄像头";
}
qDebug()<<"Mat:比例"<
三、使用QImage遍历像素点
/*
在图片里查找指定颜色的比例
*/
intWidget::qimage_color_Find(QImageqimage)
{
intnum=0;//记录颜色的像素点
floatrate;//要计算的百分率
quint8r,g,b;
//遍历图片的每一个像素点
for(inti=0;i0.60)
{
//qDebug()<<"qimage:故意遮挡摄像头";
}
qDebug()<<"qimage:比例:"<
补充知识:判断一批图片中含有某中颜色物体的图片个数占总图片的比例
最近在做一个语义分割项目,使用Label工具进行了类别的标注.然后不同类别生成了不同的颜色,如需要代码可以参考.后来我想统计一下含有一种类别的图片和含有两种类别的图片占总图片的比例,下面是我的代码:
代码思路:
1)循环读取文件夹中的图片
2)循环读取图片的每一个像素点,当图片的像素点和你检测的物体像素点一致时,对应类别加1.
3)读取完图片后计算每一类的比例.
importcv2
importos
importmatplotlib.pyplotasplt
picture_path="/home/wsb/桌面/picture"
picture_list=os.listdir(picture_path)
total_picture=len(picture_list)
total=total_picture
per=[]
number=0#图片中道路类型为1的个数
number1=0#一种道路类型并且比例小于0.0638的个数
number2=0
foriteminpicture_list:
src=os.path.join(os.path.abspath(picture_path),item)
print("start:%s"%item)
total_picture-=1
mat=cv2.imread(src)
height=mat.shape[0]
width=mat.shape[1]
ground=0
zero=0
one=0
two=0
three=0
four=0
five=0
six=0
seven=0
eight=0
rateground=0
rate0=0
rate1=0
rate2=0
rate3=0
rate4=0
rate5=0
rate6=0
rate7=0
rate8=0
rate=0
road_type=0
foriinrange(height):
forjinrange(width):
#print("r:%s"%mat[i][j][0])
#print("r:%s"%mat[i][j][1])
#print("r:%s"%mat[i][j][2])
'''
我这里共有9种分类情况,况且我已知道每一种颜色的具体rgb值,我将它们作为我的判断条件
如不你不知道可以在网上查找自己想查看比例的rgb值或者范围
'''
ifmat[i][j][0]==0andmat[i][j][1]==0andmat[i][j][2]==0:
ground+=1
elifmat[i][j][0]==128andmat[i][j][1]==0andmat[i][j][2]==0:
zero+=1
elifmat[i][j][0]==0andmat[i][j][1]==128andmat[i][j][2]==0:
one+=1
elifmat[i][j][0]==128andmat[i][j][1]==128andmat[i][j][2]==0:
two+=1
elifmat[i][j][0]==0andmat[i][j][1]==0andmat[i][j][2]==128:
three+=1
elifmat[i][j][0]==128andmat[i][j][1]==0andmat[i][j][2]==128:
four+=1
elifmat[i][j][0]==0andmat[i][j][1]==128andmat[i][j][2]==128:
five+=1
elifmat[i][j][0]==128andmat[i][j][1]==128andmat[i][j][2]==128:
six+=1
elifmat[i][j][0]==0andmat[i][j][1]==0andmat[i][j][2]==64:
seven+=1
elifmat[i][j][0]==0andmat[i][j][1]==0andmat[i][j][2]==192:
eight+=1
else:
print("输入正确的图片,或者更改上面判断条件的像素值")
rateground=ground/(height*width)
rate0=zero/(height*width)
ifrate0!=0:
road_type+=1
rate1=one/(height*width)
ifrate1!=0:
road_type+=1
rate2=two/(height*width)
ifrate2!=0:
road_type+=1
rate3=three/(height*width)
ifrate3!=0:
road_type+=1
rate4=four/(height*width)
ifrate4!=0:
road_type+=1
rate5=five/(height*width)
ifrate5!=0:
road_type+=1
rate6=six/(height*width)
ifrate6!=0:
road_type+=1
rate7=seven/(height*width)
ifrate7!=0:
road_type+=1
rate8=eight/(height*width)
ifrate8!=0:
road_type+=1
rate=rate0+rate1+rate2+rate3+rate4+rate5+rate6+rate7+rate8
per.append(rate)
ifroad_type==1:
number+=1
ifrate<0.0638:
number1+=1#一种类型道路并且所占比例小于0.0638的情况
else:
ifrate<0.532:
number2+=1#两种道路类型,并且正确正确道路类型所占比例小于0.532时的个数
print("theremaining%d"%total_picture)
A=number/total#图片中道路类型大于1种的概率
A1=number1/total#图片中一种道路类型并且比例小于0.0638的概率
A2=number2/total#图片中有两种道路,并且一种道路所占比例小于0.532时的概率
print("A1:%s"%A1)
print("theprecentageofoneroadis%s"%A)
print("theprecentageoftworoadis%s"%(1-A))
print("A2:%s"%A2)
plt.plot(per)
plt.ylabel('thepercentageofroad')
plt.show()
以上这篇Opencv图像处理:如何判断图片里某个颜色值占的比例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。