OpenCV can't find my USB webcam - opencv

I am trying to create an OpenCV application on my MacBook with built-in iSight camera. I grabbed some very simple code off the internet and ran it with no trouble. OpenCV automatically discovered the built-in webcam and ran properly but I can't get it to work with my USB webcam.
#include <stdio.h>
#include <opencv.hpp>
int main( int argc, char **argv )
{
CvCapture *capture = 0;
IplImage *frame = 0;
int key = 0;
/* initialize camera */
capture = cvCaptureFromCAM(0);
/* always check */
if ( !capture ) {
fprintf( stderr, "Cannot open initialize webcam!\n" );
return 1;
}
/* create a window for the video */
cvNamedWindow( "Test", CV_WINDOW_AUTOSIZE );
while( key != 'q' ) {
/* get a frame */
frame = cvQueryFrame( capture );
/* always check */
if( !frame ) break;
/* display current frame */
cvShowImage( "Test", frame );
/* exit if user press 'q' */
key = cvWaitKey( 1 );
}
/* free memory */
cvDestroyWindow( "Test" );
cvReleaseCapture( &capture );
return 0;
}
I compiled this with:
g++ webcam.c -o webcam -I/opt/local/include/opencv2 -I/opt/local/include -L/opt/local/lib -lopencv_core -lopencv_highgui
According to the documentation, by changing the line capture = cvCaptureFromCAM(0); to
capture = cvCaptureFromCAM(1); I should be able to access the other webcam that I have plugged in but running the program gives me the error message: Warning: Max Camera Num is 0; Using camera 0
What steps can I take to get OpenCV to recognize that I have another camera connected to my USB drive?

This is based on windows experience, but I believe the main issue to be the same. (To get input from a logitech USB-camera on my laptop.)
As far as I recall; OpenCV does not support multiple cameras, and thereby not the choice of camera.
I am guessing that you can easily run your build-in camera with the code you've shown.
My solution to a similar problem was deactivating the build-in camera. Giving the USB camera the only "availavle" slot for your cvCaptureFromCAM(0) function.
I hope this can solve your problem, even though the solution is a little 'clunky'.

Related

OpenCV Fullscreen Windows on Multiple Monitors

I have an OpenCV application that displays a fullscreen window, via:
cv::namedWindow("myWindow", CV_WINDOW_NORMAL)
cv::setWindowProperties("myWindow", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN)
It works fine, but when I have multiple monitors it always displays the fullscreen window on the First monitor. Is there any way to display on the 2nd monitor? I've tried setting X/Y and Width/Height, but they seem to be ignored once fullscreen is enabled.
Edits:
Sometimes pure OpenCV code cannot do a fullscreen window on a dual display. Here is a Qt way of doing it:
#include <QApplication>
#include <QDesktopWidget>
#include <QLabel>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDesktopWidget dw;
QLabel myLabel;
// define dimension of the second display
int width_second = 2560;
int height_second = 1440;
// define OpenCV Mat
Mat img = Mat(Size(width_second, height_second), CV_8UC1);
// move the widget to the second display
QRect screenres = QApplication::desktop()->screenGeometry(1);
myLabel.move(QPoint(screenres.x(), screenres.y()));
// set full screen
myLabel.showFullScreen();
// set Qimg
QImage Qimg((unsigned char*)img.data, img.cols, img.rows, QImage::Format_Indexed8);
// set Qlabel
myLabel.setPixmap(QPixmap::fromImage(Qimg));
// show the image via Qt
myLabel.show();
return app.exec();
}
Don't forget to configure the .pro file as:
TEMPLATE = app
QT += widgets
TARGET = main
LIBS += -L/usr/local/lib -lopencv_core -lopencv_highgui
# Input
SOURCES += main.cpp
And in terminal compile your code as:
qmake
make
Original:
It is possible.
Here is a working demo code, to show a full-screen image on a second display. Hinted from How to display different windows in different monitors with OpenCV:
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main ( int argc, char **argv )
{
// define dimension of the main display
int width_first = 1920;
int height_first = 1200;
// define dimension of the second display
int width_second = 2560;
int height_second = 1440;
// move the window to the second display
// (assuming the two displays are top aligned)
namedWindow("My Window", CV_WINDOW_NORMAL);
moveWindow("My Window", width_first, height_first);
setWindowProperty("My Window", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
// create target image
Mat img = Mat(Size(width_second, height_second), CV_8UC1);
// show the image
imshow("My Window", img);
waitKey(0);
return 0;
}
I've tried different ways to make it working, but unfortunetely it seems that this is not possible using OpenCV. The only thing you can do is probably display one window on main(primary) screen just using your current code and handle second window manually - set window position, resize image, and just use imshow function to display it. Here is some example:
void showWindowAlmostFullscreen(cv::Mat img, std::string windowTitle, cv::Size screenSize, cv::Point screenZeroPoint)
{
screenSize -= cv::Size(100, 100); //leave some place for window title bar etc
double xScallingFactor = (float)screenSize.width / (float)img.size().width;
double yScallingFactor = (float)screenSize.height / (float)img.size().height;
double minFactor = std::min(xScallingFactor, yScallingFactor);
cv::Mat temp;
cv::resize(img, temp, cv::Size(), minFactor, minFactor);
cv::moveWindow(windowTitle, screenZeroPoint.x, screenZeroPoint.y);
cv::imshow(windowTitle, temp);
}
int _tmain(int argc, _TCHAR* argv[])
{
cv::Mat img1 = cv::imread("D:\\temp\\test.png");
cv::Mat img2;
cv::bitwise_not(img1, img2);
cv::namedWindow("img1", CV_WINDOW_AUTOSIZE);
cv::setWindowProperty("img1", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
cv::namedWindow("img2");
while(cv::waitKey(1) != 'q')
{
cv::imshow("img1", img1);
cv::setWindowProperty("img1", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
showWindowAlmostFullscreen(img2, "img2", cv::Size(1366, 768), cv::Point(260, 1080));
}
}
and the result:
Screen size and screen zero point (i don't know whether this is a correct name of this point - generally it's just a point in which there is screen (0,0) point) you can get using some other library or from windows control panel. Screen zero point will display when you will start moving screen:
If you use QT for writing your code, you can possibly utilize QT5's "Widget".
Here is a tutorial that will show you how to display an OpenCV image in a QT Widget.
Once you have that working you can then use something like this:
QScreen *screen = QGuiApplication::screens()[1]; // specify which screen to use
SecondDisplay secondDisplay = new SecondDisplay(); // your widget
** Add your code to display opencv image in widget here **
secondDisplay->move(screen->geometry().x(), screen->geometry().y());
secondDisplay->resize(screen->geometry().width(), screen->geometry().height());
secondDisplay->showFullScreen();
(Code found here on another SO answer)
I have not tried this myself, so I can't guarantee it will work, however, but it seems likely (if not a little overkill)
Hope this helps.

Is it possible to have a square resolution with a webcam video stream using OpenCV?

I wrote a simple OpenCV program that recovers my webcam video stream and display it on a simple window. I wante to resize this window to the resolution 256x256 but it changed it to 320x240.
Here's my source code :
#include <iostream>
#include <opencv/cv.h>
#include <opencv/highgui.h>
using namespace std;
int main(int argc, char** argv)
{
char key;
cvNamedWindow("Camera_Output", cv::WINDOW_NORMAL);
CvCapture *capture = cvCaptureFromCAM(CV_CAP_ANY);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 256);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 256);
while(1){
IplImage *frame = cvQueryFrame(capture);
cvShowImage("Camera_Output", frame);
key = cvWaitKey(10);
if (key == 27){
break;
}
}
cvReleaseCapture(&capture);
cvDestroyWindow("Camera_Output");
return 0;
}
The output resolution is 320x240 and I want a 256x256 resolution. I think it's not possible because the camera manages its output video stream buffer and it has to keep the same ratio (width/height). What do you think about this idea ?
Is there a function which can force the resolution as a square resolution using OpenCV ?
Thanks a lot in advance for your help.
Seems like you video source does not handle 256x256 resolution. If you want to display it as such, you will have to crop the image yourself before displaying it.
Simple, you can do this by:
VideoCapture cap;
cap.open(0); // open your web-camera
cap.set(CV_CAP_PROP_FRAME_WIDTH, 256);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 256);
If this doesn't work, you need to resize it manually by calling cv::resize().

Why would my OpenCV program on BeagleBone Black capture images like this?

