簡體   English   中英

OpenCV在實時攝像頭中更改顏色

[英]OpenCV change colors in real time camera feed

我正在使用openCV 2.4.9。 我在新窗口中使用OpenCV運行相機打開相機。 我想通過按一下鍵來更改相機供稿的顏色。 例如,當我單擊“ 1”時,攝像機的供稿會更改為灰度,“ 2”->黑白,“ 3”-> HSV,而當我按“ ESC”時,返回(0)。 到目前為止,我提出了以下內容:

#include <iostream>
#include <conio.h>
using namespace std;

#include<opencv\cv.h>
#include<opencv\highgui.h>
#include "opencv2\core\core.hpp"
#include "opencv2\imgproc\imgproc.hpp"


void main(){

  CvCapture *capture = cvCaptureFromCAM(CV_CAP_ANY);
  IplImage *frame = 0, *image = 0;
  int key = 0, last = 0;

  cvNamedWindow("WebCamera", CV_WINDOW_AUTOSIZE);

  while(key != 27)  {

          frame = cvQueryFrame(capture);
          image = cvCloneImage(frame);

        // i try to use swich and case for this but i can't get it work
        // when using cvtColor need to use Mat image but when use cvShowImage need IplImage 
        //  switch(last)
        //  {
        //      case '1': 
        //           cvtColor(image,HSVimage,CV_BGR2HSV);
        //      case '2': 
        //           cvtColor(image,HSVimage,CV_BGR2GRAY);
        //      case '3': 
        //           . 
        //           .
        //      default: break;
        //  }


          cvShowImage("WebCamera", image);
          cvReleaseImage(&image);
          key = cvWaitKey(1);
          if (key != -1) last = key;
  }
  cvDestroyWindow("WebCamera");
  cvReleaseCapture(&capture);

  exit(0);
}

我想在同一窗口中一次又一次地更改顏色,或者(如果不可能的話)為每個濾色器打開和關閉窗口。 謝謝。 對不起英語不好

它應該與下面的代碼一起使用。 OpenCV教程和OpenCV文檔中獲得了它。

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

using namespace cv;
using namespace std;

int main()
{
    int key = 0, last = 0;
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened()) // check if we camera is opened
    { 
        cout << "Cannot open selected camera" << endl;
        return -1;   
    } 
    namedWindow("Capture",1);
    Mat convertedImage;

    for(;;) //Loop until user hit "esc"
    {
        Mat frame;
        cap >> frame; // get a new frame from camera            

        switch(last)
        {
           case '1':
           { 
               cvtColor(frame,convertedImage,CV_BGR2GRAY);
               break;
           }
          case '2': //Binarization to generate Black/White image
          {
               Mat img_gray;
               cvtColor(frame,img_gray,CV_BGR2GRAY); //First convert to gray
               //Binarization. Use your parameters here or try adaptiveThreshold
               threshold(img_gray, convertedIamge, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU); 
          }
          case '3': 
          {  
              cvtColor(frame,convertedImage,CV_BGR2HSV);
              break;
          }
          default: //use to prevent ecxeption at program start or use case '0' to show original image
          {
                  convertedImage = frame;
          }
        }

        imshow("Capture", convertedImage); //show converted image

        key = waitKey(1);
        if (key != -1)             
            last = key;  

        if(key == 27)
          break;            

        // the camera will be deinitialized automatically in VideoCapture destructor
    }
    return 0;
}

我沒有檢查(編譯)。但是我認為它可以幫助您。

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main() {
VideoCapture stream1(0);

if (!stream1.isOpened()) { cout << "cannot open camera"; }

while (true) {
Mat cameraFrame;
stream1.read(cameraFrame);
switch(last)
{
case '1': 
cvtColor(image,HSVimage,CV_BGR2HSV);
break;
case '2': 
cvtColor(image,HSVimage,CV_BGR2GRAY);
break;
case '3': 
...}
imshow("cam", cameraFrame);
key = cvWaitKey(1);
if (key != -1) last = key;
}
return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM