Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

CrunchDown

Reading in a web cam for image acquisition

This topic is 5091 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Wonder if anybody knows how to do this. I heard it can be done using DirectPlay. Does anybody have any information about it, or know of a fairly simple way to do it. From what I hear, DirectX is quite alot of trouble. Will be coding a software eye that can convert images to 3d data Thanks in advance for help.

Share this post


Link to post
Share on other sites
Advertisement
Not DirectPlay, the actual API is DirectShow. DirectPlay is DirectX''s answer to networking, and it is suprisingly intuitive. And actually, I have some experience with DirectShow in particualar and found it very easy to use. Direct3D is notoriously difficult, but it is very possible to only use the DirectShow api in conjunction with OpenGL (or some other program) After reading the tutorials for DirectShow, In under three hours I had a program that could take movie data imported through directshow Mpeg1/2/4/Avi filters (the filter manager can easily be configured to use a capture filter graph) and display it on an openGL textured quad. It isnt hard to do at all.



--------------------------------------

It took me long enough, but now I am finally 16. Yay! Sorta funny, but I am probably the only person on earth who wrote a 3D driving game prior to actually driving .

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Thanks steve. Will look up some Direct Show tuts.

Share this post


Link to post
Share on other sites
Here''s some horrible code that uses DirectShow to grab images from a webcam. I have a much neater version, but it''s split across multiple files so it''s not really possible to post here. Hopefully you''ll be able to figure out what''s going on here:
#define coCreateInstance(a, b, c) hResult = CoCreateInstance(CLSID_##a##, null, CLSCTX_INPROC_SERVER, IID_I##b##, (void**)&##c##);
#define queryInterface(a, b, c) hResult = a##->QueryInterface(IID_I##b##, (void**)&##c##);
#define release(a) hResult = a##->Release();

const class Null
{

public:

template <typename TYPE>
operator TYPE*() const
{
return 0;
}

template <typename CLASS, typename TYPE>
operator TYPE CLASS::*() const
{
return 0;
}

template <typename TYPE>
friend bool operator==(const TYPE* const t, const Null& n)
{
return t == (TYPE*)n;
}

} null;

class HResult
{

public:

HResult& operator=(HRESULT hr)
{
if (FAILED(hr))
{
std::cout << "Error: " << (unsigned int)hr << ''\n'';
if (hr == S_OK)
{
std::cout << "S_OK\n";
}
if (hr == VFW_S_PARTIAL_RENDER)
{
std::cout << "VFW_S_PARTIAL_RENDER\n";
}
if (hr == E_ABORT)
{
std::cout << "E_ABORT\n";
}
if (hr == E_POINTER)
{
std::cout << "E_POINTER\n";
}
if (hr == VFW_E_CANNOT_CONNECT)
{
std::cout << "VFW_E_CANNOT_CONNECT\n";
}
if (hr == VFW_E_NOT_IN_GRAPH)
{
std::cout << "VFW_E_NOT_IN_GRAPH\n";
}
throw hr;
}
return *this;
}

} hResult;

class ComUninitialise
{

public:

static void inc();
static void dec();

private:

static int c;

};

void ComUninitialise::inc()
{
c++;
}

void ComUninitialise::dec()
{
c--;
if (c == 0)
{
CoUninitialize();
}
}

int ComUninitialise::c = 0;

template <typename COM_CLASS>
class ComPointer
{

public:

ComPointer();
ComPointer(COM_CLASS* comInterface);
~ComPointer();
ComPointer<COM_CLASS>& operator=(COM_CLASS* comInterface);
COM_CLASS* operator->() const;
operator COM_CLASS*() const;

private:

ComPointer(const ComPointer<COM_CLASS>& comInterface);
ComPointer<COM_CLASS>& operator=(const ComPointer<COM_CLASS>& comInterface);

COM_CLASS* i;

};

template <typename COM_CLASS>
ComPointer<COM_CLASS>::ComPointer()
:
i(null)
{
}

template <typename COM_CLASS>
ComPointer<COM_CLASS>::ComPointer(COM_CLASS* comInterface)
:
i(comInterface)
{
ComUninitialise::inc();
}

template <typename COM_CLASS>
ComPointer<COM_CLASS>::~ComPointer()
{
release(i);
ComUninitialise::dec();
}

template <typename COM_CLASS>
ComPointer<COM_CLASS>& ComPointer<COM_CLASS>::operator=(COM_CLASS* comInterface)
{
if (i)
{
release(i);
}
i = comInterface;
}

template <typename COM_CLASS>
COM_CLASS* ComPointer<COM_CLASS>::operator->() const
{
return i;
}

template <typename COM_CLASS>
ComPointer<COM_CLASS>::operator COM_CLASS*() const
{
return i;
}

