13.OpenCV—鼠标事件剪切图像

发布于:2025-06-19 ⋅ 阅读:(11) ⋅ 点赞:(0)

.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
  

private slots:
     void on_pushButton_clicked();

     void on_pushButton_2_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <qdebug.h>
#include "opencv2/opencv.hpp"

using namespace cv;



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
      testOpenCV(); // 新增调用
}

MainWindow::~MainWindow()
{
    delete ui;
}




#include <qdebug.h>
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

Rect selection;
Point origin;
bool dragging = false;
bool selected = false;
Mat image; // 在这里定义全局图像变量

// 鼠标回调函数
void onMouse(int event, int x, int y, int, void* userdata) {
    Mat* img = reinterpret_cast<Mat*>(userdata); // 通过 userdata 获取图像

    if (event == EVENT_LBUTTONDOWN) {
        origin = Point(x, y);
        selection = Rect(x, y, 0, 0);
        dragging = true;
    } else if (event == EVENT_MOUSEMOVE) {
        if (dragging) {
            selection.x = MIN(x, origin.x);
            selection.y = MIN(y, origin.y);
            selection.width = abs(x - origin.x);
            selection.height = abs(y - origin.y);
        }
    } else if (event == EVENT_LBUTTONUP) {
        dragging = false;
        selection.x = MIN(x, origin.x);
        selection.y = MIN(y, origin.y);
        selection.width = abs(x - origin.x);
        selection.height = abs(y - origin.y);
        selected = true; // 选区完成
    } else if (event == EVENT_RBUTTONDOWN) { // 右键点击
        if (selected && selection.width > 0 && selection.height > 0) {
            Mat croppedImage = (*img)(selection); // 使用指针访问图像并裁剪
            imwrite("cropped.jpg", croppedImage); // 保存剪切的图像
            cout << "Saved cropped image." << endl;
        }
        // 退出程序


         waitKey(0);
         exit(0);
    //   destroyAllWindows();
    }
}


//显示图像
void MainWindow::on_pushButton_2_clicked()
{// 读取图像
    image = imread("1.jpg"); // 请替换为你的图像路径
    if (image.empty()) {
        cout << "Could not open or find the image!" << endl;
        return ;
    }

    // 创建窗口并设置鼠标回调
    namedWindow("Image", WINDOW_AUTOSIZE);
       imshow("Image", image);
    setMouseCallback("Image", onMouse, &image); // 传递图像作为 userdata

}



//剪切图像
void MainWindow::on_pushButton_clicked()
{

       while (true) {
           Mat imgCopy = image.clone();

           // 绘制矩形选区(若拖拽中)
           if (dragging) {
               rectangle(imgCopy, selection, Scalar(255, 0, 0), 2);
           }

           // 在图像上绘制已确定的矩形
           if (selected) {
               rectangle(imgCopy, selection, Scalar(0, 255, 0), 2);
           }

           imshow("Image", imgCopy);

           // 按下 'q' 键退出
           char c = (char)waitKey(10);
           if (c == 'q') {
               break;
           }
       }

       destroyAllWindows();
}


网站公告

今日签到

点亮在社区的每一天
去签到