theroom101
theroom101

Reputation: 609

Cannot access to my webcam opencv ubuntu

here is my code

#include <iostream>

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace std;

const int KEY_ENTER = 10;
const int KEY_ESC   = 27;
const int KEY_1         = 49;
const int KEY_2         = 50;
const int KEY_3         = 51;
const int KEY_4         = 52;
const int KEY_5         = 53;
const int KEY_6         = 54;

const int DELAY = 30;

const string WIN_NAME = "Camera View";

const string NAME[6] = {"me", "serk", "prot", "vitkt", "st", "tara"};

struct pg
{
string name;
int cnt;
pg(): name(""), cnt (0) {};
pg(string s, int c) : name(s) , cnt(c) {};
};

pg crew[6];

int main()
{
for(int i = 0; i < 6; ++i)
    crew[i] = pg(NAME[i], 0);

cv::VideoCapture cam;

cam.open(0);

cv::Mat frame;

pg cur = crew[0];

int c = 0;
for(;cam.isOpened();)
{
    try
    {
    cam >> frame;

    cv::imshow(WIN_NAME, frame);

    int key = cv::waitKey(DELAY);

    cur = (key >= KEY_1 && key <= KEY_6) ? crew[key - KEY_1] : cur;

    if(KEY_ENTER == key)
        cv::imwrite(cv::format("%s%d.jpg", cur.name.c_str(), cur.cnt++), frame);

    if(KEY_ESC == key)
        break;
    } catch (cv::Exception e)
    {
        cout << e.err << endl;
    }
}

cam.release();
return 0;
}

but I cannot capture a video from camera. =( I've got Ubuntu 12.04 on my PC,

I did exactly every instruction in linux install istructions I googled my problem and installed additional dependencies this

and many others which I can find. but it still doesn't work.
It's ridiculous but this code works on my laptop, with the same distribution of ubuntu. I have no compilation errors.

in terminal gstreamer-properties opens that camera. Does someone know what to do? Help me please.

I've noticed that it even doesn't load pictures from file

code example #include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hpp"

#include <iostream>

using namespace std;

int main()
{
system("clear");

cv::Mat picture;
picture = cv::imread("boobies.jpg");

cout << picture.rows << endl;
cv::imshow("Smile", picture);

char ch;
cin >> ch;

cv::destroyWindow("Smile");

return 0;
}

haven't load the picture from project folder

Upvotes: 2

Views: 3706

Answers (2)

user1810087
user1810087

Reputation: 5334

You forgot to initilize cam. you must use the constructor with int as parameter.

// the constructor that opens video file
VideoCapture(const string& filename);
// the constructor that starts streaming from the camera
VideoCapture(int device);

Do it like:

cv::VideoCapture cam(0);
cam.open(0);

Also, you could use cvCaptureFromCAM:

CvCapture *capture;
capture = cvCaptureFromCAM( 0 );

This will allocates and initializes your capture instance.

Upvotes: 2

Poko
Poko

Reputation: 822

if you are under Opencv 2.4.6 it has been hotfixed: http://opencv.org/hot-fix-for-opencv-2-4-6.html

Upvotes: 1

Related Questions