I have Windows 8 on "DELL Inspiron 5537" installed and i create a virtual machine from "Virtual box" and installed Ubuntu 13.04 in it. And then I installed Opencv Version=2.4.9 through commands in Ubuntu's terminal now I want to do camera interfacing in opencv which is very necessary for my project to move forward..my code contains no error still it is not working the way i want.
My code is given below:
#include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace std;
char key;
int main()
{
cvNamedWindow("Camera_Output", 1); //Create window
CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
//Capture using any camera connected to your system
while(1){ //Create infinte loop for live streaming
IplImage* frame = cvQueryFrame(capture); //Create image
frames from capture
cvShowImage("Camera_Output", frame); //Show image
frames on created window
key = cvWaitKey(10); //Capture Keyboard stroke
if (char(key) == 27){
//cvSaveImage("webcam_frame.png",frame);
}
}
cvReleaseCapture(&capture); //Release capture.
cvDestroyWindow("Camera_Output"); //Destroy Window
return 0;
}
when I run this code in my terminal it is not capturing the image rather it shows a blank window. I have attached a photo of output of this code.
Related
I have a program here that performs face detection and I would like to use these coordinates to move a window created with GTK+ 3.22 using GTK's
gtk_window_move function. I would like the window to remain open the entire time while it is moving similar to OpenCV's moveWindow function.
I just downloaded the GTK+ packages yesterday so I am not all too familiar.
The program will perform a loop 100 times, tracking a face the entire time. Currently, the face tracking works, but the window does not appear until the loop is complete. Why is this? I believe the gtk_move_window function is working, but the window does not stay open. I have tried reopening the window each time in the loop, or just opening once before the loop. If you are familiar with OpenCV's moveWindow function that is exactly what I am looking for. Here is the sample code.
By the way, if you know how a GTK+ function that will bring the window to the very top layer on top of all the other windows open when called, that is helpful information for me as well.
#include "FlyCapture2.h"
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/cuda.hpp>
#include <opencv2/cudaobjdetect.hpp>
#include <math.h>
#include <thread>
#include <iostream>
#include <vector>
#include <gtk-3.0/gtk/gtk.h>
using namespace FlyCapture2;
cv::Ptr<cv::cuda::CascadeClassifier> face_detect;
void detect_faces(cv::Mat img, cv::cuda::GpuMat buf,GtkWidget *win)
{
std::vector<cv::Rect>faces;
cv::cuda::GpuMat image_gpu(img);
//Face detection here
...
if (faces.size() > 0)
{
float x = faces[0].x;
float y = faces[0].y;
int new_x = roundf(x*40/51);
int new_y = roundf(y*135/256);
gtk_window_move(GTK_WINDOW (win),new_x,new_y);
gtk_widget_show (win); //Should this go here?
std::cout<<faces[0]<<std::endl;
}
}
int main( int argc, char *argv[])
{
//Camera connect here
...
//face detect variables
face_detect = cv::cuda::CascadeClassifier::create("/home/nvidia/opencv/data/haarcascades_cuda/haarcascade_frontalface_default.xml");
cv::cuda::GpuMat objbuf;
//GTK+ Params
GtkWidget *window;
GdkRGBA *color;
gtk_init (&argc, &argv);
gdk_rgba_parse(color,"(0,0,0)");
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_decorated(GTK_WINDOW (window),FALSE);
gtk_window_set_position(GTK_WINDOW (window), GTK_WIN_POS_CENTER);
gtk_widget_override_background_color(window, GTK_STATE_FLAG_NORMAL, color);
gtk_widget_show (win); //Should this go here?
// capture loop
for (int i=0;i<100;i++)
{
// Get the image
Image rawImage;
camera.RetrieveBuffer( &rawImage );
// convert to rgb
Image rgbImage;
rawImage.Convert( FlyCapture2::PIXEL_FORMAT_MONO8, &rgbImage );
// convert to OpenCV Mat
unsigned int rowBytes = (double)rgbImage.GetReceivedDataSize()/(double)rgbImage.GetRows();
cv::Mat image = cv::Mat(rgbImage.GetRows(), rgbImage.GetCols(), CV_8UC1, rgbImage.GetData(),rowBytes);
//Detect Faces
detect_faces(image,objbuf,window);
}
//Disconnect Camera
camera.StopCapture();
camera.Disconnect();
gtk_main();
return 0;
}
The code in your capture loop should be in an event handler callback.
You first need to call g_timeout_add or g_idle_add to register your callback.
The callback you registered is a GSourceFunc that will be called after gtk_main is run. The return value (G_SOURCE_CONTINUE or G_SOURCE_REMOVE) controls if you want to have the callback be called again.
I'm developing a project with visual studio 2010 and opencv. Here is my problem: i acquire a video from webcam, analize it, do some operation on it and then i show the result in another window (Object Tracking). The code is ok, no compiling errors but as soon as i start the program the console windows it closes immediately and i cannot see both the original and the modified video. If i debug the code i can see the webcam works and acquire images but obviously i ned to do this in real time. Any suggestion?
Can you give any code?
Are you write and compile any video player program like this?
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
//Video Capture cap(path_to_video); // open the video file
if(!cap.isOpened()) // check if we succeeded
return -1;
namedWindow("Video",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("Video", frame);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
Try this:
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
int main() {
VideoCapture cap(0);
while (true)
{
Mat imgOriginal;
Mat imgHSV;
bool bSuccess = cap.read(imgOriginal);
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV
imshow("Thresholded Image", imgHSV);
imshow("Original", imgOriginal);
waitKey(33);
}
return 0;
}
This code to display a video using opencv with Visual studio
i have been looking everywhere for a tutorial how to use Qt with opencv to display video
but i couldn't find any :/
is there anyone here knows how to do that?
#include <opencv\highgui.h>
#include <opencv\cv.h>
int main(int argc, char** argv)
{
CvCapture* capture1 = cvCreateFileCapture("c:\\VideoSamples\\song.avi");
IplImage* frame1;
cvNamedWindow( "display video1", CV_WINDOW_AUTOSIZE );
while(1)
{
frame1 = cvQueryFrame( capture1 );
cvSmooth( frame1, out, CV_GAUSSIAN, 17, 17 );
if( !frame1 ) break;
cvShowImage( "display video1", frame1 );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture1 );
cvDestroyWindow( "display video1" );
}
You can easily display a cv::Mat in a QLabel:
Assuming frame is your current RGB-videoframe with 8bit depth as cv::Mat-object and label is a pointer to your QLabel:
//convert to QPixmap:
QPixmap pixmap = QPixmap::fromImage(QImage((uchar*)frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888));
//set scaled pixmap as content:
label->setPixmap(pixmap.scaled(frame.cols, frame.rows, Qt::KeepAspectRatio));
For starters, you've got to make sure that the OpenCV libraries you are using have been built with Qt support.
You will probably need to download the source code (available on Github), configure the build using CMake, and re-build them yourself. Here is the link to the guide on how to build the OpenCV libraries from source.
Once that is done, this is an example of how to capture frames from a camera (just swap camera with file for your case) and display the frames to a window, making use of the Qt framework.
Hope this helps you.
I am using opencv2.4.6.1 on Ubuntu 12.04 LTS. I am new to opencv and have been trying to understand the sample programs in the opencv docs. I am trying to work on a project which takes a picture from a USB webcam (Kinamax Night Vision Camera) and do some image processing on it. I came across a sample code that is shown below:
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
// A Simple Camera Capture Framework
int main()
{
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
if ( !capture ) {
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
return -1;
}
// Create a window in which the captured images will be presented
cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
// Show the image captured from the camera in the window and repeat
while ( 1 ) {
// Get one frame
IplImage* frame = cvQueryFrame( capture );
if ( !frame ) {
fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}
cvShowImage( "mywindow", frame );
// Do not release the frame!
//If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
//remove higher bits using AND operator
if ( (cvWaitKey(10) & 255) == 27 ) break;
}
// Release the capture device housekeeping
cvReleaseCapture( &capture );
cvDestroyWindow( "mywindow" );
return 0;
}
On compiling using:
g++ trycam.c -o trycam `--cflags --libs opencv`
It gives no errors.
When I try to run it using : ./trycam Nothing shows up! Literally Nothing.
On searching google and some other posts in the stackoverflow community, I tried updating the libraries and install other dependencies like ffmpeg,GTK, Gstreamer,etc. I understand that the webcam I have connected via USB is not supported as per the list of webcams supported by linux opencv in the link here. Even my default webcam that is in my HP Pavilion dv6000 is not opening.
Is there a way I could get around this? Kindly help me out.
I'm capturing the webcam image with OpenCV. That works fine. But if I want to close the OpenCV when a button is pressed, it does not work (tried both cvDestroyWindow("NameOfWindow")and cvDestroyAllWindows()). The window stays open and the application is still running.
The OpenCV was initialized on separate thread from the main GUI.
I'm using the Juce Framework with C++ on my Mac. But the same problem occurs also on Windows with Qt and Windows Forms, when the OpenCV Window has it's own cvNamedWindow.
Here is the basic code of the VST plugin editor class:
PluginEditor.cpp
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "PluginProcessor.h"
#include "PluginEditor.h"
//
TestAudioProcessorEditor::TestAudioProcessorEditor (TestAudioProcessor* ownerFilter)
: AudioProcessorEditor (ownerFilter)
{
// This is where our plugin's editor size is set.
setSize (500, 500);
// open the tracker
openTracker();
}
// code for opencv handling
TestAudioProcessorEditor::openTracker() {
// KEY LINE: Start the window thread
cvStartWindowThread();
// Create a window in which the captured images will be presented
cvNamedWindow( "Webcam", CV_WINDOW_AUTOSIZE );
cvWaitKey(0);
cvDestroyWindow( "Webcam" );
// window should disappear!
}
TestAudioProcessorEditor::~TestAudioProcessorEditor()
{
}
// paint stuff on the vst plugin surface
void TestAudioProcessorEditor::paint (Graphics& g) {
}
The piece you may be missing is a call to the cvStartWindowThread function.
On Linux, using the GTK HighGUI, this example reproduced your problem, until I put in the call to cvStartWindowThread.
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include <iostream>
using namespace std;
int main( int argc, char** argv )
{
// KEY LINE: Start the window thread
cvStartWindowThread();
// Open a window
cvNamedWindow("original", 0);
// Wait until a key gets pressed inside the window
cvWaitKey(0);
// Close the window
cvDestroyWindow("original");
// Verify that the window is closed
cout<<"The window should be closed now. (Press ENTER to continue.)"<<endl;
string line;
getline(cin, line);
cout<<"Exiting..."<<endl;
}
If cvStartWindowThread doesn't help, try doing an extra call to cvWaitKey after your cvDestroy call.
To run the example, compile it with GCC:
g++ destroy_window.cpp -o destroy_window -lopencv_core -lopencv_highgui