以下是我的代买,请问如何C++ opencv摄像头窗口和cmd窗口合并为一个窗口。
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
#include "opencv2\highgui\highgui_c.h"
#include <iostream>
#include <string>
#include <functional>
#include <pthread.h>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0) //用来检测按键的点击事件
using namespace std;
using namespace cv;
using namespace xmate;
using CartesianControl = std::function<CartesianPose(RCI::robot::RobotState robot_state)>;
POINT p;
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
HWND h = GetForegroundWindow();
CONSOLE_FONT_INFO consoleCurrentFont;
int X,Y;
Mat result;
Mat Movedetect(Mat temp) {
Mat result = temp.clone();
//1.covert the cam to gray
Mat gray1, gray2;
cvtColor(temp, gray1, COLOR_BGR2GRAY);
//cvtColor(frame, gray2, COLOR_BGR2GRAY);
//2.subtract 2 frame
//Mat diff;
//absdiff(gray1, gray2, diff);
//imshow("sub", diff);
//3.color
Mat diff_thresh;
threshold(gray1, diff_thresh, 50, 255, THRESH_BINARY);
//imshow("color", diff_thresh);
////4.kernel transfer
//Mat kernel_ercode = getStructuringElement(MORPH_RECT, Size(3, 3));
//Mat kernel_dilate = getStructuringElement(MORPH_RECT, Size(18, 18));
////5. Corrosion transformation
//erode(diff_thresh, diff_thresh, kernel_ercode);
////6.Dilate transformation
//dilate(diff_thresh, diff_thresh, kernel_dilate);
//7.search and draw outline
vector<vector<Point>>contours;
findContours(diff_thresh, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
drawContours(result, contours, 0, Scalar(0, 0, 255), 2);
////8.outline
//vector<Rect>boundRect(contours.size());
//boundRect[1] = boundingRect(contours[0]);
//rectangle(result, boundRect[1], Scalar(0, 255, 0), 2);
float A, B, C, D, a, b, c, d;
A = contours[0][1].x;
a = contours[0][1].y;
B = contours[0][2].x;
b = contours[0][2].y;
X = (A + B) / 2;
Y = (a + b) / 2;
/*cout << X << ',' << Y << endl;*/
return result;
}
void Video() {
VideoCapture capture(1);
if (!capture.isOpened()) { //test cam
cout << "cam open error" << endl;
}
while (true) { // cycle frame
Mat frame;
capture >> frame;
int getx, gety;
if (KEY_DOWN(VK_RBUTTON)) {
POINT p;
GetCursorPos(&p);
HWND hWnd = (HWND)cvGetWindowHandle("cam");
ScreenToClient(hWnd, &p);
getx = p.x, gety = p.y;
}
result = Movedetect(frame);
circle(result, Point(getx, gety), 10, Scalar(255, 0, 0), 1, CV_AA);
line(result,Point(320,0),Point(320,480),Scalar(255, 100, 0), 1, CV_AA);
line(result, Point(0, 240), Point(640, 240), Scalar(255, 100, 0), 1, CV_AA);
imshow("cam", result);
waitKey(30); //延时30
}
}
int main(int argc, char *argv[]) {
thread th = thread(Video);
th.detach();
while (1) {
if (KEY_DOWN(VK_RBUTTON)) { //Mouse right button,move x,y
/*
q_begin.pos = robot.receiveRobotState().toolTobase_pos_m;
cout << q_begin.pos << endl;*/
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hStdin, &mode);
mode &= ~ENABLE_QUICK_EDIT_MODE;
SetConsoleMode(hStdin, mode);
POINT p;
GetCursorPos(&p);
HWND hWnd = (HWND)cvGetWindowHandle("cam");
ScreenToClient(hWnd, &p);
GetCurrentConsoleFont(hOutput, FALSE, &consoleCurrentFont); //获取字体信息
int getx = p.x, gety = p.y;
int x = p.x /= consoleCurrentFont.dwFontSize.X;
int y = p.y /= consoleCurrentFont.dwFontSize.Y;
/*printf("key down (%d,%d)\n", x, y);*/
cout << "kuang:"<<X << "," << Y << endl;
cout << "mouse:"<<getx << "," << gety << endl;
}
Sleep(100);
}
if (waitKey(20) == 27) {
cout << "Quit" << endl;
}
Sleep(100);
return 0;
}