C++ 加速OpenCV';输入响应
我正在编写我的第一个C++ 加速OpenCV';输入响应,c++,opencv,C++,Opencv,我正在编写我的第一个OpenCV程序,只是在Macbook上用相机进行一些图像处理。下面的代码仅显示了摄像头,允许我按0查看正常视图,1,2,或3从GRB更改,并按4将其更改为黑白 不幸的是,我必须按住键才能让它做出反应。是什么导致了这种延迟?我如何才能使代码对输入做出更大的响应 #include "opencv2/core/core.hpp" #include "opencv2/flann/miniflann.hpp" #include "opencv2/imgproc/imgproc.hpp
OpenCV
程序,只是在Macbook上用相机进行一些图像处理。下面的代码仅显示了摄像头,允许我按0
查看正常视图,1
,2
,或3
从GRB更改,并按4
将其更改为黑白
不幸的是,我必须按住键才能让它做出反应。是什么导致了这种延迟?我如何才能使代码对输入做出更大的响应
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/photo/photo.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core_c.h"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc/imgproc_c.h"
using namespace cv;
using namespace std;
Mat channel(Mat A, int ich) {
Mat Channel[3];
Mat B = A.clone();
split(B, Channel);
for(int i = 0; i < 3; i++) {
if( ich-1 != i ) Channel[i] = Mat::zeros(B.rows, B.cols, CV_8UC1);
}
merge(Channel, 3, B);
return B;
}
Mat BW(Mat A) {
Mat B;
B = A.clone();
cvtColor( A, B, CV_BGR2GRAY );
return B;
}
int main() {
int waitCount = 1; // wait for this many milliseconds to check for input
VideoCapture stream1(0);
namedWindow("cam", CV_WINDOW_NORMAL);
if( !stream1.isOpened() ) {
cout << "Cannot open camera!" << endl;
}
int showKind = 0;
Mat cameraFrame; // showKind = 0
Mat grey; // showkind = 4
while( true ) {
/// read the cameraFrame
stream1.read(cameraFrame);
/// show the cameraFrame
if( showKind == 0 ) imshow("cam", cameraFrame);
else if( showKind > 0 && showKind < 4 ) imshow("cam", channel(cameraFrame, showKind));
else if( showKind == 4 ) imshow("cam", BW(cameraFrame) );
else {
cout << "ERROR: Unknown showKind = " << showKind << endl;
}
////////////////////////////////////////////////////////////
/// check for input
////////////////////////////////////////////////////////////
// close down
if( waitKey(waitCount) == 27 ) {
cout << "ESC pressed ... exiting" << endl;
break;
}
// convert showKind
else if( waitKey(waitCount) == 48 ) {
cout << "Showkind changed to 0" << endl;
showKind = 0;
}
else if( waitKey(waitCount) == 49 ){
cout << "Showkind changed to 1" << endl;
showKind = 1;
}
else if( waitKey(waitCount) == 50 ){
cout << "Showkind changed to 2" << endl;
showKind = 2;
}
else if( waitKey(waitCount) == 51 ){
cout << "Showkind changed to 3" << endl;
showKind = 3;
}
else if( waitKey(waitCount) == 52 ){
cout << "Showkind changed to 4" << endl;
showKind = 4;
}
}
return 0;
}
#包括“opencv2/core/core.hpp”
#包括“opencv2/flann/miniflann.hpp”
#包括“opencv2/imgproc/imgproc.hpp”
#包括“opencv2/photo/photo.hpp”
#包括“opencv2/video/video.hpp”
#包括“opencv2/features2d/features2d.hpp”
#包括“opencv2/objdetect/objdetect.hpp”
#包括“opencv2/calib3d/calib3d.hpp”
#包括“opencv2/ml/ml.hpp”
#包括“opencv2/highgui/highgui.hpp”
#包括“opencv2/core/core_c.h”
#包括“opencv2/highgui/highgui_c.h”
#包括“opencv2/imgproc/imgproc_c.h”
使用名称空间cv;
使用名称空间std;
Mat通道(Mat A,int ich){
Mat通道[3];
Mat B=A.clone();
分裂(B,通道);
对于(int i=0;i<3;i++){
if(ich-1!=i)通道[i]=Mat::zero(B.行,B.列,CV_8UC1);
}
合并(通道3,B);
返回B;
}
材料BW(材料A){
材料B;
B=A.clone();
CVT颜色(A、B、CV_bgr2灰色);
返回B;
}
int main(){
int waitCount=1;//等待这么多毫秒检查输入
视频捕获流1(0);
namedWindow(“凸轮”,CV\u窗口\u正常);
如果(!stream1.isOpened()){
cout该问题是由对waitKey
的级联调用引起的。您只能调用waitKey
一次,并存储按下的键,如:
int key = waitKey(waitCount);
if (key == 27) {
cout << "ESC pressed ... exiting" << endl;
break;
}
// convert showKind
else if (key == 48) {
cout << "Showkind changed to 0" << endl;
showKind = 0;
}
else if (key == 49){
cout << "Showkind changed to 1" << endl;
showKind = 1;
}
else if (key == 50){
cout << "Showkind changed to 2" << endl;
showKind = 2;
}
else if (key == 51){
cout << "Showkind changed to 3" << endl;
showKind = 3;
}
else if (key == 52){
cout << "Showkind changed to 4" << endl;
showKind = 4;
}
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
Mat channel(const Mat& A, int ich) {
Mat Channel[3];
Mat B;
split(A, Channel);
for (int i = 0; i < 3; i++) {
if (ich - 1 != i) Channel[i].setTo(0);
}
merge(Channel, 3, B);
return B;
}
Mat BW(const Mat& A) {
Mat B;
cvtColor(A, B, CV_BGR2GRAY);
return B;
}
int main() {
int waitCount = 1; // wait for this many milliseconds to check for input
VideoCapture stream1(0);
namedWindow("cam", CV_WINDOW_NORMAL);
if (!stream1.isOpened()) {
cout << "Cannot open camera!" << endl;
}
int showKind = 0;
Mat cameraFrame; // showKind = 0
Mat grey; // showkind = 4
while (true) {
/// read the cameraFrame
stream1 >> cameraFrame;
/// show the cameraFrame
if (showKind == 0) imshow("cam", cameraFrame);
else if (showKind > 0 && showKind < 4) imshow("cam", channel(cameraFrame, showKind));
else if (showKind == 4) imshow("cam", BW(cameraFrame));
else {
cout << "ERROR: Unknown showKind = " << showKind << endl;
}
////////////////////////////////////////////////////////////
/// check for input1
////////////////////////////////////////////////////////////
// close down
int key = waitKey(waitCount);
if (key == 27) {
cout << "ESC pressed ... exiting" << endl;
break;
}
// convert showKind
else if (key == 48) {
cout << "Showkind changed to 0" << endl;
showKind = 0;
}
else if (key == 49){
cout << "Showkind changed to 1" << endl;
showKind = 1;
}
else if (key == 50){
cout << "Showkind changed to 2" << endl;
showKind = 2;
}
else if (key == 51){
cout << "Showkind changed to 3" << endl;
showKind = 3;
}
else if (key == 52){
cout << "Showkind changed to 4" << endl;
showKind = 4;
}
}
return 0;
}