Qt+opencv4区域鼠标截图+图像比对(视频采集)

opencv感兴趣区域选取 ROI

需定义ROI区域的起点坐标和长度、宽度

int roi_x;

int roi_y;

int roi_h;

int roi_w;

void LVSD_TEST::ROIImage()
{
    image_grab = imread("image_grab\\grab.bmp");
    namedWindow("GRAB", WINDOW_AUTOSIZE);
    setMouseCallback("GRAB", on_drawing, (void*)(&image_grab));

    imshow("GRAB", image_grab);
    temp = image_grab.clone();
}
void on_drawing(int event, int x, int y, int flags, void* userdata)
{
    static Point sp(-1, -1);
    static Point ep(-1, -1);

    Mat image = *((Mat*)userdata); 
    if (event == EVENT_LBUTTONDOWN) {
        sp.x = x;
        sp.y = y;
    }
    else if (event == EVENT_LBUTTONUP) {
        ep.x = x;
        ep.y = y;
        int dx = ep.x - sp.x;
        int dy = ep.y - sp.y;

        if (dx >= 0 && dy > 0) {
            Rect box(sp.x, sp.y, dx, dy);
            temp.copyTo(image);
            Mat img_box = image(box);
//            imshow("ROI", img_box);
            imwrite("image_grab\\roi_1.bmp", img_box);
            rectangle(image, box, Scalar(0, 255, 0), 2, 8);
            imshow("GRAB", image);
 //           imwrite("image_grab\\rectImage.bmp", image);
            sp.x = -1;
            sp.y = -1;
        }
    }
    else if (event == EVENT_MOUSEMOVE) {
        if (sp.x > 0 && sp.y > 0) {
            ep.x = x;
            ep.y = y;
            int dx = ep.x - sp.x;
            int dy = ep.y - sp.y;
            if (dx >= 0 && dy > 0) {
                Rect box(sp.x, sp.y, dx, dy);
                temp.copyTo(image);
                rectangle(image, box, Scalar(0, 255, 0), 2, 8);
                imshow("GRAB", image);
            }
        }
    }
    else if (event == EVENT_MOUSEWHEEL)
    {
        value = getMouseWheelDelta(flags);
        int currentCols = image.cols;
        int currentRows = image.rows;

        currentCols = currentCols + count;
        currentRows = currentRows + count;
        resizeWindow("GRAB", currentCols, currentRows);

        qDebug() << value;

        if (value > 0 && count < image.cols)
        {
            count += 33;
            qDebug() << count;
        }      
        else if (value < 0 && count > -924)
        {
            count -= 33;
            qDebug() << count;
        }
    }
}

imshow中截取ROI后在Qt.label中圈出截取框

destroyWindow("GRAB");
QPixmap pixmap("image_grab\\grab.bmp");
QPainter painter(&pixmap);
painter.setPen(QPen(Qt::green, 3, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin));
painter.drawRect(roi_x, roi_y, roi_h, roi_w);
ui.label_Model->setPixmap(pixmap);

对所选区域与新采集图像同样位置进行比对

返回结果

void ContrastImage()
{
    Mat resultImage, grabImage, roiImage;

    grabImage = imread("image_grab\\grab.bmp");
    grabImage = grabImage(Rect(roi_x, roi_y, roi_h, roi_w));
    roiImage = imread("image_grab\\roi_1.bmp");
    //图像减法
    absdiff(grabImage, roiImage, resultImage);
    //信道分离
    Mat _channel[4];
    split(resultImage, _channel);
    //各信道二值化
    for (int i = 0; i < resultImage.channels(); i++)
    {
        threshold(_channel[i], _channel[i], ui.spinBox_Threshold->text().toInt(), 20, THRESH_BINARY);
    }
    //信道叠加
    add(_channel[0], _channel[1], _channel[3]);
    add(_channel[3], _channel[2], _channel[3]);
    threshold(_channel[3], _channel[3], 10, 255.0, THRESH_BINARY);

    //查找轮廓
    double _TempMaxArea = 0.0;
    double _TempArea = 0.0;
    double _TempAreaPV = 0.0;
    int _TempCount = 0;
    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;
    findContours(_channel[3], contours, hierarchy, RETR_LIST, CHAIN_APPROX_NONE);

    //绘制轮廓
    Mat imageContours = Mat::zeros(_channel[3].size(), CV_8UC1);
    Mat Contours = Mat::zeros(_channel[3].size(), CV_8UC1);
    for (int i = 0; i < contours.size(); i++)
    {
        //contours[i]代表的是第i个轮廓,contours[i].size()代表的是第i个轮廓上所有的像素点数
        for (int j = 0; j < contours[i].size(); j++)
        {
            //绘制出contours向量内所有的像素点
            Point P = Point(contours[i][j].x, contours[i][j].y);
            Contours.at<uchar>(P) = 255;

            if (_TempAreaPV == 0)
                _TempAreaPV = 3.0;
            if (_TempAreaPV > _TempMaxArea)
                _TempMaxArea = _TempAreaPV;
            _TempArea += _TempAreaPV;
            _TempCount++;
            if (_TempMaxArea > 4)
            {
                _Result = 1; break;//图像比对斑点大小超限
            }
            if (_TempArea > 5)
            {
                _Result = 2; break;//图像比对总斑点面积超限
            }
            if (_TempCount > 2)
            {
                _Result = 3; break;//图像比对斑点数量超限
            }
        }
        drawContours(imageContours, contours, i, Scalar(255), 1, 8, hierarchy);
    }
    if (_Result == 0)
    {
        QMessageBox::information(this, "Result", "    success    ");
    }
    else if (_Result == 1 || _Result == 2 || _Result == 3)
    {
        QMessageBox::information(this, "Result", "     failed     ");
    }
    //imshow("Contours Image", imageContours); //轮廓
    //imshow("Point of Contours", Contours);   //向量contours内保存的所有轮廓点
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值