0

I am always getting a grey screen when showing image using opencv, the image is captured from camera. Any help?

#include "stdafx.h"
#include <opencv2\opencv.hpp>
#include <opencv2\imgcodecs.hpp>

using namespace cv;

cv::Mat takePicture() {
cv::Mat pic;
VideoCapture cam(0);
while (!cam.isOpened()) {
    std::cout << "Failed to make connection to cam" << std::endl;
    VideoCapture cam(0);
}
cam >> pic;
return pic;
}
int main()
{

cv::Mat pic1;

pic1 = takePicture();

imshow("camera", pic1);

}
  • You need to use [`waitKey()`](http://docs.opencv.org/2.4/modules/highgui/doc/user_interface.html?highlight=waitkey#waitkey) after using `imshow()` to get the image to show up in the window. More [here](https://stackoverflow.com/questions/12452118/what-does-waitkey-30-mean-in-opencv). But that may not be your problem. I've flagged this as a duplicate as it has been asked before, and has an answer. – alkasm Jun 15 '17 at 21:04
  • 3
    Possible duplicate of [OpenCV - Webcam imshow not displaying live feed, gray screen instead](https://stackoverflow.com/questions/43726804/opencv-webcam-imshow-not-displaying-live-feed-gray-screen-instead) – alkasm Jun 15 '17 at 21:05
  • @AlexanderReynolds Thanks! That Solved my problem. – Steve Studios Jun 15 '17 at 21:51

1 Answers1

0

Camera needs time to be initialized. Maybe you should change your code like this:

#include "stdafx.h"
#include <opencv2\opencv.hpp>
#include <opencv2\imgcodecs.hpp>    
VideoCapture cam(0);
cv::Mat pic;
cv::Mat takePicture(){
    while (!cam.isOpened()) {
        std::cout << "Failed to make connection to cam" << std::endl;
        cam.open(0);
    }
    cam>>pic;
    return pic;
}
int main(){
    cv::waitKey(1000);
    cv::Mat pic1;
    pic1 = takePicture();
    imshow("camera", pic1);
    cv::waitKey();
}
Ponkux
  • 1
  • 2