Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello i create a program with OpenCv that dispaly video. the program is a consolea application hat lunch a window that show the video.
Now i want to display the video on a picturbox.
Could you help me please.
i work with c++/CLI

the code is here
C++
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <Windows.h>


using namespace std;
using namespace cv;


const static int SENSITIVITY_VALUE = 40;                                        

const static int BLUR_SIZE = 20;                                               

int theObject[2] = { 0, 0 };

Rect objectBoundingRectangle = Rect(0, 0, 0, 0);


string intToString(int number){


	std::stringstream ss;
	ss << number;
	return ss.str();
}

void searchForMovement(Mat thresholdImage, Mat &cameraFeed){

	bool objectDetected = false;
	Mat temp;
	thresholdImage.copyTo(temp);
	
	vector< vector<Point> > contours;
	vector<Vec4i> hierarchy;

	findContours(temp, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

	if (contours.size()>0)objectDetected = true;
	else objectDetected = false;

	if (objectDetected){

		vector< vector<Point> > largestContourVec;
		largestContourVec.push_back(contours.at(contours.size() - 1));
	
		objectBoundingRectangle = boundingRect(largestContourVec.at(0));
		int xpos = objectBoundingRectangle.x + objectBoundingRectangle.width / 2;
		int ypos = objectBoundingRectangle.y + objectBoundingRectangle.height / 2;

		theObject[0] = xpos, theObject[1] = ypos;
	}

	int x = theObject[0];
	int y = theObject[1];

	circle(cameraFeed, Point(x, y), 30, Scalar(0, 255, 0), 2);  //******************30 IS THE SIZE OF THE CIRCLE
	line(cameraFeed, Point(x, y), Point(x, y - 30), Scalar(0, 255, 0), 2);  //**********30 IS THE SIZE OF THE CROSS HAIR
	line(cameraFeed, Point(x, y), Point(x, y + 30), Scalar(0, 255, 0), 2);
	line(cameraFeed, Point(x, y), Point(x - 30, y), Scalar(0, 255, 0), 2);
	line(cameraFeed, Point(x, y), Point(x + 30, y), Scalar(0, 255, 0), 2);
	putText(cameraFeed, "(" + intToString(x) + "," + intToString(y) + ")", Point(x, y), 1, 1, Scalar(255, 0, 0), 2);//*********MODIFIED



}
int main(){

	//some boolean variables for added functionality
	bool objectDetected = false;
	//these two can be toggled by pressing 'd' or 't'
	bool debugMode = false;
	bool trackingEnabled = false;
	//pause and resume code
	bool pause = false;
	//set up the matrices that we will need
	//the two frames we will be comparing
	Mat frame1, frame2;
	//their grayscale images (needed for absdiff() function)
	Mat grayImage1, grayImage2;
	//resulting difference image
	Mat differenceImage;
	//thresholded difference image (for use in findContours() function)
	Mat thresholdImage;
	//video capture object.
	VideoCapture capture;

	capture.open(0);    //0 = built-in, 1 = external

	if (!capture.isOpened()){
		cout << "ERROR ACQUIRING VIDEO FEED\n";
		getchar();
		return -1;
	}

	while (1){

		//we can loop the video by re-opening the capture every time the video reaches its last frame



		//check if the video has reach its last frame.
		//we add '-1' because we are reading two frames from the video at a time.
		//if this is not included, we get a memory error!
		//read first frame
		capture.read(frame1);
		//convert frame1 to gray scale for frame differencing
		cv::cvtColor(frame1, grayImage1, COLOR_BGR2GRAY);
		//copy second frame
		capture.read(frame2);
		//convert frame2 to gray scale for frame differencing
		cv::cvtColor(frame2, grayImage2, COLOR_BGR2GRAY);
		//perform frame differencing with the sequential images. This will output an "intensity image"
		//do not confuse this with a threshold image, we will need to perform thresholding afterwards.
		cv::absdiff(grayImage1, grayImage2, differenceImage);
		//threshold intensity image at a given sensitivity value
		cv::threshold(differenceImage, thresholdImage, SENSITIVITY_VALUE, 255, THRESH_BINARY);
		if (debugMode == true){
			//show the difference image and threshold image
			cv::imshow("Difference Image", differenceImage);
			cv::imshow("Threshold Image", thresholdImage);
		}
		else{
			//if not in debug mode, destroy the windows so we don't see them anymore
			cv::destroyWindow("Difference Image");
			cv::destroyWindow("Threshold Image");
		}
		//use blur() to smooth the image, remove possible noise and
		//increase the size of the object we are trying to track. (Much like dilate and erode)
		cv::blur(thresholdImage, thresholdImage, cv::Size(BLUR_SIZE, BLUR_SIZE));
		//threshold again to obtain binary image from blur output
		cv::threshold(thresholdImage, thresholdImage, SENSITIVITY_VALUE, 255, THRESH_BINARY);
		if (debugMode == true){
			//show the threshold image after it's been "blurred"
			imshow("Final Threshold Image", thresholdImage);
		}
		else {
			//if not in debug mode, destroy the windows so we don't see them anymore
			cv::destroyWindow("Final Threshold Image");
		}

		//if tracking enabled, search for contours in our thresholded image
		if (trackingEnabled){
			searchForMovement(thresholdImage, frame1);
		}

		//show our captured frame

		imshow("Frame1", frame1);

		//check to see if a button has been pressed.
		//this 10ms delay is necessary for proper operation of this program
		//if removed, frames will not have enough time to referesh and a blank 
		//image will appear.

		switch (waitKey(10)){                                   //*************MODIFIED

		case 27: //'esc' key has been pressed, exit program.
			return 0;
		case 116: //'t' has been pressed. this will toggle tracking
			trackingEnabled = !trackingEnabled;
			if (trackingEnabled == false) cout << "Tracking disabled." << endl;
			else cout << "Tracking enabled." << endl;
			break;
		case 100: //'d' has been pressed. this will debug mode
			debugMode = !debugMode;
			if (debugMode == false) cout << "Debug mode disabled." << endl;
			else cout << "Debug mode enabled." << endl;
			break;
		case 112: //'p' has been pressed. this will pause/resume the code.
			pause = !pause;
			if (pause == true){
				cout << "Code paused, press 'p' again to resume" << endl;
				while (pause == true){
					//stay in this loop until 
					switch (waitKey()){
						//a switch statement inside a switch statement? Mind blown.
					case 112:
						//change pause back to false
						pause = false;
						cout << "Code resumed." << endl;
						break;
					}
				}
			}


		}



	}

	return 0;

}
Posted
Comments
Richard MacCutchan 31-Oct-15 5:06am    
Help with what?
Jammes_Ca 31-Oct-15 5:37am    
I want to display the video on a picturebox
sreeyush sudhakaran 1-Nov-15 1:28am    
Hi there is Visual studio Integration Tool for OpenCv
https://visualstudiogallery.msdn.microsoft.com/00ebd33e-c7c6-4a02-9466-b619b5eddfb5
http://docs.opencv.org/2.4/doc/tutorials/introduction/windows_visual_studio_Opencv/windows_visual_studio_Opencv.html
Jammes_Ca 1-Nov-15 7:07am    
Thanks but what you sent me, i did it, my question is how to display the video into a pictur box in a GUI.
not a console application.

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