加快OpenCV的输入响应 [英] Speeding up OpenCV's input response
问题描述
我在写我的第一个 OpenCV
程序,只是玩弄一些图像处理使用我的Macbook上的相机。下面的代码只是显示相机,并允许我按正常视图 0
按 1
, 2
或 3
更改为GRB和 4
和白色。
I am writing my first OpenCV
program and just playing around with some image manipulation using the camera on my Macbook. The code below just shows the camera and allows me to press 0
for the normal view, 1
, 2
, or 3
to change from GRB and 4
to change it to black and white.
不幸的是,我必须按住键来回应。是什么原因导致这种延迟,我如何得到代码更加响应输入?
Unfortunately, I have to hold down the key for it to respond. What causes this delay and how can I get the code to be more responsive to input?
#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;
}
推荐答案
级联调用 waitKey
。您可以调用 waitKey
仅存储一次,并按下键
,如:
The issue is caused by your cascade calls to waitKey
. You can call waitKey
only once, and store the key
pressed, like:
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>
而不是您的所有#include
- 将矩阵设置为给定值,您可以使用
setTo
,因此Channel [i] .setTo(0);
- 您不需要初始化OpenCV函数
OutputArray
的矩阵。 - Using a
switch
statement could also be clearer. - You can simply use
#include <opencv2/opencv.hpp>
instead of all your#include
- to set a matrix to a given value, you can use
setTo
, soChannel[i].setTo(0);
- You do not need to initialize matrices that are
OutputArray
of OpenCV's functions.
这里的完整代码有一些改进:
So here the full code with a few improvements:
#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;
}
这篇关于加快OpenCV的输入响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!