OpenCV在实时相机输入中改变颜色

时间:2017-04-18 21:52:29

标签: c++ opencv

我正在使用openCV 2.4.9。我在新窗口中使用OpenCV运行相机打开相机。我想用按键改变相机输入的颜色。例如,当我点击'1'相机Feed变为灰度时,'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);
}

我希望在同一窗口中反复更改颜色或(如果不可能)打开和关闭每个滤色器的窗口。谢谢。对不起英文不好

2 个答案:

答案 0 :(得分:2)

它应该与下面的代码一起使用。从this OpenCV教程和OpenCV documentation获得。

#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;
}

答案 1 :(得分: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;
}
相关问题