基於顏色特徵提取的數字識別

2021-08-18 04:06:53 字數 3602 閱讀 7119

最近因為畢業課題的需要,初學opencv

,鑽研一周,終於有一點點小小的收穫。

編譯環境:vs2013+opencv.4.9

任務:攝像頭識別電梯中已亮的按鈕數字

目的:框選圖中已亮的3和5

號按鈕。話不多說,上**。

//rgb直方圖

//把r

通道分為32個

bin 

int rbins = 32; 

//int histsize = ;  

int histsize = ;

//r的取值範圍 

0-255  

float rranges = ;

//const float* ranges  = ;  

const float* ranges = ;

ma*** hist;

//我們根據影象第乙個通道一維直方圖  

int channels = ;

calchist(&frame, 1, channels, mat(), hist, 1, histsize, ranges, true, false);

//輸出直方圖 

cout <

矩陣值

//對上述矩陣處理得到我們需要的矩陣區間,

處理目的是找出矩陣空間的陡變區間

int one,two,m;

int j;

float k[27];

for (int i = 5,j=0; i 

float a = max(hist.at(i, 0), hist.at(i + 1, 0));

float b = min(hist.at(i, 0), hist.at(i + 1, 0));

float c = a / b;

k[j] = c;

//cout <

cout <

j++;

for ( one = 0, m = 0; m 

if (k[one] 

one = m;

for ( two = 0,m= 0; m 

{   if (k[two]two = m;

one = max(one, two);

two = min(one, two);

cout <

int lowb = 8*(two-3)+1;//對得到的矩陣區間進行係數糾正,得到閾值範圍;(在這裡我只對

r通道的矩陣區間進行處理其他兩個通道同樣的處理方式)

int highb = 8*(one+10)-1;

int lowg = 230;

int highg = 255;

int lowr = 230;

int highr =255;

inrange(frame, scalar(lowb, lowg, lowr), scalar(highb, highg, highr), rbgthresholded); //閾值化原圖

cvtcolor(rbgthresholded, gray, cv_bgra2gray);//rgb圖轉換成灰度圖

imshow("灰度圖

", gray);//

輸出灰度圖

//對灰度圖進行一系列的處理

//開運算(去掉噪點)

mat element = getstructuringelement(morph_rect, size(5,5 ));

morphologyex(gray, gray, morph_open, element);

//閉運算 

(連線部分連通域)

morphologyex(gray, gray, morph_close, element);

threshold(gray, bin, 120, 255, cv_thresh_binary);二值化

medianblur(bin, bin, 3);//中值濾波

imshow("二值圖

//尋找畫素點,繪製輪廓

vector> contours;

vectorhiers;

mat bin_con;

bin.copyto(bin_con);//拷貝資料

mat bin_concontours = mat::zeros(bin_con.size(), cv_8uc1);

mat contours = mat::zeros(bin_con.size(), cv_8uc1);  //繪製  

for (int i = 0; i 

//contours[i]代表的是第

i個輪廓,

contours[i].size()

代表的是第

i個輪廓上所有的畫素點數  

for (int j = 0; j 

//繪製出

contours

向量內所有的畫素點  

point p = point(contours[i][j].x, contours[i][j].y);

contours.at

(p) = 255;

//繪製輪廓  

drawcontours(bin_concontours, contours, i, scalar(255), 1, 8, hiers);

imshow("輪廓圖

最終得到效果如圖,完美!

依次對其他幾個樣圖檢測:

基於顏色特徵提取數字

最近因為畢業課題的需要,初學opencv,鑽研一周,終於有一點點小小的收穫。編譯環境 vs2013 opencv.4.9 任務 攝像頭識別電梯中已亮的按鈕數字 目的 框選圖中已亮的3和5號按鈕。話不多說,上 rgb直方圖 把r通道分為32個bin int rbins 32 int histsize ...

基於顏色的特徵提取

內容及要求 一 設計說明 基於內容的影象檢索 content based image retrieval,cbir 技術由機器自動提取包含影象內容的視覺化特徵,如顏色 形狀 紋理等,對資料庫中的影象和查詢樣本影象在特徵空間進行匹配,檢索出與樣本相似的影象。其原理框圖如圖1所示。圖1 基於內容的影象檢...

顏色特徵提取

顏色特徵是在影象檢索中應用最為廣泛的視覺特徵,主要原因在於顏色往往和影象中所包含的物體或場景十分相關。此外,與其他的視覺特徵相比,顏色特徵對影象本身的尺寸 方向 視角的依賴性較小,從而具有較高的魯棒性。面向影象檢索的顏色特徵的表達涉及到若干問題。首先,我們需要選擇合適的顏色空間來描述顏色特徵 其次,...