PDA

View Full Version : color image + openCV



vestaproman
October 12, 2010, 12:11:54
Dear all,

I used this post to grab image from an analog camera with openCV

http://www.theimagingsourceforums.com/showthread.php?t=323456&highlight=opencv

I receive only a gray image.

How can I have color image.

Best regards

VPM

Ubuntu
November 16, 2010, 04:38:55
i had carried out it !
use callback function:
Create eRGB24 color fomat at the first,and then create a IplImage like this imagesr=cvCreateImage(cvSize(width,height),8,3);
in callback funtion ,you can get the data by these codes:
imagesr->imageData=(char*)pBuffer->getPtr();

it runs well~~~

Stefan Geissler
November 16, 2010, 08:54:35
Hello,

somehow I must have missed your post 4 weeks ago. However, you are right, its simply createing the correct CVImage header and pass the pointer of the IC Membuffer to this structure.

vestaproman
December 22, 2010, 11:56:20
Thanks for your reply.
I did not receive the reply notification and I just came back in the forum.
It works great

Thanks a lot

Best wishes for next year

VPM

moisdc
February 23, 2011, 18:31:17
Hello,

I have not had much success. I have put this before the main:


struct RGB24Pixel {
BYTE b;
BYTE g;
BYTE r;
};

Then I have put this.


img = cvCreateImageHeader( cvSize(info.dim.cx,info.dim.cy),8,1)

while( (img->imageData = (char*)pSink->getLastAcqMemBuffer()->getPtr()) !=NULL)
{
int key;

//img->imageData = (char*)pSink->getLastAcqMemBuffer()->getPtr();
//img->imageData=(char*)pBuffer->getPtr();

if(!img) break;

cvShowImage("Original Image", img);

key = cvWaitKey(10);
}

Can somebody help me?

Thanks in advance

-- Moises Diaz-Cabrera

Stefan Geissler
February 24, 2011, 12:27:53
Hello Moises,

I guess, you create the wrong OpenCV image type. The last parameter "1" is the count of bytes per pixel, that means you only use an RGB8 or Y800 image, whil your IC sink type may is set to RGB24. You can initialize your OpenCV image as follows


if( img == NULL )
{
smart_ptr<BITMAPINFOHEADER> pInf = pSink->getLastAcqMemBuffer()->getBitmapInfoHeader();
// Calculate the size of the image.

imgSize.width = pInf->biWidth;
imgSize.height = pInf->biHeight;

img = cvCreateImageHeader( imgSize, IPL_DEPTH_8U, pInf->biBitCount / 8 );
}

Your while loop looks fine to me.

vestaproman
February 24, 2011, 14:43:30
Yes I thinks the same as Stephan, you need to have 3 channels
img = cvCreateImageHeader( cvSize(info.dim.cx,info.dim.cy),8,3)

regards,

VPM

moisdc
February 24, 2011, 16:30:58
Dear colleagues,

I have tried everything that you have said to me. Thank you so much. However, I don't get to run successfully my code.

The Debug stopped in this point.


cvShowImage("Original_Image", img);

Thank you very much for your help.

PS: I have attached my code.

Stefan Geissler
February 24, 2011, 16:40:48
Helo Moises,

the line

tFrameHandlerSinkPtr pSink = FrameHandlerSink::create( eY800, 1 );
Creates one byte per pixel grayscale in the sink. Please exchange it against

tFrameHandlerSinkPtr pSink = FrameHandlerSink::create( eRGB24 , 2 );
Also the ringbuffer should contain at least 2 images, otherwise your application will be unnecessary slow.

moisdc
February 24, 2011, 17:17:45
Dear Stefan,

I have done the change, but it does not work properly. Now, I cannot see a gray image, but a distorted gray image.

moisdc
February 24, 2011, 17:32:38
Currently, I have connected the DFGUSB2 (Video to USB Conv) to my television and to my laptop.
I don't know if this could help to solve the problem.
Thanks in advance.
-- Moises Diaz-Cabrera

vestaproman
February 25, 2011, 09:29:18
Hello,

Here my code using OPENCV on visual studio 2008 with DSG\USB2-it et a PAL camera.

This code work fine for me, Hope it will help you

Regards,

VPM



#include "stdafx.h"
#include <tisudshl.h>


