MFC Visual Studio 2013でファイルからビデオを読み込み、オブジェクトを追跡できるプログラムを読みました。私は停止ボタンを設定しました。ビデオの読み込みと読み込みを停止します。私は追跡中の実行中のビデオを停止することはできません。それは私のGUIプログラムです:スレッドでmfcでビデオを停止する
bool Cgui2Dlg::getImageFilePath()
{
m_inputVideo.GetWindowTextW(file_path_);
if (file_path_.GetLength() > 0)
return true;
else
{
AfxMessageBox(L"Please, select one video file or image sequence folder!");
//return false;
}
}
Size Cgui2Dlg::getWindowSizeByID(int IDC) {
return getWindowSize(GetDlgItem(IDC)->m_hWnd);
}
void Cgui2Dlg::openfile()
{
getImageFilePath();
CT2CA filePath2(file_path_);
string fp(filePath2);
VideoCapture capture(fp);
Mat frame;
if (!capture.isOpened())
throw "Error when reading steam_avi";
run_ = true;
///// show on the window frame
while (run_) {
capture >> frame;
if (frame.empty())
{
run_ = false;
break;
}
auto input_size = getWindowSizeByID(IDC_Video_Original);
view1_.create(input_size, CV_8UC3);
rect1_ = ZoomWithSpect(frame, view1_, cofi_, Scalar::all(0));
matToWinControl(view1_, &m_original_frame);
///
SendMessage(WM_MSG, 0, 0);
waitKey(400); // waits to display frame
this_thread::sleep_for(std::chrono::milliseconds(interval_));
}
if (run_) {
run_ = false;
OnBnClickedStop();
}
}
void Cgui2Dlg::OnBnClickedButton1()
{
//CFileDialog dlg(TRUE, _T("*.avi"), NULL,
//OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY, _T("video files (*.bmp; *.jpg; *.avi) |*.bmp;*.jpg;|All Files (*.*)|*.*||"), NULL);
//dlg.m_ofn.lpstrTitle = _T("Open Video");
//CString file_path_ = dlg.GetPathName(); // the selected filename // This is image pointer
setButtonsState(EcvButtonState::BUTTON1);
UpdateData(TRUE);
interval_ = 25;
std::thread t(&Cgui2Dlg::openfile, this);
t.detach();
}
void Cgui2Dlg::OnBnClickedButton3()
{
setButtonsState(EcvButtonState::BUTTON3);
UpdateData(TRUE);
interval_ = 200;
std::thread tt(&Cgui2Dlg::trackingfile, this);
tt.detach();
}
void Cgui2Dlg::trackingfile()
{
getImageFilePath();
CT2CA filePath2(file_path_);
string fp(filePath2);
run_ = true;
//VideoCapture capture(fp);
//Mat frame;
//capture >> frame;
setButtonsState(EcvButtonState::BUTTON3);
//check radio botton for choosing one algorithm
int checkradio = GetCheckedRadioButton(IDC_RADIO1, IDC_RADIO2);
switch (checkradio)
{
case IDC_RADIO1:
{
auto input_size = getWindowSizeByID(IDC_Video_Track);
view2_.create(input_size, CV_8UC3);
//rect1_ = ZoomWithSpect(frame, view1_, cofi_, Scalar::all(0));
//matToWinControl(view2_, &m_Track_frame);
//FCAMshift CAM(fp);//,run_);
//CAM.track(fp); //, run_);
FRunTracker CAM(fp);//,run_);
CAM.track(fp); //, run_);
//CAM.showresult();
///
//waitKey(40); // waits to display frame
//this_thread::sleep_for(std::chrono::milliseconds(interval_));
}
if (run_) {
run_ = false;
OnBnClickedStop();
}
break;
case IDC_RADIO2:
{
auto input_size = getWindowSizeByID(IDC_Video_Track);
view2_.create(input_size, CV_8UC3);
FCAMshift CAM(fp);//,run_);
CAM.track(fp); //, run_);
///
//waitKey(40); // waits to display frame
this_thread::sleep_for(std::chrono::milliseconds(interval_));
}
if (run_) {
run_ = false;
OnBnClickedStop();
}
break;
}
}
void Cgui2Dlg::setButtonsState(const EcvButtonState& state) {
switch (state)
{
case EcvButtonState::BUTTON1:
m_stop_btn.EnableWindow(TRUE);
m_Track_btn.EnableWindow(TRUE);
m_load_btn.EnableWindow(TRUE);
break;
case EcvButtonState::BUTTON3:
m_stop_btn.EnableWindow(TRUE);
m_Track_btn.EnableWindow(TRUE);
m_load_btn.EnableWindow(TRUE);
break;
case EcvButtonState::stop:
m_stop_btn.EnableWindow(TRUE);
m_Track_btn.EnableWindow(TRUE);
m_load_btn.EnableWindow(TRUE);
break;
}
//UpdateData(FALSE);
}
void Cgui2Dlg::OnBnClickedRadio1()
{
// TODO: Add your control notification handler code here
}
void Cgui2Dlg::OnBnClickedButton2()
{
run_ = false;
setButtonsState(EcvButtonState::stop);
}
void Cgui2Dlg::OnBnClickedRadio2()
{
// TODO: Add your control notification handler code here
}
void Cgui2Dlg::OnStnClickedVideoTrack()
{
}
void Cgui2Dlg::OnStnClickedVideoOriginal()
{
// TODO: Add your control notification handler code here
}
LRESULT Cgui2Dlg::onUpdateUI(WPARAM wparam, LPARAM lparam) {
matToWinControl(view1_, &m_original_frame);
//matToWinControl(view2_, &m_result_frame);
return 0;
}
void Cgui2Dlg::OnBnClickedStop()
{
run_ = false;
interval_ = 2000;
UpdateData(TRUE);
setButtonsState(EcvButtonState::stop);
}
、これが私のトラッキングコードです:FRunTrackerクラスで
/************************************************************************
* File: RunTracker.cpp
************************************************************************/
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "CompressiveTracker.h"
#include "RunTracker.h"
using namespace cv;
using namespace std;
FRunTracker::FRunTracker(string x) //,bool y)
{
fp = x;
//run_= y;
}
Mat src2, img2, ROI2, image2;
Rect box(0, 0, 0, 0);
Point Pi1(0, 0);
Point Pi2(0, 0);
const char* winName2 = "compressive tracking demo";
bool click = false;
void showimage22() {
img2 = src2.clone();
//checkBoundary();
if (box.width>0 && box.height>0) {
ROI2 = src2(box);
//imshow("cropped", ROI2);
}
rectangle(img2, box, Scalar(0, 255, 0), 1, 8, 0);
imshow(winName2, img2);
}
void onMouse2(int event, int x, int y, int f, void*) {
switch (event) {
case CV_EVENT_LBUTTONDOWN:
click = true;
Pi1.x = x;
Pi1.y = y;
Pi2.x = x;
Pi2.y = y;
break;
case CV_EVENT_LBUTTONUP:
Pi2.x = x;
Pi2.y = y;
click = false;
//trackObject = -1;
break;
case CV_EVENT_MOUSEMOVE:
if (click) {
Pi2.x = x;
Pi2.y = y;
}
break;
default: break;
}
if (click) {
if (Pi1.x>Pi2.x) {
box.x = Pi2.x;
box.width = Pi1.x - Pi2.x;
}
else {
box.x = Pi1.x;
box.width = Pi2.x - Pi1.x;
}
if (Pi1.y>Pi2.y) {
box.y = Pi2.y;
box.height = Pi1.y - Pi2.y;
}
else {
box.y = Pi1.y;
box.height = Pi2.y - Pi1.y;
}
}
showimage22();
}
void FRunTracker::track(string fp)
{
VideoCapture input_video(fp);
input_video >> src2;
namedWindow(winName2, WINDOW_NORMAL);
setMouseCallback(winName2, onMouse2, NULL);
imshow(winName2, src2);
waitKey(0);
// CT framework
CompressiveTracker ct;
Mat grayimg2;
cvtColor(src2, grayimg2, CV_RGB2GRAY);
ct.init(grayimg2, box);
char strFrame[20];
//FILE* resultStream;
//resultStream = fopen("TrackingResults.txt", "w");
//fprintf(resultStream, "%i %i %i %i\n", (int)box.x, (int)box.y, (int)box.width, (int)box.height);
while (1) {
input_video >> src2;
if (src2.empty())
break;
cvtColor(src2, grayimg2, CV_RGB2GRAY);
ct.processFrame(grayimg2, box);// Process frame
rectangle(src2, box, Scalar(200, 0, 0), 2);// Draw rectangle
//fprintf(resultStream, "%i %i %i %i\n", (int)box.x, (int)box.y, (int)box.width, (int)box.height);
//sprintf(strFrame, "#%d ", i);
//putText(src2, strFrame, cvPoint(0, 20), 2, 1, CV_RGB(25, 200, 25));
imshow("Compressive Tracking", src2);// Display
waitKey(1);
}
//fclose(resultStream);
}
FRunTracker::~FRunTracker()
{
}
MFCを使いこなすには、C++を初めて使う方が0になる可能性が低くなります。起こることはありません。スレッド間でウィンドウ階層を構築することは、技術的には可能ですが、複雑なので、熟練した開発者であっても、それを防ぐために何かを試すことになります。とにかく、この質問は将来の訪問者に役立つことはまずありません。閉会に投票しました。 – IInspectable