2019-11-30
這周主要還是在學習opencv基本API的應用與原理,同時也在學習C++的線程,那么這次就記錄對燈條的顏色識別
HSV基本顏色分量范圍(通過實驗得到的模糊范圍,實際操作中我們可以據此做出適當調整)
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include <math.h>
using namespace cv;
using namespace std;
int main()
{
//VideoCapture cap("D:/H.mp4"); //capture the video from web cam
//if (!cap.isOpened()) // if not success, exit program
//{
// cout << "Cannot open the web cam" << endl;
// return -1;
//}
namedWindow("control", 1);
int ctrl = 0;
createTrackbar("ctrl", "control", &ctrl, 7);
while (true)
{
Mat imgOriginal;
//bool bSuccess = cap.read(imgOriginal);
//if (!bSuccess)
//{
// cout << "Cannot read a frame from video stream" << endl;
// break;
//}
imgOriginal = imread("D:/L.png");
Mat imgHSV, imgBGR;
Mat imgThresholded;
if (0)
{
vector<Mat> hsvSplit; //創建向量容器,存放HSV的三通道數據
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV
split(imgHSV, hsvSplit); //分類原圖像的HSV三通道
equalizeHist(hsvSplit[2], hsvSplit[2]); //對HSV的亮度通道進行直方圖均衡
merge(hsvSplit, imgHSV);//合並三種通道
cvtColor(imgHSV, imgBGR, COLOR_HSV2BGR); //將HSV空間轉回至RGB空間,為接下來的顏色識別做准備
}
else
{
imgBGR = imgOriginal.clone();
}
switch (ctrl)
{
case 0:
{
inRange(imgBGR, Scalar(60, 120, 245), Scalar(120, 255, 255), imgThresholded); //藍色
break;
}
case 1:
{
inRange(imgBGR, Scalar(128, 128, 128), Scalar(255, 255, 255), imgThresholded); //白色
break;
}
case 2:
{
inRange(imgBGR, Scalar(128, 128, 0), Scalar(255, 255, 127), imgThresholded); //靛色
break;
}
case 3:
{
inRange(imgBGR, Scalar(128, 0, 128), Scalar(255, 127, 255), imgThresholded); //紫色
break;
}
case 4:
{
inRange(imgBGR, Scalar(0, 128, 128), Scalar(127, 255, 255), imgThresholded); //黃色
break;
}
case 5:
{
inRange(imgBGR, Scalar(0, 128, 0), Scalar(127, 255, 127), imgThresholded); //綠色
break;
}
case 6:
{
inRange(imgBGR, Scalar(0, 0, 128), Scalar(127, 127, 255), imgThresholded); //紅色
break;
}
case 7:
{
inRange(imgBGR, Scalar(0, 0, 0), Scalar(127, 127, 127), imgThresholded); //黑色
break;
}
}
imshow("形態學去噪聲前", imgThresholded);
Mat element = getStructuringElement(MORPH_RECT, Size(10, 15));
morphologyEx(imgThresholded, imgThresholded, MORPH_OPEN, element);
morphologyEx(imgThresholded, imgThresholded, MORPH_CLOSE, element);
imshow("Thresholded Image", imgThresholded); //show the thresholded image
imshow("直方圖均衡以后", imgBGR);
imshow("Original", imgOriginal); //show the original image
char key = (char)waitKey(300);
if (key == 27)
break;
}
return 0;
}
通過滑條發現ctrl=1 時 效果較好,但數字沒有消除。
當我單獨分出藍色識別范圍,並繼續用HSV空間時。
#include <iostream> #include <opencv2/highgui/highgui.hpp> #include <opencv2/core/core.hpp> #include <opencv2/opencv.hpp> #include <math.h> using namespace cv; using namespace std; int main() { //VideoCapture cap("D:/H.mp4"); //if (!cap.isOpened()) //{ // cout << "Cannot open the web cam" << endl; // return -1; //} namedWindow("control", 1); int ctrl = 0; createTrackbar("ctrl", "control", &ctrl, 7); while (true) { Mat imgOriginal; //bool bSuccess = cap.read(imgOriginal); //if (!bSuccess) //if not success, break loop //{ // cout << "Cannot read a frame from video stream" << endl; // break; //} imgOriginal = imread("D:/L.png"); Mat imgHSV, imgBGR; Mat imgThresholded; if (imgOriginal.empty()) { cout << "open failed" << endl; return -1; } /*else { imgBGR = imgOriginal.clone(); } */ vector<Mat> hsvSplit; //創建向量容器,存放HSV的三通道數據 cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); split(imgHSV, hsvSplit); //分類原圖像的HSV三通道 equalizeHist(hsvSplit[2], hsvSplit[2]); //對HSV的亮度通道進行直方圖均衡 merge(hsvSplit, imgBGR);//合並三種通道 //cvtColor(imgHSV, imgBGR, COLOR_HSV2BGR); //將HSV空間轉回至RGB空間,為接下來的顏色識別做准備 switch (ctrl) { case 0: { inRange(imgBGR, Scalar(60, 120, 245), Scalar(120, 255, 255), imgThresholded); //藍色 break; } case 1: { inRange(imgBGR, Scalar(128, 128, 128), Scalar(255, 255, 255), imgThresholded); //白色 break; } case 2: { inRange(imgBGR, Scalar(128, 128, 0), Scalar(255, 255, 127), imgThresholded); //靛色 break; } case 3: { inRange(imgBGR, Scalar(128, 0, 128), Scalar(255, 127, 255), imgThresholded); //紫色 break; } case 4: { inRange(imgBGR, Scalar(0, 128, 128), Scalar(127, 255, 255), imgThresholded); //黃色 break; } case 5: { inRange(imgBGR, Scalar(0, 128, 0), Scalar(127, 255, 127), imgThresholded); //綠色 break; } case 6: { inRange(imgBGR, Scalar(0, 0, 128), Scalar(127, 127, 255), imgThresholded); //紅色 break; } case 7: { inRange(imgBGR, Scalar(0, 0, 0), Scalar(127, 127, 127), imgThresholded); //黑色 break; } } imshow("形態學去噪聲前", imgThresholded); Mat element = getStructuringElement(MORPH_RECT, Size(10, 15)); morphologyEx(imgThresholded, imgThresholded, MORPH_OPEN, element); morphologyEx(imgThresholded, imgThresholded, MORPH_CLOSE, element); imshow("Thresholded Image", imgThresholded); //show the thresholded image imshow("直方圖均衡以后", imgBGR); imshow("Original", imgOriginal); //show the original image char key = (char)waitKey(300); if (key == 27) break; } return 0; }
效果如下
通過代碼對比發現如果將HSV再轉換位BGR空間的時候,HSV的顏色將不適用,因此做好圖像的預處理是非常重要。這對於圖像轉換為視頻有着必然的聯系。
總結一下部分API功能及代碼講解。
總之這周學習的東西也有不少,但沒有整理好,因為快考試也在忙着復習功課,時間上特別的緊。后面我將繼續學習下裝甲板的識別。