OpenCV fails to recognize webcam, but mplayer succeeds - c

As a first step on a larger project I was trying to display the imagem from my webcam using OpenCV:
#include <stdlib.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int
main()
{
cv::VideoCapture cap(-1);
if (!cap.isOpened())
exit(EXIT_FAILURE);
cv::Mat frame;
bool done = false;
while (!done) {
cap >> frame;
cv::imshow("webcam", frame);
done = (cv::waitKey(30) >= 0);
}
return EXIT_SUCCESS;
}
This returns an error code (!cap.isOpened() passes ,confirmed with gdb). Initially I had 0 instead of -1. When searching this site -1 was suggested, but it was to no avail. I also tried 1 through 3, as another user suggested it.
I can display my webcam using mplayer, more specifically mplayer tv:// -tv driver=v4l2.

v4l2 is the "video for linux" driver. I noticed OpenCV can be installed with such driver by compiling it with -DWITH_V4L and -DWITH_LIBV4L (v4l USE flag in Gentoo). After recompiling OpenCV with it, it successfully recognized the webcam. GTK support seems to be needed to display the image.

Related

Calling Chicken Scheme function from SDL2 audio callback function hangs

I am trying to embed Chicken Scheme into a C program, to generate sounds to be played with SDL2's audio system. I would have liked to use the sdl2 egg, but it does not seem to support Audio yet (despite the documentation mentioning the 'audio flag for the init! function).
At first, I was using SDL_QueueAudio from C, passing it a buffer that I had allocated in C and then filled in Scheme. This worked fine, passing a Sint16 * and size_t into Scheme, then using pointer-s16-set! from Scheme to fill it and returning a size_t to note how many cells were filled.
Then, when I realised that using the callback api for generating the audio was much better suited to this, I tried switching to it (having already used it before in C), only for the Scheme function to never be entered. Logging something in the callback function before the Scheme call worked, but logging directly within the Scheme function, or after the Scheme call, never happened.
I can only imagine that this is due to SDL2's audio callback running on a separate thread, and that messing with calling through to Scheme somehow. With this in mind, I tried calling CHICKEN_run(C_toplevel); from within the callback function, the first time that it was called, but that only resulted in a bus error.
So my question is: is there a way of calling embedded Chicken Scheme from SDL2's audio callback?
I am on macOs 10.13.6 High Sierra, with SDL2 and chicken both installed and up-to-date through Homebrew.
I compile with (as I said, this works fine when using the queue audio api):
csc code.c codescm.scm -embedded -o code -L -lSDL2
My simplified code is below:
#include <chicken.h>
#include "SDL2/SDL.h"
extern size_t fill_sound_buffer(Sint16 *buffer, size_t buffer_length);
void fill_sound_callback(void *user_data, Uint8 *stream, int stream_length)
{
// Logging here prints to the console
fill_sound_buffer((Sint16 *)stream, stream_length / 2);
// Logging here does not print to the console
}
void play(void)
{
SDL_AudioSpec audio_want;
SDL_zero(audio_want);
audio_want.freq = 44100;
audio_want.format = AUDIO_S16SYS;
audio_want.channels = 1;
audio_want.samples = 2048;
audio_want.callback = fill_sound_callback;
SDL_AudioSpec audio_have;
SDL_AudioDeviceID audio_device = SDL_OpenAudioDevice(NULL, 0, &audio_want, &audio_have, 0);
SDL_PauseAudioDevice(audio_device, 0);
SDL_Delay(5000);
// Logging here shows up after 5 seconds, but the program then continues to wait
SDL_CloseAudioDevice(audio_device);
}
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_AUDIO);
CHICKEN_run(C_toplevel);
play();
SDL_Quit();
return 0;
}
(import (chicken format)
(chicken foreign)
(chicken memory)
(chicken platform))
(define-external (fill_sound_buffer ((c-pointer short) buffer) (size_t buffer_length)) size_t
; This never prints when using the callback api
(printf "In Scheme~%")
; Removed the code that calculates a sine wave and fills the buffer with it, which works
0)
(return-to-host)

glXChooseFBConfig w/ GLX_BIND_TO_TEXTURE_*_EXT returning no FBConfigs on Nvidia driver

I have an application that uses the GLX extension texture_from_pixmap, which requires a color buffer created using an FBConfig with GLX_BIND_TO_TEXTURE_RGB_EXT, or GLX_BIND_TO_TEXTURE_RGBA_EXT, per the spec.
Only a color buffer of a GLX pixmap created using an FBConfig with
attribute GLX_BIND_TO_TEXTURE_RGB_EXT or GLX_BIND_TO_TEXTURE_RGBA_EXT
set to TRUE can be bound as a texture.
https://www.khronos.org/registry/OpenGL/extensions/EXT/GLX_EXT_texture_from_pixmap.txt
My application does this, and works fine with Mesa and the Intel i965 driver, but not with the proprietary Nvidia driver.
When using glXChooseFBConfig with the Nvidia driver, no matching FBConfigs are returned, and I can't seem to figure out why.
I've made a minimal code sample that reproduces this problem.
#include <stdio.h>
#include <X11/Xlib.h>
#include <GL/glx.h>
int main()
{
Display *display = XOpenDisplay(NULL);
if (!display) {
printf("Unable to connect to display.\n");
return 1;
}
int pixmap_config[] = {
GLX_BIND_TO_TEXTURE_RGB_EXT, True,
GLX_NONE
};
int c = 0;
GLXFBConfig *configs = glXChooseFBConfig(display, 0, pixmap_config, &c);
if (!configs) {
printf("No appropriate GLX FBConfig available!\n");
} else {
printf("Number of matching configs: %i\n", c);
}
return 0;
}
On any Nvidia graphics card I test using the proprietary driver, I get:
No appropriate GLX FBConfig available!
Using Intel Graphics with Mesa, I get:
Number of matching configs: 82
What am I doing wrong here?
I think the issue comes from the attributes list passed to glXChooseFBConfig (your pixmap_config[]).
I guess some driver may fill required fields with default values, and then compare its internal configurations with the requested one.
The problem is that EXT_texture_from_pixmap only works with pixmaps, not windows.
So, you should set the GLX_DRAWABLE_TYPE field with a mask containing GLX_PIXMAP_BIT and not the default GLX_WINDOW_BIT.
To quote the spec:
attrib_list
Specifies a list of attribute/value pairs. The last attribute must be None.
Some GL implementations, such as Mesa, are more permissive, and will accept GLX_NONE (0x8000) terminating this list of attributes. However, the Nvidia driver does not, and will return NULL. Specifying Xlib's None (0) works. This is also the case for glXCreatePixmap.

