我使用OpenCV
建立了一个成功的边缘检测程序,该程序使用我的Macbook相机,并且可以根据原始图像中的不同通道打印各种边缘,但是当我添加轨迹栏以允许更改公差时在Canny
,它没有反应,导致按键速度变慢。
我怀疑我使用了这个错误,因为a similar error in my code引起了同样的延迟,但我不确定这里有什么问题。我的轨迹栏没有响应的任何想法?
#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"
#include <cstdlib>
using namespace cv;
using namespace std;
int showKind = 0;
int waitCount = 1; // wait for this many milliseconds to check for input
Mat channel(Mat A, int ich); // return the image with only this channel non-zero
Mat BW(Mat A); // return the black and white versin of this image
void myWaitKey();
Mat Blur(Mat A, int kernSize);
Mat Edges(Mat A, double tol1, double tol2);
int tol1 = 25;
int tol2 = 75;
int tol2Max = 250;
/// trackbar
void on_trackbar(int, void*) {
cout << "tol2 = " << tol2 << endl;
}
int main() {
VideoCapture stream1(0);
namedWindow("cam", CV_WINDOW_NORMAL);
if( !stream1.isOpened() ) {
cout << "Cannot open camera!" << endl;
}
string trackBarName = "tol2";
createTrackbar( trackBarName.c_str(), "cam", &tol2, tol2Max, on_trackbar);
on_trackbar( tol2, 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 if( showKind == 5 ) imshow("cam", Edges(channel(cameraFrame,1), tol1, tol2));
else if( showKind == 6 ) imshow("cam", Edges(channel(cameraFrame,2), tol1, tol2));
else if( showKind == 7 ) imshow("cam", Edges(channel(cameraFrame,3), tol1, tol2));
else if( showKind == 8 ) imshow("cam", Edges(cameraFrame, 25, 75));
else {
cout << "ERROR: Unknown showKind = " << showKind << endl;
}
myWaitKey();
}
return 0;
}
Mat channel(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(Mat A) {
Mat B;
cvtColor( A, B, CV_BGR2GRAY );
return B;
}
void myWaitKey() {
int key = waitKey(waitCount);
if( key == 27 ) {
cout << "ESC pressed ... exiting" << endl;
exit(EXIT_SUCCESS);
}
// convert showKind
else if( key == 48 ){ showKind = 0; }
else if( key == 49 ){ showKind = 1; }
else if( key == 50 ){ showKind = 2; }
else if( key == 51 ){ showKind = 3; }
else if( key == 52 ){ showKind = 4; }
else if( key == 53 ){ showKind = 5; }
else if( key == 54 ){ showKind = 6; }
else if( key == 55 ){ showKind = 7; }
else if( key == 56 ){ showKind = 8; }
}
Mat Blur(Mat A, int kernSize) {
//Mat G = BW(A);
Mat C;
// C is now black and white
blur(A, C, Size(kernSize,kernSize));
return C;
}
Mat Edges(Mat A, double tol1, double tol2) {
Mat G = BW(Blur(A,4));
Mat E;
Canny(G, E, tol1, tol2);
A.copyTo(G, E);
return G;
}
答案 0 :(得分:0)
在Mac OS X上实现轨迹栏实现的麻烦。
也许这个答案可以帮助: OpenCV trackbar performance in OSX
答案 1 :(得分:0)
请参阅下面的示例代码。我已经编写了这个演示代码。希望这对您有帮助。
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
using namespace cv;
/// Global variables
Mat src, src_gray;
Mat dst, detected_edges;
int edgeThresh = 1;
int lowThreshold;
int const max_lowThreshold = 100;
int ratio = 3;
int kernel_size = 3;
char* window_name = "Edge Map";
void CannyThreshold(int, void*)
{
/// Reduce noise with a kernel 3x3
blur( src_gray, detected_edges, Size(3,3) );
/// Canny detector
Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
/// Using Canny's output as a mask, we display our result
dst = Scalar::all(0);
src.copyTo( dst, detected_edges);
imshow( window_name, dst );
}
int main( int argc, char** argv )
{
/// Load an image
src = imread( argv[1] );
if( !src.data )
{ return -1; }
dst.create( src.size(), src.type() );
cvtColor( src, src_gray, CV_BGR2GRAY );
namedWindow( window_name, CV_WINDOW_AUTOSIZE );
createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold, CannyThreshold );
CannyThreshold(0, 0);
waitKey(0);
return 0;
}