简体   繁体   中英

OpenCV change colors in real time camera feed

I am using openCV 2.4.9. I open camera using OpenCV run camera in a new window. I want to change colors of camera feed whith a key press. For example, when I click '1' camera feed change to gray scale, '2' -> black and white, '3' -> HSV, and when I press 'ESC' return(0). This what I've came up so far:

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

I want change colors again and again in same window or (if it is not possible) open and close windows for each color filter. Thank You. Sorry for bad English

It should work with the code below. Got it from this OpenCV tutorial and from the 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;
}

I not checked this(compileted).. But I think it can help you.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM