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功能及代码讲解。
总之这周学习的东西也有不少,但没有整理好,因为快考试也在忙着复习功课,时间上特别的紧。后面我将继续学习下装甲板的识别。