Capture image from webcam and display it - OpenCV - Eclipse - Windows

I'm new to OpenCV and I want to display what my webcam sees with OpenCV. I'm using the C Coding Language.
I've tried with this code:
#include <stdio.h>
#include <cv.h> // Include the OpenCV library
#include <highgui.h> // Include interfaces for video capturing
int main()
{
cvNamedWindow("Window", CV_WINDOW_AUTOSIZE);
CvCapture* capture =cvCreateCameraCapture(-1);
if (!capture){
printf("Error. Cannot capture.");
}
else{
cvNamedWindow("Window", CV_WINDOW_AUTOSIZE);
while (1){
IplImage* frame = cvQueryFrame(capture);
if(!frame){
printf("Error. Cannot get the frame.");
break;
}
cvShowImage("Window",frame);
}
cvReleaseCapture(&capture);
cvDestroyWindow("Window");
}
return 0;
}
My webcam's light turns on, but the result is a completely grey window, with no image.
Can you help me?
You need to add
cvWaitKey(30);
to the end of while-loop.
cvWaitKey(x) / cv::waitKey(x) does two things:
It waits for x milliseconds for a key press. If a key was pressed during that time, it returns the key's ASCII code. Otherwise, it returns -1.
It handles any windowing events, such as creating windows with cvNamedWindow(), or showing images with cvShowImage().
A common mistake for opencv newcomers is to call cvShowImage() in a loop through video frames, without following up each draw with cvWaitKey(30). In this case, nothing appears on screen, because highgui is never given time to process the draw requests from cvShowImage().
See What does OpenCV's cvWaitKey( ) function do? for more info.

OpenCV - CvVideoWriter codec error in raspbian

I'm making something like black box in raspberry pi.
I set OpenCV 2.4.3 and many video libraries.
( I referred this site - Opencv cannot acces my webcam )
And I compiled this sample code.
#include <stdio.h>
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include "opencv/cxcore.h"
int main(void){
CvCapture* capture = cvCaptureFromCAM(0);
cvNameWindow("video", 1);
double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
CvSize frame_size = cvSize((int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH), (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT));
CvVideoWriter* writer = cvCreateVideoWriter("out.avi", -1, fps, frame_size, 1);
IpImage* frame;
while(1){
frame = cvQueryFrame(capture);
cvShowImage("video", frame);
if(cvWaitKey(38) == 27){
break;
}
}
cvReleaseVideoWriter(&writer);
cvReleaseCapture(&capture);
cvDestroyWindow("video");
return 0;
}
This code compiled successfully.
But when i run this process, there are some error.
OpenCV Error: Unsupported format or combination of formats (Gstreamer Opencv backend doesn't support this codec acutally.) in CvVideoWriter_GStreamer::open, file /home/pi/OpenCV-2.4.3/modules/highgui/src/cap_gstreamer.cpp, line 479
terminate called after throwing an instance of 'cv::Exception'
what(): /home/pi/OpenCV-2.4.3/modules/highgui/src/cap_gstreamer.cpp:479: error: (-210) Gstreamer Opencv backend doesn't support this codec acutally. in function CvVideoWriter_GStreamer::open
Aborted
So, i changed codec part in 'cvCreateVideoWriter' instead of -1.
I tried many types of codec like 'CV_FOURCC('M','J','P','G')' and so on..
but I cannot fix this problem.
How can i solve this problem? Please help me..

Function cvShowImage make program crash. Why? OpenCv

I am using OpenCV 2.3.1 with Windows 8. When I call function cvShowImage the program crashes. I am using codeblocks and I don't know why this happens.
If I comment the line the program runs well.
The code looks as follows:
IplImage *img1=NULL;
img1=cvLoadImage("LenaComFormas.pgm",CV_LOAD_IMAGE_GRAYSCALE);
cvShowImage("Original", img1);
cvWaitKey(0);
your code is working fine with Qt and opencv 2.4.3 on Windows7...possible checks you can do...
check links to the libraries...opencv_core231 , opencv_highgui231 , opencv_imgproc231...check includes to opencv2/opencv/core.hpp opencv2/opencv/highgui.hpp.
check the IplImage pointer img1
IplImage *img1 = cvLoadImage("",0);
if(img1==NULL)
return -1;
3 . check the support of *.pgm images...or else get the latest opencv and try...
EDIT..
4 . just for check try the following..
#include <opencv/opencv2/core.hpp>
#include <opencv/opencv2/highgui.hpp>
using namespace cv;
int main()
{
Mat image;
image = imread("",0);
imshow("TEST",image);
waitKey(0);
return 0;
}
Because image wasn't load (cvLoadImage returned NULL). You can use debugger to check it. Check the path to the file again.

Resources