ComPointer<IGraphBuilder> graphBuilder;
ComPointer<IBaseFilter> sampleGrabberFilter;
ComPointer<IBaseFilter> nullRenderer;
ComPointer<ISampleGrabber> sampleGrabber;
ComPointer<IMediaControl> mediaControl;
ComPointer<IMediaEvent> mediaEvent;
long sizeBuffer = 0;
unsigned char* pBuffer;

void setup()
{
hResult = CoInitialize(null);
coCreateInstance(FilterGraph, GraphBuilder, graphBuilder);
coCreateInstance(SampleGrabber, BaseFilter, sampleGrabberFilter);
queryInterface(sampleGrabberFilter, SampleGrabber, sampleGrabber);
coCreateInstance(NullRenderer, BaseFilter, nullRenderer);
hResult = graphBuilder->AddFilter(nullRenderer, L"Null Renderer");
AM_MEDIA_TYPE mt;
ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE));
mt.majortype = MEDIATYPE_Video;
mt.subtype = MEDIASUBTYPE_RGB24;
hResult = sampleGrabber->SetMediaType(&mt);
hResult = graphBuilder->AddFilter(sampleGrabberFilter, L"Sample Grabber");
ICreateDevEnum* deviceEnumeratorCreator;
coCreateInstance(SystemDeviceEnum, CreateDevEnum, deviceEnumeratorCreator);
IEnumMoniker* deviceEnumerator;
hResult = deviceEnumeratorCreator->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &deviceEnumerator, 0);
IMoniker* moniker = null;
IBaseFilter* filter;
while (deviceEnumerator->Next(1, &moniker, null) == S_OK)
{
IPropertyBag* propertyBag;
try
{
hResult = moniker->BindToStorage(0, 0, IID_IPropertyBag, (void**)&propertyBag);
try
{
VARIANT variantName;
VariantInit(&variantName);
try
{
hResult = propertyBag->Read(L"Description", &variantName, 0);
}
catch (HRESULT)
{
hResult = propertyBag->Read(L"FriendlyName", &variantName, 0);
}
USES_CONVERSION;
VariantClear(&variantName);
hResult = moniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&filter);
while(deviceEnumerator->Next(1, &moniker, null) == S_OK)
{
}
}
catch (HRESULT hr)
{
}
release(propertyBag);
}
catch (HRESULT hr)
{
}
release(moniker);
}
release(deviceEnumerator);
release(deviceEnumeratorCreator);
hResult = graphBuilder->AddFilter(filter, L"Capture Filter");
IEnumPins* pEnum;
filter->EnumPins(&pEnum);
IPin* pCameraOutput;
IPin* pGrabberInput;
IPin* pGrabberOutput;
IPin* pNullInputPin;

hResult = pEnum->Reset();
hResult = pEnum->Next(1, &pCameraOutput, NULL);

release(pEnum);
pEnum = NULL;
sampleGrabberFilter->EnumPins(&pEnum);
pEnum->Reset();
hResult = pEnum->Next(1, &pGrabberInput, NULL);
release(pEnum);
pEnum = NULL;
sampleGrabberFilter->EnumPins(&pEnum);
pEnum->Reset();
pEnum->Skip(1);
hResult = pEnum->Next(1, &pGrabberOutput, NULL);


release(pEnum);
pEnum = NULL;
nullRenderer->EnumPins(&pEnum);
pEnum->Reset();
hResult = pEnum->Next(1, &pNullInputPin, NULL);
hResult = graphBuilder->Connect(pCameraOutput, pGrabberInput);

hResult = graphBuilder->Connect(pGrabberOutput, pNullInputPin);
release(filter);
queryInterface(graphBuilder, MediaControl, mediaControl);
queryInterface(graphBuilder, MediaEvent, mediaEvent);
hResult = sampleGrabber->SetBufferSamples(TRUE);
hResult = sampleGrabber->SetOneShot(TRUE);
hResult = mediaControl->Run();
long eventCode = 0;
hResult = mediaEvent->WaitForCompletion(INFINITE, &eventCode);
sizeBuffer = 0;
hResult = sampleGrabber->GetCurrentBuffer(&sizeBuffer, NULL);
pBuffer = new unsigned char[sizeBuffer];
lastFrame = null;
if (!pBuffer)
{
std::cerr << "Failed\n";
std::exit(-1);
}
release(pEnum);
release(pCameraOutput);
release(pGrabberInput);
release(pGrabberOutput);
release(pNullInputPin);
}

void grabFrame()
{
hResult = sampleGrabber->GetCurrentBuffer(&sizeBuffer, (long*)pBuffer);
}


Enigma

Share this post


Link to post
Share on other sites
Uh looks like your emails not working IanC. Could you email me the source at shawn__sch@hotmail.com? Really appreciate it.

I'm sure not having an easy time with COM objects with Direct Show.

[edited by - Crunchdown on June 13, 2004 7:40:38 PM]

Share this post


Link to post
Share on other sites
Hi Shawn,

Sorry mate, wasn't checking thread - got yr email, just zipping it up for you now :)

I.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!