Opencv图像处理:如何判断图片里某个颜色值占的比例


Posted in Python onJune 03, 2020

一、功能

这里的需求是,判断摄像头有没有被物体遮挡。这里只考虑用手遮挡---->判断黑色颜色的范围。

二、使用OpenCV的Mat格式图片遍历图片

下面代码里,传入的图片的尺寸是640*480,判断黑色范围。

/*
在图片里查找指定颜色的比例
*/
int Widget::Mat_color_Find(QImage qimage)
{
  Mat image = QImage2cvMat(qimage);//将图片加载进来
  int num = 0;//记录颜色的像素点
  float rate;//要计算的百分率
  //遍历图片的每一个像素点
  for(int i = 0; i < image.rows;i++) //行数
  {
   for(int j = 0; j <image.cols;j++) //列数
   {
    //对该像素是否为指定颜色进行判断 BGR 像素点
    //OpenCV 中 MAT类的默认三原色通道顺序BGR
    /*
   动态地址访问像素语法:image.at<Vec3b>(i,j)[0]、image.at<uchar>(i, j)
   访问三通道图像的单个像素:
   int b = image.at<Vec3b>(i, j)[0];
   int g = image.at<Vec3b>(i, j)[1];
   int r = image.at<Vec3b>(i, j)[2];
   对于三通道图像,每个像素存储了三个值,分别为蓝色、绿色、红色通道上的数值。
   int gray_data = image.at<uchar>(i, j);
   用来访问灰度图像的单个像素。对于灰度图像,每个像素只存储一个值
   */
    if((image.at<Vec3b>(i, j)[0] <= 120 &&
     image.at<Vec3b>(i, j)[1] <= 120 &&
     image.at<Vec3b>(i, j)[2] <= 120))
    {
     num++;
    }
   }
  }
  rate = (float)num / (float)(image.rows * image.cols);
 
  //阀值为 0.249255 表示为全黑
  if(rate>0.20)
  {
   qDebug()<<":Mat:故意遮挡摄像头";
  }
  qDebug()<<"Mat:比例"<<rate;
  return 0;
}
 
 
Mat Widget::QImage2cvMat(QImage image)
{
 Mat mat;
 switch(image.format())
 {
 case QImage::Format_ARGB32:
 case QImage::Format_RGB32:
 case QImage::Format_ARGB32_Premultiplied:
  mat = Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
  break;
 case QImage::Format_RGB888:
  mat = Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
  cvtColor(mat, mat, CV_BGR2RGB);
  break;
 case QImage::Format_Indexed8:
  mat = Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
  break;
 }
 return mat;
}

三、使用QImage遍历像素点

/*
在图片里查找指定颜色的比例
*/
int Widget::qimage_color_Find(QImage qimage)
{
 int num = 0;//记录颜色的像素点
 float rate;//要计算的百分率
 quint8 r,g,b;
 //遍历图片的每一个像素点
 for(int i = 0; i < qimage.height();i++) //行数
 {
  for(int j = 0; j <qimage.width();j++) //列数
  {
   QRgb rgb=qimage.pixel(j,i);
   r=qRed(rgb);
   g=qGreen(rgb);
   b=qBlue(rgb);
 
   if((r <= 120 && g <= 120 && b <= 120))
   {
    num++;
   }
  }
 }
 rate = (float)num / (float)(qimage.height() * qimage.width());
 
 //阀值为 0.99777 表示为全黑
 if(rate>0.60)
 {
   //qDebug()<<"qimage:故意遮挡摄像头";
 }
 qDebug()<<"qimage:比例:"<<rate;
 return 0;
}

补充知识:判断一批图片中含有某中颜色物体的图片个数占总图片的比例

最近在做一个语义分割项目,使用Label工具进行了类别的标注.然后不同类别生成了不同的颜色,如需要代码可以参考.后来我想统计一下含有一种类别的图片和含有两种类别的图片占总图片的比例,下面是我的代码:

代码思路:

1)循环读取文件夹中的图片

2)循环读取图片的每一个像素点,当图片的像素点和你检测的物体像素点一致时,对应类别加1.

3)读取完图片后计算每一类的比例.

