nomis_ch
2011-10-20 08:59:15 UTC
Hello !
General informations:
I am working on a framework to record and process multiple stream from
different sensors. (based on CRNT)I am actually implementing the
webcam/kinect part.
I am using OpenCV to retrieve and process images.Retrieving and saving
images is fine, but I have problem displaying the images in a window.
The framework si based on different modules (threads) each handling some
part of the information and sending it to a different module.
For webcam I coded the following modules: WebcamReader (to read images
from a camera), WebcamWriter (to save images as an avi file) and
WebcamVisualizer to display the video on the screen.
I use opencv 2.3.1, on macosx 10.6.8 with MacPorts.
Problem:
When I try to display the images, the first received frame is displayed
in the window but then the window is not updated anymore (but I still
receive new frames which are not displayed) !From what I read, the
problem is because the imshow and waitkey functions are called from
within a secondary thread, but I have not seen any solution.
Tried solutions
I tried to increase the time for waitkey(), to create the namedWindow at
different places to call sleep() to ensure the thread let enough tiem to
display the image.
Note that I made a small test with the same code with a simple
application (within the main and not a thread), and the image gets
updated without problem.
Is there anyway I can force the update of the window through a signal or
anything ?
Code:
/*
TO COMPLETE
Module to display an image/video received through a connection stream.
*/
#include "WebcamVisualizer.h"
using namespace std;
WebcamVisualizer::WebcamVisualizer( ) : StreamTask(1,0)
{
//Constructor
cv::namedWindow("WebcamWindow", CV_WINDOW_AUTOSIZE);
}
WebcamVisualizer::WebcamVisualizer( const WebcamVisualizer& r) :
StreamTask(r)
{
log("Copy constructor");
}
WebcamVisualizer::~WebcamVisualizer()
{
log("WebcamVisualizer: released (destructor)");
//cv::destroyAllWindows();
}
void WebcamVisualizer::paramsChanged(){
log("WebcamVisualizer: params changed");
}
void WebcamVisualizer::run()
{
//INIT
printf("Webcam: Starting visualizer thread\n");
DataPacket *p;
InPort *inPort = inPorts[0];
//init variables for time statistics
struct timeval start, end, actual;
long mtime, seconds, useconds;
//Get an image to initialize parameters
p = inPort->receive();
if(p==NULL){
printf("WebCamVisualizer: Failed to initialize resolution with received
image");
}else{
//do something with p
Value *val = p->dataVector[0];
cv::Mat mat = (dynamic_cast<CvMatValue*>(val))->getCvMat();
}
int nFrame = 0;
while (running) {
//Read inPort
if(p == NULL){
if(inPort->notEmpty()){
p=inPort->receive();
}
}else{
gettimeofday(&start, NULL);
//Read header
//Read cv::Mat
Value *val = p->dataVector[0];
//Display image
cv::imshow("WebcamWindow",
(dynamic_cast<CvMatValue*>(val))->getCvMat());
cv::waitKey(20);
gettimeofday(&end, NULL);
if(debug){
seconds = end.tv_sec - start.tv_sec;
useconds= end.tv_usec - start.tv_usec;
mtime += ((seconds)*1000 + useconds/1000.0)+0.5;
}
delete p;
p=NULL;
nFrame++;
}
}
if(debug){
//printf("WebCamWriter: received #%d frames, displayed with %dx%d
resolution \n", nFrame, resolutionW, resolutionH);
printf("WebCamWriter: Average time taken pro frame: %.2f
milliseconds\n", (mtime/((double)nFrame)));
fflush(stdout);
}
log("Running Visualizer finished succesfully");
}
General informations:
I am working on a framework to record and process multiple stream from
different sensors. (based on CRNT)I am actually implementing the
webcam/kinect part.
I am using OpenCV to retrieve and process images.Retrieving and saving
images is fine, but I have problem displaying the images in a window.
The framework si based on different modules (threads) each handling some
part of the information and sending it to a different module.
For webcam I coded the following modules: WebcamReader (to read images
from a camera), WebcamWriter (to save images as an avi file) and
WebcamVisualizer to display the video on the screen.
I use opencv 2.3.1, on macosx 10.6.8 with MacPorts.
Problem:
When I try to display the images, the first received frame is displayed
in the window but then the window is not updated anymore (but I still
receive new frames which are not displayed) !From what I read, the
problem is because the imshow and waitkey functions are called from
within a secondary thread, but I have not seen any solution.
Tried solutions
I tried to increase the time for waitkey(), to create the namedWindow at
different places to call sleep() to ensure the thread let enough tiem to
display the image.
Note that I made a small test with the same code with a simple
application (within the main and not a thread), and the image gets
updated without problem.
Is there anyway I can force the update of the window through a signal or
anything ?
Code:
/*
TO COMPLETE
Module to display an image/video received through a connection stream.
*/
#include "WebcamVisualizer.h"
using namespace std;
WebcamVisualizer::WebcamVisualizer( ) : StreamTask(1,0)
{
//Constructor
cv::namedWindow("WebcamWindow", CV_WINDOW_AUTOSIZE);
}
WebcamVisualizer::WebcamVisualizer( const WebcamVisualizer& r) :
StreamTask(r)
{
log("Copy constructor");
}
WebcamVisualizer::~WebcamVisualizer()
{
log("WebcamVisualizer: released (destructor)");
//cv::destroyAllWindows();
}
void WebcamVisualizer::paramsChanged(){
log("WebcamVisualizer: params changed");
}
void WebcamVisualizer::run()
{
//INIT
printf("Webcam: Starting visualizer thread\n");
DataPacket *p;
InPort *inPort = inPorts[0];
//init variables for time statistics
struct timeval start, end, actual;
long mtime, seconds, useconds;
//Get an image to initialize parameters
p = inPort->receive();
if(p==NULL){
printf("WebCamVisualizer: Failed to initialize resolution with received
image");
}else{
//do something with p
Value *val = p->dataVector[0];
cv::Mat mat = (dynamic_cast<CvMatValue*>(val))->getCvMat();
}
int nFrame = 0;
while (running) {
//Read inPort
if(p == NULL){
if(inPort->notEmpty()){
p=inPort->receive();
}
}else{
gettimeofday(&start, NULL);
//Read header
//Read cv::Mat
Value *val = p->dataVector[0];
//Display image
cv::imshow("WebcamWindow",
(dynamic_cast<CvMatValue*>(val))->getCvMat());
cv::waitKey(20);
gettimeofday(&end, NULL);
if(debug){
seconds = end.tv_sec - start.tv_sec;
useconds= end.tv_usec - start.tv_usec;
mtime += ((seconds)*1000 + useconds/1000.0)+0.5;
}
delete p;
p=NULL;
nFrame++;
}
}
if(debug){
//printf("WebCamWriter: received #%d frames, displayed with %dx%d
resolution \n", nFrame, resolutionW, resolutionH);
printf("WebCamWriter: Average time taken pro frame: %.2f
milliseconds\n", (mtime/((double)nFrame)));
fflush(stdout);
}
log("Running Visualizer finished succesfully");
}