📢:如果你也对机器人、人工智能感兴趣,看来我们志同道合✨
📢:不妨浏览一下我的博客主页【https://blog.csdn.net/weixin_51244852】
📢:文章若有幸对你有帮助,可点赞 👍 收藏 ⭐不迷路🙉
📢:内容若有错误,敬请留言 📝指正!原创文,转载请注明出处
一、鼠标框选矩形区域
本操作的目的是为了:所谓“鼠标框选矩形区域”,通过拖动鼠标框选出我们想要的识别或是检测的区域,未选中区域不作为目标区域。
#include<opencv2\opencv.hpp>
#include <stdio.h>
using namespace cv;
using namespace std;
Mat org, dst, img, tmp;
void on_mouse(int event, int x, int y, int flags, void *)
{
static Point pre_pt = (0, 0);
static Point cur_pt = (0, 0);
if (event == EVENT_LBUTTONDOWN)
{
pre_pt = Point(x, y);
}
else if (event == EVENT_MOUSEMOVE && flags)//摁下左键,flags为1
{
org.copyTo(tmp);
cur_pt = Point(x, y);
circle(tmp, cur_pt, 4, Scalar(0, 255, 0, 0), 2, 8);
rectangle(tmp, pre_pt, cur_pt, Scalar(0, 255, 0, 0), 1, 8, 0);
imshow("img", tmp);//画的时候显示框
}
else if (event == EVENT_LBUTTONUP)
{
org.copyTo(img);
rectangle(img, pre_pt, cur_pt, Scalar(0, 255, 0, 0), 1, 8, 0);
imshow("img", img);//画完后显示框
int width = abs(pre_pt.x - cur_pt.x);
int height = abs(pre_pt.y - cur_pt.y);
dst = org(Rect(min(cur_pt.x, pre_pt.x), min(cur_pt.y, pre_pt.y), width, height));
cout << "x=" << pre_pt.x << "y=" << pre_pt.y << "width=" << width << "height=" << height << endl;
namedWindow("dst");
imshow("dst", dst);
}
}
int main() {
org = imread("D:/images/111.jpg");
org.copyTo(img);
namedWindow("img");
imshow("img", img);
setMouseCallback("img", on_mouse, 0);
/* waitKey();
cvtColor(dst, dst, CV_BGR2GRAY);
imshow("gray", dst);*/
waitKey(0);
}
二、截图的全部代码
2.1静态图像截图
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
Mat src, dst, g_img_dst,g_img_sub;
bool g_is_rect_inited = false;
Point g_rect_tl;
void onMouse(int event, int x, int y, int, void*)
{
if (EVENT_LBUTTONDOWN == event) {
g_is_rect_inited = true;
g_rect_tl = Point(x, y);
}
else if (EVENT_MOUSEMOVE == event && g_is_rect_inited) {
src.copyTo(g_img_dst);
rectangle(g_img_dst, g_rect_tl, Point(x, y), Scalar_<uchar>::all(200), 3, 8);
imshow("image", g_img_dst);
}
else if (EVENT_LBUTTONUP == event && g_rect_tl != Point(x, y)) {
src(Rect(g_rect_tl, Point(x, y))).copyTo(g_img_sub);
cvtColor(g_img_sub, dst, COLOR_BGR2GRAY);
imshow("截图", dst);
g_is_rect_inited = false;
}
}
int main(int argc, char** argv) {
src = imread("D:/images/lena.jpg");
if (src.empty()) {
cout << "[ERROR] : please check your image file name." << endl;
return EXIT_FAILURE;
}
namedWindow("image", WINDOW_AUTOSIZE);
setMouseCallback("image", onMouse, 0);
while (true) {
imshow("image", src);
int c = waitKey(0);
if ((c & 255) == 27) { // Esc
destroyAllWindows();
cout << "Exiting ...\n";
break;
}
}
return 0;
}
2.2对视频流截图
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
Mat src, dst, g_img_dst,g_img_sub;
bool g_is_rect_inited = false;
Point g_rect_tl;
void onMouse(int event, int x, int y, int, void*)
{
if (EVENT_LBUTTONDOWN == event) {
g_is_rect_inited = true;
g_rect_tl = Point(x, y);
}
else if (EVENT_MOUSEMOVE == event && g_is_rect_inited) {
src.copyTo(g_img_dst);
rectangle(g_img_dst, g_rect_tl, Point(x, y), Scalar_<uchar>::all(200), 3, 8);
imshow("image", g_img_dst);
}
else if (EVENT_LBUTTONUP == event && g_rect_tl != Point(x, y)) {
src(Rect(g_rect_tl, Point(x, y))).copyTo(g_img_sub);
cvtColor(g_img_sub, dst, COLOR_BGR2GRAY);
imshow("截图", dst);
g_is_rect_inited = false;
}
}
int main(int argc, char** argv) {
VideoCapture video(0);
while (1)//循环显示每一帧
{
video >> src;//读取当前帧
if (src.empty()) {
cout << "[ERROR] : please check your image file name." << endl;
break;
}
namedWindow("image", WINDOW_AUTOSIZE);
setMouseCallback("image", onMouse, 0);
while (true) {
imshow("image", src);
int c = waitKey(0);
if ((c & 255) == 27) { // Esc
destroyAllWindows();
cout << "Exiting ...\n";
break;
}
}
return 0;
}
}
三、打开两个USB相机和视频
3.1 打开两个USB相机
#include<opencv2/opencv.hpp>
#include<thread>
using namespace cv;
int main()
{
VideoCapture CapLeft(0);
VideoCapture CapRight(1);
Mat frameLeft;
Mat frameRight;
if (!CapRight.isOpened())return 0;
if (!CapLeft.isOpened())return 0;
while (waitKey(30) != 27)
{
CapRight >> frameRight;
imshow("右摄像头", frameRight);
CapLeft >> frameLeft;
imshow("左摄像头", frameLeft);
}
return 0;
}
3.2 打开两个视频
#include<opencv2/opencv.hpp>
#include<thread>
using namespace cv;
int main()
{
VideoCapture CapLeft("D:/images/IMG_4753.mp4");
VideoCapture CapRight("D:/images/IMG_4755.mp4");
Mat frameLeft;
Mat frameRight;
if (!CapRight.isOpened())return 0;
if (!CapLeft.isOpened())return 0;
while (waitKey(30) != 27)
{
CapRight >> frameRight;
imshow("右摄像头", frameRight);
CapLeft >> frameLeft;
imshow("左摄像头", frameLeft);
}
return 0;
}
reference
[OpenCV]在显示窗口中截图