首页 新闻 会员 周边

OpenCV打开摄像头成功但是显示灰色

0
悬赏园豆:20 [已解决问题] 解决于 2015-12-20 21:37

我是想重写下摄像头标定的程序,想要好好学习一下。。前提是同样的环境下OpenCV自带标定程序:...\opencv\sources\samples\cpp\calibration.cpp工作正常,摄像头可以打开。但是我仿写的死活都是灰色。。

我的代码段如下,其中标定部分还没写完,请大家忽视。。

 1 #include <iostream>
 2 #include <time.h>
 3 
 4 #include <cv.hpp>
 5 #include <highgui\highgui.hpp>
 6 #include <calib3d\calib3d.hpp>
 7 #include <imgproc\imgproc.hpp>
 8 #include <core\core.hpp>
 9 
10 using namespace std;
11 using namespace cv;
12 
13 int main()
14 {
15     int success = 0;
16     int cameraId = 0;
17     int nFrames = 20;
18     int w = 6;
19     int h = 9;
20     clock_t prevTimestamp = 0;
21     int delay = 1000;
22 
23     //相关参数初始化
24     Size boardSize, imageSize;
25     boardSize.width = w;
26     boardSize.height = h;
27     vector<vector<Point2f>> imagePoints;
28     float squareSize, aspectRatio;
29     Mat cameraMatrix, distCoeffs, homography;
30     VideoCapture capture;
31     capture.open(cameraId);
32     namedWindow("Image View", 1);
33 
34     if (!capture.isOpened())
35     {
36         cout << "无法打开摄像头,(づ ̄3 ̄)づ╭❤~……" << endl;
37         return -1;
38     }
39 
40     for (int i = 0; success < nFrames; i++)
41     {
42         Mat view, viewGray;
43         capture >> view;
44         imageSize = view.size();
45         vector<Point2f> pointBuf;
46         cvtColor(view, viewGray, COLOR_BGR2GRAY);
47 
48         imshow("Image View", view);
49 
50         //寻找角点
51         bool found = findChessboardCorners(view, boardSize, pointBuf,
52             CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FAST_CHECK | CV_CALIB_CB_NORMALIZE_IMAGE);
53 
54         if (found)
55         {
56             //亚像素检测以提高精度
57             cornerSubPix(viewGray, pointBuf, Size(11, 11),
58                 Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
59             //标记出检测到的角点
60             drawChessboardCorners(view, boardSize, Mat(pointBuf), found);
61         }
62 
63         //等待用户改变姿态
64         if (found && clock_t() - prevTimestamp < delay*1e-3*CLOCKS_PER_SEC)
65         {
66             imagePoints.push_back(pointBuf);
67             success = success + 1;
68         }
69     }
70 
71     cout << "图像采集完成,开始标定喽,↖(^ω^)↗……" << endl;
72     //……
73 }

请指教。

nano_zombie.uestc的主页 nano_zombie.uestc | 初学一级 | 园豆:113
提问于:2015-12-19 11:33
< >
分享
最佳答案
0

来自OpenCV官网手册:http://docs.opencv.org/2.4/modules/highgui/doc/user_interface.html?highlight=waitkey

Note

This function(imshow) should be followed by waitKey function which displays the image for specified milliseconds. Otherwise, it won’t display the image. For example, waitKey(0) will display the window infinitely until any keypress (it is suitable for image display). waitKey(25) will display a frame for 25 ms, after which display will be automatically closed. (If you put it in a loop to read videos, it will display the video frame-by-frame)

nano_zombie.uestc | 初学一级 |园豆:113 | 2015-12-19 19:17
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册