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
#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); line(cameraFeed, Point(x, y), Point(x, y - 30), Scalar(0, 255, 0), 2); 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);
}
int main(){
bool objectDetected = false;
bool debugMode = false;
bool trackingEnabled = false;
bool pause = false;
Mat frame1, frame2;
Mat grayImage1, grayImage2;
Mat differenceImage;
Mat thresholdImage;
VideoCapture capture;
capture.open(0);
if (!capture.isOpened()){
cout << "ERROR ACQUIRING VIDEO FEED\n";
getchar();
return -1;
}
while (1){
capture.read(frame1);
cv::cvtColor(frame1, grayImage1, COLOR_BGR2GRAY);
capture.read(frame2);
cv::cvtColor(frame2, grayImage2, COLOR_BGR2GRAY);
cv::absdiff(grayImage1, grayImage2, differenceImage);
cv::threshold(differenceImage, thresholdImage, SENSITIVITY_VALUE, 255, THRESH_BINARY);
if (debugMode == true){
cv::imshow("Difference Image", differenceImage);
cv::imshow("Threshold Image", thresholdImage);
}
else{
cv::destroyWindow("Difference Image");
cv::destroyWindow("Threshold Image");
}
cv::blur(thresholdImage, thresholdImage, cv::Size(BLUR_SIZE, BLUR_SIZE));
cv::threshold(thresholdImage, thresholdImage, SENSITIVITY_VALUE, 255, THRESH_BINARY);
if (debugMode == true){
imshow("Final Threshold Image", thresholdImage);
}
else {
cv::destroyWindow("Final Threshold Image");
}
if (trackingEnabled){
searchForMovement(thresholdImage, frame1);
}
imshow("Frame1", frame1);
switch (waitKey(10)){
case 27: return 0;
case 116: trackingEnabled = !trackingEnabled;
if (trackingEnabled == false) cout << "Tracking disabled." << endl;
else cout << "Tracking enabled." << endl;
break;
case 100: debugMode = !debugMode;
if (debugMode == false) cout << "Debug mode disabled." << endl;
else cout << "Debug mode enabled." << endl;
break;
case 112: pause = !pause;
if (pause == true){
cout << "Code paused, press 'p' again to resume" << endl;
while (pause == true){
switch (waitKey()){
case 112:
pause = false;
cout << "Code resumed." << endl;
break;
}
}
}
}
}
return 0;
}