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

C ++を使用してOpenCVの他のカメラからビデオをキャプチャする方法は?


このトピックでは、OpenCVを使用して他のカメラからビデオをキャプチャする方法を決定します。デフォルトのカメラ以外のカメラへのアクセスは、デフォルトのカメラへのアクセスと同様です。 'VideoCapture cap(0)'を使用する代わりに、カメラ番号を割り当てる必要があるという違いが1つだけあります。カメラ番号は、USBポートのシーケンスに従います。カメラが3番目のUSBポートに接続されている場合、カメラの番号は3です。

次のプログラムは、3番目のカメラにアクセスし、カメラから取得したリアルタイムのビデオストリームを表示します。

#include<opencv2/opencv.hpp>//OpenCV header to use VideoCapture class//
#include<iostream>
using namespace std;
using namespace cv;
int main() {
   Mat myImage;//Declaring a matrix to load the frames//
   namedWindow("Video Player");//Declaring the video to show the video//
   VideoCapture cap(3);//Declaring an object to capture stream of frames from third camera//
   if (!cap.isOpened()){ //This section prompt an error message if no video stream is found//
      cout << "No video stream detected" << endl;
      system("pause");
      return-1;
   }
   while (true){ //Taking an everlasting loop to show the video//
      cap >> myImage;
      if (myImage.empty()){ //Breaking the loop if no video frame is detected//
         break;
      }
      imshow("Video Player", myImage);//Showing the video//
      char c = (char)waitKey(25);//Allowing 25 milliseconds frame processing time and initiating break condition//
      if (c == 27){ //If 'Esc' is entered break the loop//
         break;
      }
      imshow("Video Player", myImage);//Showing the video//
      char c = (char)waitKey(25);//Allowing 25 milliseconds frame processing time and initiating break condition//
      if (c == 27){ //If 'Esc' is entered break the loop//
         break;
      }
   }
   cap.release();//Releasing the buffer memory//
   return 0;
}

出力

C ++を使用してOpenCVの他のカメラからビデオをキャプチャする方法は?


  1. C ++を使用してOpenCVでバイナリイメージを反転する方法は?

    バイナリイメージを反転するとは、ピクセル値を反転することを意味します。視覚的な観点から、バイナリイメージを反転すると、白のピクセルは黒に変換され、黒のピクセルは白に変換されます。 この関数の基本的な形式は-です。 cvtColor(original_image, grayscale_image, COLOR_BGR2GRAY); 次の行は、グレースケール画像をバイナリ画像に変換し、変換された画像を「binary_image」行列に保存しています。 threshold(grayscale_image, binary_image, 100, 255, THRESH_BINARY); ここで、

  2. C ++を使用してOpenCVでバイナリイメージを作成するにはどうすればよいですか?

    バイナリイメージは、白黒の2色を表す単なるデジタルイメージです。画像処理の観点から、バイナリ画像には、0と1の2つの可能な値を持つピクセルが含まれています。ピクセルの値が0の場合、それは純粋な黒色を表します。ピクセルの値が1の場合、それは純粋な白色を意味します。 グレースケール画像では、それぞれに256の異なる可能な値があります。しかし、バイナリイメージでは、可能な値は2つだけです。バイナリイメージには、さまざまなタイプのアプリケーションがあります。たとえば、形態学的変換には2値画像が必要であり、背景からのオブジェクト形状の抽出には2値画像が必要です。OpenCVを使用すると、画像を2値画像