import cv2
import os
import matplotlib.pyplot as plt
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
for item in picture_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
  for i in range(height):
    for j in range(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值或者范围
      '''
      if mat[i][j][0]==0 and mat[i][j][1]==0 and mat[i][j][2]==0:
        ground+=1
      elif mat[i][j][0]==128 and mat[i][j][1]==0 and mat[i][j][2]==0:
        zero+=1
      elif mat[i][j][0]==0 and mat[i][j][1]==128 and mat[i][j][2]==0:
        one+=1
      elif mat[i][j][0]==128 and mat[i][j][1]==128 and mat[i][j][2]==0:
        two+=1
      elif mat[i][j][0]==0 and mat[i][j][1]==0 and mat[i][j][2]==128:
        three+=1
      elif mat[i][j][0]==128 and mat[i][j][1]==0 and mat[i][j][2]==128:
        four+=1
      elif mat[i][j][0]==0 and mat[i][j][1]==128 and mat[i][j][2]==128:
        five+=1
      elif mat[i][j][0]==128 and mat[i][j][1]==128 and mat[i][j][2]==128:
        six+=1
      elif mat[i][j][0]==0 and mat[i][j][1]==0 and mat[i][j][2]==64:
        seven+=1
      elif mat[i][j][0]==0 and mat[i][j][1]==0 and mat[i][j][2]==192:
        eight+=1
      else:
        print("输入正确的图片,或者更改上面判断条件的像素值")
  rateground=ground/(height*width)
  rate0=zero/(height*width)
  if rate0!=0:
    road_type+=1
  rate1=one/(height*width)
  if rate1!=0:
    road_type+=1
  rate2=two/(height*width)
  if rate2!=0:
    road_type+=1
  rate3=three/(height*width)
  if rate3!=0:
    road_type+=1
  rate4=four/(height*width)
  if rate4!=0:
    road_type+=1
  rate5=five/(height*width)
  if rate5!=0:
    road_type+=1
  rate6=six/(height*width)
  if rate6!=0:
    road_type+=1
  rate7=seven/(height*width)
  if rate7!=0:
    road_type+=1
  rate8=eight/(height*width)
  if rate8!=0:
    road_type+=1
  rate=rate0+rate1+rate2+rate3+rate4+rate5+rate6+rate7+rate8
  per.append(rate)
  if road_type==1:
    number+=1
    if rate<0.0638:
      number1+=1#一种类型道路并且所占比例小于0.0638的情况 
  else:
    if rate<0.532:
      number2+=1#两种道路类型,并且正确正确道路类型所占比例小于0.532时的个数
  print("the remaining %d"%total_picture)
A=number/total#图片中道路类型大于1种的概率
A1=number1/total#图片中一种道路类型并且比例小于0.0638的概率
A2=number2/total#图片中有两种道路,并且一种道路所占比例小于0.532时的概率
print("A1:%s"%A1)
print("the precentage of one road is %s"%A)
print("the precentage of two road is %s"%(1-A))
print("A2:%s"%A2)
plt.plot(per)
plt.ylabel('the percentage of road')
plt.show()

以上这篇Opencv图像处理:如何判断图片里某个颜色值占的比例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python专用方法与迭代机制实例分析
Sep 15 Python
零基础写python爬虫之使用urllib2组件抓取网页内容
Nov 04 Python
python日志记录模块实例及改进
Feb 12 Python
python读取和保存视频文件
Apr 16 Python
python3+PyQt5使用数据库窗口视图
Apr 24 Python
python文件拆分与重组实例
Dec 10 Python
零基础使用Python读写处理Excel表格的方法
May 02 Python
Python 通过截图匹配原图中的位置(opencv)实例
Aug 27 Python
python中的Elasticsearch操作汇总
Oct 30 Python
Python通过VGG16模型实现图像风格转换操作详解
Jan 16 Python
python中常见错误及解决方法
Jun 21 Python
python批量修改文件名的示例
Sep 27 Python
QML用PathView实现轮播图
Jun 03 #Python
Python基于smtplib协议实现发送邮件
Jun 03 #Python
Pytorch环境搭建与基本语法
Jun 03 #Python
如何学习Python time模块
Jun 03 #Python
使用openCV去除文字中乱入的线条实例
Jun 02 #Python
Python能做什么
Jun 02 #Python
什么是Python中的匿名函数
Jun 02 #Python
You might like
第二节--PHP5 的对象模型
2006/11/16 PHP
基于php验证码函数的使用示例
2013/05/03 PHP
PHP转盘抽奖接口实例
2015/02/09 PHP
php mysqli查询语句返回值类型实例分析
2016/06/29 PHP
PHP实现RTX发送消息提醒的实例代码
2017/01/03 PHP
laravel实现一个上传图片的接口,并建立软链接,访问图片的方法
2019/10/12 PHP
JS弹出层单纯的绝对定位居中示例代码
2014/02/18 Javascript
谷歌浏览器不支持showModalDialog模态对话框的解决方法
2014/09/22 Javascript
JavaScript中的数组遍历forEach()与map()方法以及兼容写法介绍
2016/05/19 Javascript
jQuery插件HighCharts实现2D柱状图、折线图的组合多轴图效果示例【附demo源码下载】
2017/03/09 Javascript
Windows安装Node.js报错:2503、2502的解决方法
2017/10/25 Javascript
Webpack优化配置缩小文件搜索范围
2017/12/25 Javascript
vue通过滚动行为实现从列表到详情,返回列表原位置的方法
2018/08/31 Javascript
基于vue-router 多级路由redirect 重定向的问题
2018/09/03 Javascript
JS事件绑定的常用方式实例总结
2019/03/02 Javascript
解决vux 中popup 组件Mask 遮罩在最上层的问题
2020/11/03 Javascript
Vue3 响应式侦听与计算的实现
2020/11/11 Javascript
[04:49]2014DOTA2国际邀请赛 Newbee顺利挺进总决赛 ImbaTV独家专访
2014/07/19 DOTA
python实现的一个火车票转让信息采集器
2014/07/09 Python
理解Python中函数的参数
2015/04/27 Python
Python装饰器基础详解
2016/03/09 Python
Python制作刷网页流量工具
2017/04/23 Python
关于Python中浮点数精度处理的技巧总结
2017/08/10 Python
Python OpenCV之图片缩放的实现(cv2.resize)
2019/06/28 Python
python如何实现不用装饰器实现登陆器小程序
2019/12/14 Python
计算pytorch标准化(Normalize)所需要数据集的均值和方差实例
2020/01/15 Python
Python使用lambda抛出异常实现方法解析
2020/08/20 Python
利用CSS3的transition属性实现滑动效果
2015/08/05 HTML / CSS
急诊科护士自我鉴定
2013/10/14 职场文书
英文简历中的自我评价用语
2013/12/09 职场文书
国家励志奖学金个人先进事迹材料
2014/05/04 职场文书
教师考察材料范文
2014/06/03 职场文书
真诚的求职信
2014/07/04 职场文书
聘任协议书(挂靠)
2015/09/21 职场文书
四年级作文之植物
2019/09/20 职场文书
提高系统的吞吐量解决数据库重复写入问题
2022/04/23 MySQL