OpenCV——視頻顏色識別


#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int minh,maxh,mins,maxs,minv,maxv;
void helptext()
{
    cout << "B——黑色\n";
    cout << "H——灰色\n";
    cout << "W——白色\n";
    cout << "R——紅色\n";
    cout << "O——橙色\n";
    cout << "Y——黃色\n";
    cout << "G——綠色\n";
    cout << "L——藍色\n";
    cout << "P——紫色\n";
    cout << "輸入要求識別的顏色對應的字母:" ;
}

void deal(char color)
{
        switch(color){
    case 'B':
        minh = 0;
        maxh = 180;
        mins = 0;
        maxs = 255;
        minv = 0;
        maxv = 46;
        break;
    case 'H':
        minh = 0;
        maxh = 180;
        mins = 0;
        maxs = 43;
        minv = 46;
        maxv = 220;
        break;
    case 'W':
        minh = 0;
        maxh = 180;
        mins = 0;
        maxs = 30;
        minv = 221;
        maxv = 255;
        break;
    case 'R':
        minh = 0;
        maxh = 10;
        mins = 43;
        maxs = 255;
        minv = 46;
        maxv = 255;
        break;
    case 'O':
        minh = 11;
        maxh = 25;
        mins = 43;
        maxs = 255;
        minv = 46;
        maxv = 255;
        break;
    case 'Y':
        minh = 26;
        maxh = 25;
        mins = 43;
        maxs = 255;
        minv = 46;
        maxv = 255;
        break;
    case 'G':
        minh = 35;
        maxh = 77;
        mins = 43;
        maxs = 255;
        minv = 46;
        maxv = 255;
        break;
    case 'L':
        minh = 100;
        maxh = 124;
        mins = 43;
        maxs = 255;
        minv = 46;
        maxv = 255;
        break;
    case 'P':
        minh = 125;
        maxh = 155;
        mins = 43;
        maxs = 255;
        minv = 46;
        maxv = 255;
        break;
    default:
        cout << "輸入錯誤" << endl;
        exit(0);
    }
}

int main(   )
{
    VideoCapture capture(0);
    Mat special;
    helptext();

    char color;
    cin >> color;
    deal(color);
    while(1){
        Mat frame;              //存儲每一幀的圖像
        capture >> frame;       //讀取當前幀
        Mat fhsv;
        cvtColor(frame,fhsv,COLOR_BGR2HSV);   //將圖像轉換為HSV模型
        
        inRange(fhsv,Scalar(minh,mins,minv),Scalar(maxh,maxs,maxv),special);          //找尋在要求區間內的顏色
        imshow("ABC",special);
        if(waitKey(30) >= 0)break;
    }

    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM