我正在使用 openCV 2.4.9。我在新窗口中使用 OpenCV run camera 打开相机。我想通过按键更改相机提要的颜色。例如,当我单击“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);
}
我想在同一窗口中一次又一次地更改颜色,或者(如果不可能)为每个滤色器打开和关闭窗口。谢谢。对不起英语不好
最佳答案
它应该与下面的代码一起工作。从 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;
}
关于c++ - OpenCV 在实时相机提要中改变颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43482824/