Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have implemented the code that displays four windows of opencv to get live video feed from 4 webcams. But the code is not dynamic. I want to make it dynamic where it will display only windows equal to the number of cameras attached to System. I am getting the number of camera attached to the machine. The only task remains is to implement the code for dynamic scenario.
Here's the code.

C++
#include<opencv\cv.h>
#include<opencv\highgui.h>
#include<d:\my>

using namespace cv;

int main(){

	//Create matrix to store image
	Mat image;
	Mat image1;
	Mat image2;
	Mat image3;

	//Mat image1;
	//initailize capture
	videoInput VI;
	int numDevices = VI.listDevices();	
 	VideoCapture cap;
	VideoCapture cap1;
	VideoCapture cap2;
	VideoCapture cap3;

	bool x = false;
	//VideoCapture cap1;
	cap.open(0);
	cap1.open(1);
	cap2.open(2);
	cap3.open(3);
	
	namedWindow("Camera 1",1);
	namedWindow("Camera 2",1);
	namedWindow("Camera 3",1);
	namedWindow("Camera 4",1);

	while(1){
		//copy webcam stream to image
		cap >> image;
		cap1 >> image1;
		cap2 >> image2;
		cap3 >> image3;
		cap4 >> image4;
		//cap1 >> image1;
		imshow("Camera 1",image);
		imshow("Camera 2",image1);
		imshow("Camera 3",image2);
		imshow("Camera 4",image3);

		//imshow("Camera 2",image1);
		//delay 33ms
		waitKey(33);
	}
}

Please point me to right direction for the making the dynamic code. Later i want to implement the code in mfc.
Posted
Updated 26-Oct-13 3:39am
v2
Comments
Sergey Alexandrovich Kryukov 26-Oct-13 2:24am    
Not clear where is your problem. You successfully got the number of cameras. So, what's missing?
—SA

1 solution

There's likely to be a list of cameras provided, perhaps by the camera API - that's the first place to look, camera manufacturers usually provided example code. And perhaps linking to a list of structures, where each structure contains information about one camera. I'd look for these.
When using cameras (on a firewire bus) I needed be able to detect newly a added camera and reasoned that a person is there to add (or remove that camera) and so can press a button 'refresh camera list' rather demand an event fired by some low-level detection. Though it may be your API provides such an event so again, have a look for that.
You might later find it useful to write a C++ class to handle such additions and removals. I've added the header from such a class I wrote, and its description, to show how this might look.

C#
#include "CameraList.h" // The list of [camera] nodes on the bus that are managed.

/* The number of cameras on the firewire bus can change, that is a camera could be manually connected or disconnected or a connected camera could fail therefore we need to have our list of cameras updated, both on an event driven basis and at our request. A Bus Manager has been introduced to take care of this. Event driven changes are not really a problem, but at times the firewire bus has been initialised and generated events before the list of information on the ameras is actually required. So being able to ask the Bus Manager to regenerate the list upon demand, simply by calling the event handler OnNodeListChanged is extremely useful.
CBusManager also wraps the code for adding and removing nodes from the list of Cameras, making use of the bus simpler and cleaner. */

class CBusManager
{
public:
    CBusManager(CCameraList* pCameraList);
    void OnNodelistChanged();
    void EnableNode(UINT32 Ix,BOOL Enable);
    void RemoveNode(UINT32 Ix);
    void AddNode(FGNODEINFO *pInfo);

    CCameraList* m_pCameraList;
};
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900