int _tmain(int argc, _TCHAR* argv[])
{
bool result;
result = DShowLib::InitLibrary( "XXXXXXXXX");
if (result == false)
{
printf("FAILED\n");
cin.get();
}

Grabber grabber;
setupDeviceFromFile( grabber );
grabber.setOverlayBitmapPathPosition( ePP_NONE );
tFrameHandlerSinkPtr pSink = FrameHandlerSink::create( eRGB24, 2 );
pSink->setSnapMode( true );
grabber.setSinkType( pSink );
result = grabber.prepareLive( false );

if (result == false)
{
printf("FAILED\n");
cin.get();
}

FrameTypeInfo info;
pSink->getOutputFrameType( info );

IplImage *img;
IplImage *imgNDG;
img = cvCreateImage( cvSize(info.dim.cx,info.dim.cy), IPL_DEPTH_8U , 3 );
imgNDG = cvCreateImage( cvSize(info.dim.cx,info.dim.cy), IPL_DEPTH_8U , 1 );

BYTE* pBuf;
pBuf = new BYTE[info.buffersize];
tMemBufferCollectionPtr pCollection = MemBufferCollection::create( info, 1, &pBuf );
pSink->setMemBufferCollection( pCollection );
grabber.startLive();

while(1)
{

// acquisition image
pSink->snapImages(1);
img->imageData = (char*)pSink->getLastAcqMemBuffer()->getPtr();
cvConvertImage(img,imgNDG,CV_RGB2GRAY);

cvShowImage( "Resultat", img );
cvShowImage( "GRAY", imgNDG );


char c = cvWaitKey(20);
if( c == 27 ) break;
}

grabber.stopLive();

cvReleaseImage(&img);
cvReleaseImage(&imgNDG);
delete [] pBuf;

printf("We are done\n");
cin.get();

return 0;
}

moisdc
February 25, 2011, 11:12:28
thank you vestaproman,

I have tried your code and it will work fine. However I have changed a little the code.


setupDeviceFromFile( grabber );
grabber.setOverlayBitmapPathPosition( ePP_NONE );


Grabber::tVidCapDevListPtr pVidCapDevList = grabber.getAvailableVideoCaptureDevices();
grabber.openDev(pVidCapDevList->at(0));

If I use your code, Visual Studio 2008 will stop in this point: setupDeviceFromFile( grabber );

Have you used IC Control 3.1? I use IC Control 3.0, which I have downloaded freely for this website: http://www.imagingcontrol.com/en_US/library/cpp/

Now, the color image is rotated 180 degrees.
Thanks so much again

--Moises Diaz-Cabrera

moisdc
February 25, 2011, 11:17:08
Moreover, I have put these includes:

#include "stdafx.h"
#include "tisudshl.h"
#include "cv.h"
#include "highgui.h"
#include <math.h>

vestaproman
February 25, 2011, 11:27:56
I use 3.1
and I put that inside the stdafx :



#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
using namespace std;

// TODO: reference additional headers your program requires here
#include "tisudshl.h"
using namespace _DSHOWLIB_NAMESPACE;

#include "cv.h"
#include "cxcore.h"
#include "highgui.h"

#include "D:\\Developpements\\IC Imaging Control 3.1\\samples\\vc9\\Common\\CmdHelper.h"



Regards,

VPM

moisdc
February 25, 2011, 11:47:05
Thanks VPM,
I will try to get IC control 3.1
Yours sincerely
-- Moises Diaz-Cabrera
ps: Attach my current code for IC Control 3.0, although yet the color image is rotated 180 degrees


#include "stdafx.h"
#include "tisudshl.h"
#include "cv.h"
#include "highgui.h"
#include <math.h>

int _tmain(int argc, _TCHAR* argv[])
{
bool result;
result = DShowLib::InitLibrary( "ISB3200016679");
if (result == false)
{
printf("FAILED\n");
cin.get();
}

Grabber grabber;
Grabber::tVidCapDevListPtr pVidCapDevList = grabber.getAvailableVideoCaptureDevices();
grabber.openDev(pVidCapDevList->at(0));
//setupDeviceFromFile( grabber ); funcion del IC Control 3.1
grabber.setOverlayBitmapPathPosition( ePP_NONE );
tFrameHandlerSinkPtr pSink = FrameHandlerSink::create( eRGB24, 2 );
pSink->setSnapMode( true );
grabber.setSinkType( pSink );
result = grabber.prepareLive( false );

if (result == false)
{
printf("FAILED\n");
cin.get();
}

FrameTypeInfo info;
pSink->getOutputFrameType( info );

IplImage *img = 0;
IplImage *imgNDG;
img = cvCreateImage( cvSize(info.dim.cx,info.dim.cy), IPL_DEPTH_8U , 3 );
imgNDG = cvCreateImage( cvSize(info.dim.cx,info.dim.cy), IPL_DEPTH_8U , 1 );

BYTE* pBuf;
pBuf = new BYTE[info.buffersize];
tMemBufferCollectionPtr pCollection = MemBufferCollection::create( info, 1, &pBuf );
pSink->setMemBufferCollection( pCollection );
grabber.startLive();

while(1)
{

// acquisition image
pSink->snapImages(1);
img->imageData = (char*)pSink->getLastAcqMemBuffer()->getPtr();
cvConvertImage(img,imgNDG,CV_RGB2GRAY);

cvShowImage( "Resultat", img );
cvShowImage( "GRAY", imgNDG );


char c = cvWaitKey(20);
if( c == 27 ) break;
}

grabber.stopLive();

cvReleaseImage(&img);
cvReleaseImage(&imgNDG);
delete [] pBuf;

printf("We are done\n");
cin.get();

return 0;
}

moisdc
February 25, 2011, 18:23:26
Currently I'm using the ic control 3.2 and it works well.
I attach the code.
Thanks so much for your help


#include "stdafx.h"
#include "tisudshl.h"
#include "cv.h"
#include "highgui.h"
#include <math.h>

int _tmain(int argc, _TCHAR* argv[])
{
bool result;
result = DShowLib::InitLibrary( "ISB3200016679");
if (result == false)
{
printf("FAILED\n");
cin.get();
}

Grabber grabber;
setupDeviceFromFile( grabber ); //funcion del IC Control 3.1
grabber.setOverlayBitmapPathPosition( ePP_NONE );
tFrameHandlerSinkPtr pSink = FrameHandlerSink::create( eRGB24, 2 );
pSink->setSnapMode( true );
grabber.setSinkType( pSink );
result = grabber.prepareLive( false );

/* poner en la salida:
PAL_B
UYVY (768x576)
00 Video:Composite
FPS: 25.00
*/

if (result == false)
{
printf("FAILED\n");
cin.get();
}

FrameTypeInfo info;
pSink->getOutputFrameType( info );

IplImage *img = 0;
IplImage *imgNDG = 0;
CvSize size = cvSize(info.dim.cx,info.dim.cy);
img = cvCreateImage( size, IPL_DEPTH_8U , 3 );
imgNDG = cvCreateImage( size, IPL_DEPTH_8U , 1 );

BYTE* pBuf;
pBuf = new BYTE[info.buffersize];
tMemBufferCollectionPtr pCollection = MemBufferCollection::create( info, 1, &pBuf );
pSink->setMemBufferCollection( pCollection );
grabber.startLive();


while(1)
{
// acquisition image
pSink->snapImages(1);
img->imageData = (char*)pSink->getLastAcqMemBuffer()->getPtr();
cvConvertImage(img,imgNDG,CV_RGB2GRAY);
cvFlip( img, img );
cvShowImage( "Resultat", img );
cvShowImage( "GRAY", imgNDG );


char c = cvWaitKey(20);
if( c == 27 ) break;
}

grabber.stopLive();

cvReleaseImage(&img);
cvReleaseImage(&imgNDG);
delete [] pBuf;

printf("We are done\n");
cin.get();

return 0;
}

My next step is saved the video. I have used this

CvVideoWriter *writer = 0;
int isColor = 1;
int fps = 15; // or 30
writer = cvCreateVideoWriter("salida.avi",CV_FOURCC('I','Y','U','V'),fps,size,isColor);

and this:

cvWriteFrame(writer,img)
but I haven't achieved the target yet.

moisdc
February 26, 2011, 21:04:52
Thanks for your help.
Now, I would like to save the video with opencv.
In this new forum I have opened a new problem and I have put the successful code: http://www.theimagingsourceforums.com/showthread.php?t=323700
Thanks in advance for your help again.
Yours sincerely
--Moises Diaz-Cabrera

Stefan Geissler
February 28, 2011, 09:35:25
Moises,

please understand, we do no programming support for third party software. Please ask in the appropriate OpenCV forums.