Here is a link to the type of images my program produces: http://imgur.com/a/vibBx#0
I am trying out a simple capture test program that I have written. I am trying to capture images in a loop and save them onto the board properly numbered. The first capture sometimes is corrupted and the subsequent captures are a mix of two images. I have also observed that sometimes the upper half of the image is from the previous capture and the lower half is the capture from that cycle. I have given the details and the code below.
OpenCV 2.4.2 running on BeagleBone Black which has Ångström installed on it.
The camera which is plugged to the USB of BeagleBone Black is Logitech C920.
The camera is connected to BeagleBone Black before power up through the 5 V power supply and connecting BeagleBone Black to the laptop. Access is through PuTTY.
Code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <string>
#include <unistd.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <cxcore.h>
#include "LaserWeed.h"
#include "SimpleGPIO.h"
using namespace cv;
int main(int argc, char* argv[])
{
int i=0, a;
string name;
int length;
char c, Filename[10000];
CvCapture* capture = NULL;
IplImage* img = NULL;
do
{
//I am not sure if this is necessary, but I tried anyway
//to see if it makes a difference since upper half of
//image was from previous cycle.
capture = NULL;
img = NULL;
//Converting Numbers to string to save with proper name
std::stringstream ss;
ss << i;
name = ss.str();
name = name + ".jpg";
length = name.size();
for(a=0; a<length; a++)
{
Filename[a] = name[a];
}
capture = cvCaptureFromCAM(-1);
if (!capture)
{
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
return -1;
}
img = cvQueryFrame(capture);
if (!img)
{
fprintf(stderr, "ERROR: img is null...\n");
getchar();
return -1;
}
cvSaveImage(Filename,img);
cvReleaseCapture(&capture);
cvReleaseImage(&img);
i++;
c = getchar();
}
while (c!='e')
;
return 0;
}
Where might I be going wrong?
The Stack Overflow question BeagleBone, OpenCV and webcam issue somewhat has a similar problem. But reinstallation of the OS will be my last option.
This is weird, anyway try to take the capture out of the do-while loop, maybe opening and releasing the capture device each time won't give enough time for the camera to prepare it's image buffers.

OpenCV VideoCapture reading issue

