Opencv圖像處理:如何判斷圖片里某個(gè)顏色值占的比例
一、功能
這里的需求是,判斷攝像頭有沒(méi)有被物體遮擋。這里只考慮用手遮擋---->判斷黑色顏色的范圍。
二、使用OpenCV的Mat格式圖片遍歷圖片
下面代碼里,傳入的圖片的尺寸是640*480,判斷黑色范圍。
/*
在圖片里查找指定顏色的比例
*/
int Widget::Mat_color_Find(QImage qimage)
{
Mat image = QImage2cvMat(qimage);//將圖片加載進(jìn)來(lái)
int num = 0;//記錄顏色的像素點(diǎn)
float rate;//要計(jì)算的百分率
//遍歷圖片的每一個(gè)像素點(diǎn)
for(int i = 0; i < image.rows;i++) //行數(shù)
{
for(int j = 0; j <image.cols;j++) //列數(shù)
{
//對(duì)該像素是否為指定顏色進(jìn)行判斷 BGR 像素點(diǎn)
//OpenCV 中 MAT類(lèi)的默認(rèn)三原色通道順序BGR
/*
動(dòng)態(tài)地址訪問(wèn)像素語(yǔ)法:image.at<Vec3b>(i,j)[0]、image.at<uchar>(i, j)
訪問(wèn)三通道圖像的單個(gè)像素:
int b = image.at<Vec3b>(i, j)[0];
int g = image.at<Vec3b>(i, j)[1];
int r = image.at<Vec3b>(i, j)[2];
對(duì)于三通道圖像,每個(gè)像素存儲(chǔ)了三個(gè)值,分別為藍(lán)色、綠色、紅色通道上的數(shù)值。
int gray_data = image.at<uchar>(i, j);
用來(lái)訪問(wèn)灰度圖像的單個(gè)像素。對(duì)于灰度圖像,每個(gè)像素只存儲(chǔ)一個(gè)值
*/
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遍歷像素點(diǎn)
/*
在圖片里查找指定顏色的比例
*/
int Widget::qimage_color_Find(QImage qimage)
{
int num = 0;//記錄顏色的像素點(diǎn)
float rate;//要計(jì)算的百分率
quint8 r,g,b;
//遍歷圖片的每一個(gè)像素點(diǎn)
for(int i = 0; i < qimage.height();i++) //行數(shù)
{
for(int j = 0; j <qimage.width();j++) //列數(shù)
{
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;
}
補(bǔ)充知識(shí):判斷一批圖片中含有某中顏色物體的圖片個(gè)數(shù)占總圖片的比例
最近在做一個(gè)語(yǔ)義分割項(xiàng)目,使用Label工具進(jìn)行了類(lèi)別的標(biāo)注.然后不同類(lèi)別生成了不同的顏色,如需要代碼可以參考.后來(lái)我想統(tǒng)計(jì)一下含有一種類(lèi)別的圖片和含有兩種類(lèi)別的圖片占總圖片的比例,下面是我的代碼:
代碼思路:
1)循環(huán)讀取文件夾中的圖片
2)循環(huán)讀取圖片的每一個(gè)像素點(diǎn),當(dāng)圖片的像素點(diǎn)和你檢測(cè)的物體像素點(diǎn)一致時(shí),對(duì)應(yīng)類(lèi)別加1.
3)讀取完圖片后計(jì)算每一類(lèi)的比例.
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#圖片中道路類(lèi)型為1的個(gè)數(shù)
number1=0#一種道路類(lèi)型并且比例小于0.0638的個(gè)數(shù)
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種分類(lèi)情況,況且我已知道每一種顏色的具體rgb值,我將它們作為我的判斷條件
如不你不知道可以在網(wǎng)上查找自己想查看比例的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#一種類(lèi)型道路并且所占比例小于0.0638的情況
else:
if rate<0.532:
number2+=1#兩種道路類(lèi)型,并且正確正確道路類(lèi)型所占比例小于0.532時(shí)的個(gè)數(shù)
print("the remaining %d"%total_picture)
A=number/total#圖片中道路類(lèi)型大于1種的概率
A1=number1/total#圖片中一種道路類(lèi)型并且比例小于0.0638的概率
A2=number2/total#圖片中有兩種道路,并且一種道路所占比例小于0.532時(shí)的概率
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圖像處理:如何判斷圖片里某個(gè)顏色值占的比例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python+OpenCV圖像處理——圖像二值化的實(shí)現(xiàn)
- Opencv圖像處理之詳解掩膜mask
- Python+OpenCV圖像處理——實(shí)現(xiàn)直線檢測(cè)
- 詳解python opencv、scikit-image和PIL圖像處理庫(kù)比較
- OpenCV圖像處理之常見(jiàn)的圖像灰度變換
- Python+OpenCV數(shù)字圖像處理之ROI區(qū)域的提取
- Opencv圖像處理之輪廓外背景顏色改變
- Python+OpenCV圖像處理—— 色彩空間轉(zhuǎn)換
- Python+OpenCV圖像處理——實(shí)現(xiàn)輪廓發(fā)現(xiàn)
- OpenCV+Qt實(shí)現(xiàn)圖像處理操作
相關(guān)文章
python制作一個(gè)簡(jiǎn)單的gui 數(shù)據(jù)庫(kù)查詢(xún)界面
這篇文章主要介紹了python制作一個(gè)簡(jiǎn)單的gui 數(shù)據(jù)庫(kù)查詢(xún)界面,幫助大家更好的理解和學(xué)習(xí)python tkinter的使用,感興趣的朋友可以了解下2020-11-11
Python3實(shí)現(xiàn)將本地JSON大數(shù)據(jù)文件寫(xiě)入MySQL數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Python3實(shí)現(xiàn)將本地JSON大數(shù)據(jù)文件寫(xiě)入MySQL數(shù)據(jù)庫(kù)的方法,涉及Python針對(duì)json大數(shù)據(jù)文件的逐行讀取、mysql數(shù)據(jù)庫(kù)寫(xiě)入等相關(guān)操作技巧,需要的朋友可以參考下2018-06-06
Python光學(xué)仿真光的偏振編程理解學(xué)習(xí)
這篇文章主要為大家介紹了通過(guò)Python光學(xué)仿真來(lái)理解光的偏振編程學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10
anaconda中Conda創(chuàng)建虛擬環(huán)境的實(shí)現(xiàn)步驟
在Anaconda中,可以使用conda命令來(lái)創(chuàng)建和管理虛擬環(huán)境,本文主要介紹了anaconda中Conda創(chuàng)建虛擬環(huán)境的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
Jupyter Notebook切換conda虛擬環(huán)境的實(shí)現(xiàn)步驟
本文主要介紹了Jupyter Notebook切換conda虛擬環(huán)境的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
深入理解NumPy簡(jiǎn)明教程---數(shù)組2
這篇文章主要介紹了深入理解NumPy簡(jiǎn)明教程---數(shù)組2,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-12-12
python 剪切移動(dòng)文件的實(shí)現(xiàn)代碼
移動(dòng)復(fù)制文件通過(guò)os.rename方法,先進(jìn)行文件是否存在判斷,如需更加復(fù)雜相同文件判斷可以根據(jù)文件屬性進(jìn)行判斷,此處只使用同名檢查,并刪除已存在文件,來(lái)實(shí)現(xiàn)覆蓋。這篇文章主要介紹了python 剪切移動(dòng)文件的實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-08-08
python如何在pygame中設(shè)置字體并顯示中文詳解
再簡(jiǎn)單的游戲界面中均涉及文字處理,下面這篇文章主要給大家介紹了關(guān)于python如何在pygame中設(shè)置字體并顯示中文的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01

