C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

C ++でOpenCVを使用して色を検出するにはどうすればよいですか?


特定の色を検出し、色に基づいてオブジェクトを追跡する方法を理解します。色検出および色検出ベースの追跡システムのパフォーマンスは、環境に依存します。

部屋の明かりを変えたり、背景色を変えたりすると、色の検出に大きな影響があります。

次のプログラムは、C++でOpenCVを使用して色を検出する方法を示しています。

#include<iostream>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv) {
   VideoCapture video_load(0);//capturing video from default camera//
   namedWindow("Adjust");//declaring window to show the image//
   int Hue_Lower_Value = 0;//initial hue value(lower)//
   int Hue_Lower_Upper_Value = 22;//initial hue value(upper)//
   int Saturation_Lower_Value = 0;//initial saturation(lower)//
   int Saturation_Upper_Value = 255;//initial saturation(upper)//
   int Value_Lower = 0;//initial value (lower)//
   int Value_Upper = 255;//initial saturation(upper)//
   createTrackbar("Hue_Lower", "Adjust", &Hue_Lower_Value, 179);//track-bar for lower hue//
   createTrackbar("Hue_Upper", "Adjust", &Hue_Lower_Upper_Value, 179);//track-bar for lower-upper hue//
   createTrackbar("Sat_Lower", "Adjust", &Saturation_Lower_Value, 255);//track-bar for lower saturation//
   createTrackbar("Sat_Upper", "Adjust", &Saturation_Upper_Value, 255);//track-bar for higher saturation//
   createTrackbar("Val_Lower", "Adjust", &Value_Lower, 255);//track-bar for lower value//
   createTrackbar("Val_Upper", "Adjust", &Value_Upper, 255);//track-bar for upper value//
   while (1) {
      Mat actual_Image;//matrix to load actual image//
      bool temp = video_load.read(actual_Image);//loading actual image to matrix from video stream//
      Mat convert_to_HSV;//declaring a matrix to store converted image//
      cvtColor(actual_Image, convert_to_HSV, COLOR_BGR2HSV);//converting BGR image to HSV and storing it in convert_to_HSV matrix//
      Mat detection_screen;//declaring matrix for window where object will be detected//
      inRange(convert_to_HSV,Scalar(Hue_Lower_Value,Saturation_Lower_Value, Value_Lower),Scalar(Hue_Lower_Upper_Value,Saturation_Upper_Value, Value_Upper), detection_screen);//applying track-bar modified value of track-bar//
      erode(detection_screen, detection_screen, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));//morphological opening for removing small objects from foreground//
      dilate(detection_screen, detection_screen, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));//morphological opening for removing small object from foreground//
      dilate(detection_screen, detection_screen, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));//morphological closing for filling up small holes in foreground//
      erode(detection_screen, detection_screen, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));//morphological closing for filling up small holes in foreground//
      imshow("Threesholded Image", detection_screen);//showing detected object//
      imshow("Original", actual_Image);//showing actual image//
      if (waitKey(30) == 27){ //if esc is press break the loop//
         break;
      }
   }
   return 0;
}

出力

C ++でOpenCVを使用して色を検出するにはどうすればよいですか?


  1. C ++を使用してOpenCVの色空間を変換するにはどうすればよいですか?

    色空間は色を表現するモデルです。色を説明する方法はいくつかあります。たとえば、RGB、CYMK、HSV、グレースケールなど。 ここでは、imgproc.hppという名前の新しいヘッダーを使用しました。 このimgproc.hpp 画像処理の略語です。色空間を変換するには、cvtColor()を使用する必要があります OpenCVの機能。この関数はimgprocで定義されています ヘッダーファイル。そのため、imgproc.hppを含めました。 まず、2つの行列と2つのウィンドウを宣言しました。これらは、画像を読み込んで表示するためのものです。次に、「cat.jpg」という名前の画像を「

  2. C ++を使用してOpenCVの画像のチャンネル数を計算するにはどうすればよいですか?

    このトピックでは、画像のチャンネル数を確認する方法を理解します。プログラムを実行すると、チャンネル番号がコンソールウィンドウに表示されます。 チャネルの番号を取得するために、channels()という名前のOpenCVのクラスを使用しました。 クラスchannels()のオブジェクトとして画像マトリックスを渡すと、チャネルに整数値が与えられます。 次のプログラムは、チャネルの数をカウントし、コンソールウィンドウに表示します。 例 #include<iostream> #include<opencv2/highgui/highgui.hpp> using namesp