This will probably be a dumb question, but i really can't figure it out.
First of all: sorry for the vague title, i'm not really sure about how to describe my problem in a couple of words.
I'm using OpenCV 2.4.3 in MS Visual Studio, C++. I'm using the VideoCapture interface for capturing frames from my laptop webcam.
What my program should do is:
Loop on different poses of the user, for each pose:
wait that the user is in position (a getchar() waits for an input that says "i'm in position" by simply hitting enter)
read the current frame
extract a region of intrest from that frame
save the image in the ROI and then label it
Here is the code:
int main() {
Mat img, face_img, img_start;
Rect *face;
VideoCapture cam(0);
ofstream fout("dataset/dataset.txt");
if(!fout) {
cout<<"Cannot open dataset file! Aborting"<<endl;
return 1;
}
int count = 0; // Number of the (last + 1) image in the dataset
// Orientations are: 0°, +/- 30°, +/- 60°, +/-90°
// Distances are just two, for now
// So it is 7x2 images
cam.read(img_start);
IplImage image = img_start;
face = face_detector(image);
if(!face) {
cout<<"No face detected..? Aborting."<<endl;
return 2;
}
// Double ROI dimensions
face->x = face->x-face->width / 2;
face->y = face->y-face->height / 2;
face->width *= 2;
face->height *=2;
for(unsigned i=0;i<14;++i) {
// Wait for the user to get in position
getchar();
// Get the face ROI
cam.read(img);
face_img = Mat(img, *face);
// Save it
stringstream sstm;
string fname;
sstm << "dataset/image" << (count+i) << ".jpeg";
fname = sstm.str();
imwrite(fname,face_img);
//do some other things..
What i expect from it:
i stand in front of the camera when the program starts and it gets the ROI rectangle using the face_detector() function
when i'm ready, say in pose0, i hit enter and a picture is taken
from that picture a subimage is extracted and it is saved as image0.jpeg
loop this 7 times
What it does:
i stand in front of the camera when the program starts, nothing special here
i hit enter
the ROI is extracted not from the picture taken in that moment, but from the first one
At first, i used img in every cam.capture(), then i changed the first one in cam.capture(img_start) but that didn't help.
The second iteration of my code saves the image that should have been saved in the 1st, the 3rd iteration the one that should have been saved in the 2nd and so on.
I'm probably missing someting important from the VideoCapture, but i really can't figure it out, so here i am.
Thanks for any help, i really appreciate it.
The problem with your implementation is that the camera is not running freely and capturing images in real time. When you start up the camera, the videocapture buffer is filled up while waiting for you to read in the frames. Once the buffer is full, it doesn't drop old frames for new ones until you read and free up space in it.
The solution would be to have a separate capture thread, in addition to your "process" thread. The capture thread keeps reading in frames from the buffer whenever a new frame comes in and stores it in a "recent frame" image object. When the process thread needs the most recent frame (i.e. when you hit Enter), it locks a mutex for thread safety, copies the most recent frame into another object and frees the mutex so that the capture thread continues reading in new frames.
#include <iostream>
#include <stdio.h>
#include <thread>
#include <mutex>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace std;
using namespace cv;
void camCapture(VideoCapture cap, Mat* frame, bool* Capture){
while (*Capture==true) {
cap >> *frame;
}
cout << "camCapture finished\n";
return;
}
int main() {
VideoCapture cap(0); // open the default camera
if (!cap.isOpened()) // check if we succeeded
return -1;
Mat *frame, SFI, Input;
frame = new Mat;
bool *Capture = new bool;
*Capture = true;
//your capture thread has started
thread captureThread(camCapture, cap, frame, Capture);
mtx.lock();
imshow(*frame,current_frame);
mtx.unlock();
//Terminate the thread
mtx.lock();
*Capture = false;
mtx.unlock();
captureThread.join();
return 0;
}
This is the code that I wrote from the above advice. I hope someone can get help from this.
When you are capturing the image continuously, no captured frame will be stored in the opencv buffer, such that there will be no lag in streaming.
If you take screenshot/capture image with some time gap inbetween, the captured image will be first stored in the opencv buffer, after that the image is retrieved from the buffer.
When the buffer is full, when you are calling captureObject >> matObject, the last frame from the image is returned, not the current frame in the capturecard/webcam.
So only you are seeing a lag in your code. This issue can be resolved by taking screenshot based on the frames per second (fps) value of the webcam and time taken to capture the screenshot.
The time taken to read frame from buffer is very less, Measure the time taken to take the screenshot. If it is lesser than the fps we can assume that is read from buffer else it means it is captured from webcam.
Sample Code:
For capturing a recent screenshot from webcam.
#include <opencv2/opencv.hpp>
#include <time.h>
#include <thread>
#include <chrono>
using namespace std;
using namespace cv;
int main()
{
struct timespec start, end;
VideoCapture cap(-1); // first available webcam
Mat screenshot;
double diff = 1000;
double fps = ((double)cap.get(CV_CAP_PROP_FPS))/1000;
while (true)
{
clock_gettime(CLOCK_MONOTONIC, &start);
//camera.grab();
cap.grab();// can also use cin >> screenshot;
clock_gettime(CLOCK_MONOTONIC, &end);
diff = (end.tv_sec - start.tv_sec)*1e9;
diff = (diff + (end.tv_nsec - start.tv_nsec))*1e-9;
std::cout << "\n diff time " << diff << '\n';
if(diff > fps)
{
break;
}
}
cap >> screenshot; // gets recent frame, can also use cap.retrieve(screenshot);
// process(screenshot)
cap.release();
screenshot.release();
return 0;
}

opcv videowriter, i don't know why it does'nt work

I've just written a first program for videocaptur and videowriter. I copied the source from the wiki and changed the only video file name, but it made error.
Here is the source from the wiki.
The opencv is 2.1 and the compiler is visual c++ 2008 express.
#include "cv.h"
#include "highgui.h"
using namespace cv;
int main(int, char**)
{
VideoCapture capture(1); // open the default camera
if( !capture.isOpened() ) {
printf("Camera failed to open!\n");
return -1;
}
Mat frame;
capture >> frame; // get first frame for size
// record video
VideoWriter record("RobotVideo.avi", CV_FOURCC('D','I','V','X'), 30, frame.size(), true);
if( !record.isOpened() ) {
printf("VideoWriter failed to open!\n");
return -1;
}
namedWindow("video",1);
for(;;)
{
// get a new frame from camera
capture >> frame;
// show frame on screen
imshow("video", frame);
// add frame to recorded video
record << frame;
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
// the recorded video will be closed automatically in the VideoWriter destructor
return 0;
}
With the source, I changed 2 parts. One is for VideoCapture. (I don't have tunercard or camera.) The source is
VideoCapture capture(1); // open the default camera
and changed to
VideoCapture capture("C:/Users/Public/Videos/Sample Videos/WildlifeTest.wmv");
And the other is for VideoWriter:
// record video
VideoWriter record("RobotVideo.avi", CV_FOURCC('D','I','V','X'), 30, frame.size(), true);
and changed to
VideoWriter record("C:/Users/Public/Videos/Sample Videos/WildlifeRec.wmv",
CV_FOURCC('W','M','V','1'), 30,frame.size(), true);
and the part of error is:
// add frame to recorded video
record << frame;
Please show me what is my mistake!
P.S.
when I delete the line record << frame;, it works well. I think the error caused at the line.
And I found that even if without change, the wiki source program make same error.
The first error that i see is the file paths. You have to give them like this : C:\\Users\\....
please make sure you opencv_ffmpegXXX.dll work right

Resources