Click here to Skip to main content
15,896,606 members
Articles / Multimedia / GDI+

Blobby! - Shape/Blob Recognition Code

Rate me:
Please Sign up or sign in to vote.
4.38/5 (16 votes)
15 May 2005LGPL33 min read 158.8K   5.7K   51  
A project on colored shape/blob recognition.
#include "Capture.h"

bool CaptureImages()
{
	char gDeviceName[256];	// Global test device name
	int gFrameNum = 0;		// Global frame count; Offset
	int gDevNum = 0;		// Global device number
	int mode = 5;			// 640x480 RGB
	int numDevices = 0;
	CVVidCapture::VIDCAP_MODE modeInfo;
	CVImage* grabImage = 0;

	// Acquire a video capture object from CVPlatform.
	// You can also just instantiate a CVVidCaptureDSWin32 object
	// directly, but this way the code is platform-neutral.
	CVVidCapture* vidCap = CVPlatform::GetPlatform()->AcquireVideoCapture();

	// First initialize the video capture interface
	// by calling Init().
	if (CVSUCCESS(vidCap->Init()))
	{
		printf("Initialized video capture object.\n");
	}
	else
	{
		printf("Error initializing video capture object.\n");
	    
		// Release the video capture object.
		// You could also just delete it right now, but using this
		// method allows us to switch to a singleton if we need
		// for subclasses of CVVidCapture, lets us reference count
		// if desired, etc.
		CVPlatform::GetPlatform()->Release(vidCap);                  
		return false;
	}

	// Enumerate devices.
	// The enumCallback is called for each available
	// video capture device.  Our current enumeration
	// callback just saves the first one. In practice,
	// you'd usually want to add these to a list and
	// let the user select one if the last selected
	// one was not available.
	//
	// Note that the video capture interface must
	// first be initialized before enumerating devices.
	memset(gDeviceName,0,256);

	if (CVFAILED(vidCap->GetNumDevices(numDevices)))
	{
		printf("Failed enumerating devices.\n");
		vidCap->Uninit();
		CVPlatform::GetPlatform()->Release(vidCap);      
		return false;
	}

	CVVidCapture::VIDCAP_DEVICE devInfo;

	int curDevIndex = 0;
	for (curDevIndex = 0; curDevIndex < numDevices; curDevIndex++)
	{
		if (CVSUCCESS(vidCap->GetDeviceInfo(curDevIndex,devInfo)))
		{
			printf("Device %d: %s\n",curDevIndex,devInfo.DeviceString);
		}
	}

	for (int k=0; k<numDevices; k++)
	{
		// Now connect to the selected device.
		if (CVSUCCESS(vidCap->Connect(k)))
		{
			int devNameLen = 0;
			vidCap->GetDeviceName(0,devNameLen);
			devNameLen++;
			char *devName = new char[devNameLen];
			vidCap->GetDeviceName(devName,devNameLen);
		    
			printf("Connection succeeded to %s.\n",devName);
			delete [] devName;
		}
		else
		{
			printf("Connection failed.\n");
			vidCap->Uninit();
			CVPlatform::GetPlatform()->Release(vidCap);      
			return false;
		}

		if (CVFAILED(vidCap->SetMode( mode )))    // Set to first available mode
		//if (CVFAILED(vidCap->SetMode(2)))      // Set to the third mode
		{
			printf("Error setting video mode.\n");
		}

		// Get the mode info of our selected mode and print it.
		if (CVFAILED(vidCap->GetCurrentMode(modeInfo)))
		{
			printf("Error activating mode!\n");
		}
		else
		{
			printf("Activated mode: %dx%d\n",modeInfo.XRes, modeInfo.YRes);   
		}

		// Set contrast to 50% if supported. Only some video capture devices
		// will support the properties.
		long minval,maxval;
		if (CVSUCCESS(vidCap->GetPropertyInfo( CVVidCapture::CAMERAPROP_CONTRAST,
												0,
												0,
												&minval, 
												&maxval)))
		{
			printf("Contrast supported: min-%d, max-%d\n", minval, maxval);    
			vidCap->SetProperty( CVVidCapture::CAMERAPROP_CONTRAST,
								(minval+maxval)/2);
		}
		else
		{
			printf("Contrast unsupported.\n");
		}

		// Now set the brightness if available.
		if (CVSUCCESS(vidCap->GetPropertyInfo( CVVidCapture::CAMERAPROP_BRIGHT,
												0,
												0,
												&minval, 
												&maxval)))
		{
			printf("Brightness supported\n", minval, maxval);     
			vidCap->SetProperty( CVVidCapture::CAMERAPROP_BRIGHT, (minval+maxval)/2);
		}
		else
		{
			printf("Brightness unsupported.\n");
		}

		//// XXX Gamma
		//// Now set the brightness if available.
		//if (CVSUCCESS(vidCap->GetPropertyInfo( CVVidCapture::CAMERAPROP_GAIN,
		//										0,
		//										0,
		//										&minval, 
		//										&maxval)))
		//{
		//	printf("Gain supported\n", minval, maxval);     
		//	vidCap->SetProperty( CVVidCapture::CAMERAPROP_GAIN, 0);
		//}
		//else
		//{
		//	printf("Gain unsupported.\n");
		//}

		printf("Grabbing color image...\n");
		//Sleep(3000);	// Pause for a little while
		for ( int i = 0; i < 10; i++)
		{
			if (CVSUCCESS(vidCap->Grab(CVImage::CVIMAGE_RGB24, grabImage)))
			{
				char saved_file [50];
				sprintf (saved_file, "device_map_%d", i);

				if (CVFAILED(CVImage::Save(saved_file,grabImage)))
				{
					printf("Failed saving color image.");
				}

				// We're responsible for freeing the image,
				// so do so now.
				/*CVImage::ReleaseImage(grabImage);*/
			}
			else
			{
				printf("Color Grab failed.\n");
			}
			CVImage::ReleaseImage(grabImage);
		}
	}

	// Always disconnect from the device when you're done
	printf("Disconnecting...\n");
	vidCap->Disconnect();   

	// And uninitialize before deleting it
	printf("Uninitializing video capture...\n");
	vidCap->Uninit();

	// Release video capture object.
	CVPlatform::GetPlatform()->Release(vidCap); 

	return true;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Singapore Singapore
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions