PDA

View Full Version : Webcam Video Capture



bajoelkid12
1st May 2011, 11:32
Hello everybody..

I'm new in Qt, and new in OpenCV..

I want to make a program to capture a video from USB webcam, then save it to the server..

For now, i have download a source code that use Qt and OpenCV to view images from webcam.. i download it here, http://qt-apps.org/content/show.php/Qt+Opencv+webcam+viewer?content=89995

do you have any suggestion, how should i modifiy this source code, so that it can record the video, and save it ?

i really appreciate your help.. thanks before :)

P.S : sorry for my bad english

stampede
2nd May 2011, 10:01
do you have any suggestion
Yes, read OpenCV documentation, for example:
CvVideoWriter (http://opencv.willowgarage.com/documentation/reading_and_writing_images_and_video.html#CvVideoW riter)
CvWriteFrame (http://opencv.willowgarage.com/documentation/reading_and_writing_images_and_video.html#writefra me)
Post again if you have specific problems with your code.

bajoelkid12
5th May 2011, 17:42
thank you for your respond stampede.. i'm now working on how to integrate OpenCV video writing and reading function in Qt framework.. . maybe you have some advice ?

stampede
5th May 2011, 22:12
You mean you are having some problems with using OpenCv in your code, or just thinking how to do that nicely ? If its the latter, then I'd create a wrapper class(es) with nice interface, which will hide all the OpenCV stuff from client classes. What I mean is something like:


class VideoReader : public QObject{
Q_OBJECT
public:
VideoReader( const QString& file, QObject * parent = NULL );
... /// information
int frameCount() const;
QSize frameSize() const;
... // other methods, like video fps etc
/// capture methods
QImage getFrame( int frameNumber ) const; // or return QPixmap, or your own Image class
...
};

It's always better to have class with good interface, than C-style OpenCv api calls all over the code. I think you get the idea.

bajoelkid12
10th May 2011, 05:52
actually yes.. i have some problems with using OpenCV in my code.. this is what i've done so far..

mycamerawindow.cpp


#include "MyCameraWindow.h"

MyCameraWindow::MyCameraWindow(CvCapture *cam, QWidget *parent) : QWidget(parent) {
camera = cam;
QVBoxLayout *layout = new QVBoxLayout;
cvwidget = new QOpenCVWidget(this);
layout->addWidget(cvwidget);
setLayout(layout);
resize(500, 400);

startTimer(100); // 0.1-second timer
}

void MyCameraWindow::timerEvent(QTimerEvent*) {
IplImage *image=cvQueryFrame(camera);
cvwidget->putImage(image);
}


i get this code from http://qt-apps.org/content/show.php/Qt+Opencv+webcam+viewer?content=89995

btw thank you for the algorithm, now i'll try to implement it to my code.. :) but if you don't mind, may i send my project file to you ?

stampede
10th May 2011, 09:13
actually yes.. i have some problems with using OpenCV in my code..
What kind of problems ? Image is not displayed ? Displayed but incorrect ? Other ?

but if you don't mind, may i send my project file to you ?
What for ? I think you can get all the help you need by asking specific questions on this forum.

bajoelkid12
10th May 2011, 12:02
actually.. i don't know how to start it.. i'm new in programming.. new in Qt and OpenCV..i've think about some algorithm that i'll use in my work, it's somewhat like :

1. Detect camera
2. If there is a camera, then initialize the camera
3. (the images from the camera are displayed)
4. then there will be some buttons for recording, pause, and stop functions
5. if the recording button is clicked, then save the recorded video into a spesific place and certain format.

that's all, and i just don't know how to implement those algorithm into codes..i don't know how to write them.. actually, this is my first experience..

Any advice Mr.Stampede ? i'm so sorry for the silly question from me..i really appreciate your help

Added after 6 minutes:

oh yes, for the basic need of integration (OpenCV2.2 and QtCreator 2010.05), i have read it here

http://www.barbato.us/2011/03/18/using-opencv2-within-qtcreator-in-windows-2/#more-1625

now it's just "how to write the code" problems

stampede
10th May 2011, 12:19
If you are new both to Qt and OpenCV, then in my opinion best for you would be to write some simple separate applications using Qt and OpenCV, and start combining the two when you gain some experience with those libraries.

i'm new in programming..
In that case, I'd start with basic C/C++ before trying to code using Qt.

bajoelkid12
10th May 2011, 13:06
In that case, I'd start with basic C/C++ before trying to code using Qt


Do you mean TURBO C++ application ? or there is anyother ?

squidge
10th May 2011, 14:15
Yes, there is standard C++.

You can use Qt Creator to build C++ applications as well as Qt application. I would advise to learn C++ before Qt.

bajoelkid12
24th May 2011, 17:08
hello guys, i'm a bit confused on how to understand a following code


#include "QOpenCVWidget.h"

// Constructor
QOpenCVWidget::QOpenCVWidget(QWidget *parent) : QWidget(parent) {
layout = new QVBoxLayout;
imagelabel = new QLabel;
QImage dummy(100,100,QImage::Format_RGB32);
image = dummy;
layout->addWidget(imagelabel);
for (int x = 0; x < 100; x ++) {
for (int y =0; y < 100; y++) {
image.setPixel(x,y,qRgb(x, y, y));
}
}
imagelabel->setPixmap(QPixmap::fromImage(image));

setLayout(layout);
}

QOpenCVWidget::~QOpenCVWidget(void) {

}

void QOpenCVWidget::putImage(IplImage *cvimage) {
int cvIndex, cvLineStart;
// switch between bit depths
switch (cvimage->depth) {
case IPL_DEPTH_8U:
switch (cvimage->nChannels) {
case 3:
if ( (cvimage->width != image.width()) || (cvimage->height != image.height()) ) {
QImage temp(cvimage->width, cvimage->height, QImage::Format_RGB32);
image = temp;
}
cvIndex = 0; cvLineStart = 0;
for (int y = 0; y < cvimage->height; y++) {
unsigned char red,green,blue;
cvIndex = cvLineStart;
for (int x = 0; x < cvimage->width; x++) {
// DO it
red = cvimage->imageData[cvIndex+2];
green = cvimage->imageData[cvIndex+1];
blue = cvimage->imageData[cvIndex+0];

image.setPixel(x,y,qRgb(red, green, blue));
cvIndex += 3;
}
cvLineStart += cvimage->widthStep;
}
break;
default:
printf("This number of channels is not supported\n");
break;
}
break;
default:
printf("This type of IplImage is not implemented in QOpenCVWidget\n");
break;
}
imagelabel->setPixmap(QPixmap::fromImage(image));
}


Can you help me ?

high_flyer
24th May 2011, 17:10
Its quite a bit of code, with which part of it do you have trouble exactly?

bajoelkid12
24th May 2011, 17:20
thank you for the response.. .

first of all, what is the function of this code in your opinion ?

in my opinion, this code is to draw an image wrapped in Qimage, with RGB32 format.. but i don't know actually the drawing process based on that code.. .

high_flyer
24th May 2011, 17:27
in my opinion, this code is to draw an image wrapped in Qimage, with RGB32 format..
yes, but not only, it seems to convert BGR to RGB as well.


for (int x = 0; x < cvimage->width; x++) {
// DO it
red = cvimage->imageData[cvIndex+2];
green = cvimage->imageData[cvIndex+1];
blue = cvimage->imageData[cvIndex+0];

image.setPixel(x,y,qRgb(red, green, blue));
cvIndex += 3;
}

bajoelkid12
26th May 2011, 11:06
Hello guys, now i want to ask about video compression.. .is there any library that can support video compression method ? such as mpeg, or avi ? i'm using OpenCV2.2 right now. does it support video compression too ? thanks

high_flyer
26th May 2011, 11:28
Why do you ask it here?
This is a Qt forum, not OpenCV froum.
Try reading the OpenCV documentation first.
http://opencv.willowgarage.com/wiki/VideoCodecs

Ans ask OpenCV related question on their forum. you are more likely to get better answers there for OpenCV related questions.

bajoelkid12
28th May 2011, 15:05
hello guys.. now i want to ask, how to build a client - server application to transfer images that captured by webcam to another part of the network ? any suggestion ? thanks.. .

squidge
28th May 2011, 16:59
Maybe QNetworkAccessManager, or just plain old QTcpSocket, depending on what your server requires.

bajoelkid12
30th May 2011, 09:52
my server require to read the video stream that was sent by client.. .what do you suggest, QTcpSocket or QUdpSocket ? thank you

squidge
30th May 2011, 10:04
Which does your server support?

bajoelkid12
30th May 2011, 10:14
accept the video stream then display it

squidge
30th May 2011, 10:48
If you are designing the server as well as the client, then I would recommend TCP, otherwise you need to know which the server supports.

bajoelkid12
30th May 2011, 16:33
thank you for the suggestion squide. may i ask another suggestion ?

here is my program.

at the client side, the program captures video the transmit it via wlan to the server side.

at the server side, the program plays the video.

i have the source code for the capturing stuff and play the video stuff. but i'm stuck with the client - server video transfer stuff.

thanks in advance

squidge
30th May 2011, 20:11
So what is the question? You do not know how to use QTCPSocket?

bajoelkid12
31st May 2011, 17:23
use QUdpSocket.. .i have a made a simple client server class.. the problem how to put my stream into the socket.. here is my client - server class



#include "jcxudp.h"

jcxUDP::jcxUDP(QObject *parent) :
QObject(parent)
{
socket = new QUdpSocket(this);
socket->bind(QHostAddress::LocalHost,5678);
connect(socket,SIGNAL(readyRead()),this,SLOT(SiapB aca()));
}

void jcxUDP::NulisHallo()
{
QByteArray Data;
Data.append("halo dari jincheng");
socket->writeDatagram(Data,QHostAddress::LocalHost,5678);
}

void jcxUDP::SiapBaca()
{
QByteArray Buffer;
Buffer.resize(socket->pendingDatagramSize());

QHostAddress sender;
quint16 senderPort;
socket->readDatagram(Buffer.data(),Buffer.size(),&sender,&senderPort);

qDebug() << "Pesan Dari :" << sender.toString();
qDebug() << "Port Pesan :" << senderPort;
qDebug() << "Pesan :" << Buffer;
}

stampede
31st May 2011, 17:37
Sorry to interrupt, but this is getting a little off topic
@bajoelkid12: I suggest you create new thread about your socket problems, "Webcam Video Capture" no longer describes this thread correctly. Imagine someone searching the forum in the future for video capturing problems, ending up on some sort of server code :)

bajoelkid12
31st May 2011, 17:43
ahh.. thanks a lot Stampede :) i'll create a new thread then :)

adeline
18th November 2011, 01:41
You guys can use Screen Recording Suite to help you record video from webcam.

Thành Viên Mới
25th December 2011, 03:28
actually yes.. i have some problems with using OpenCV in my code.. this is what i've done so far..

mycamerawindow.cpp


#include "MyCameraWindow.h"

MyCameraWindow::MyCameraWindow(CvCapture *cam, QWidget *parent) : QWidget(parent) {
camera = cam;
QVBoxLayout *layout = new QVBoxLayout;
cvwidget = new QOpenCVWidget(this);
layout->addWidget(cvwidget);
setLayout(layout);
resize(500, 400);

startTimer(100); // 0.1-second timer
}

void MyCameraWindow::timerEvent(QTimerEvent*) {
IplImage *image=cvQueryFrame(camera);
cvwidget->putImage(image);
}


i get this code from http://qt-apps.org/content/show.php/Qt+Opencv+webcam+viewer?content=89995

btw thank you for the algorithm, now i'll try to implement it to my code.. :) but if you don't mind, may i send my project file to you ?

if you use your code, you cant record with audio, how to record video with audio use OpenCV ???