《实用OpenCV》<四> 图像和GUI窗口的基本操作(3)

ROI:从图像裁剪矩形区域

      图像的兴趣区

     兴趣区其实就如字面意思一样,是我们对图像特别感兴趣和愿意处理的一块区域。比如图片很大,大部分区域都不是我们想要用的,或者处理过程如果基于整张图的话计算量是很昂贵的。OpenCV里用一个rect结构来指定兴趣区,我们需要用左上角的位置,宽和高来定义一个rect。

例4-3 裁剪部分图像

// Program to crop images using GUI mouse callbacks
// Author: Samarth Manoj Brahmbhatt, University of Pennsylvania
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
// Global variables
// Flags updated according to left mouse button activity
bool ldown = false, lup = false;
// Original image
Mat img;
// Starting and ending points of the user's selection
Point corner1, corner2;
// ROI
Rect box;
// Callback function for mouse events
static void mouse_callback(int event, int x, int y, int, void *)
{
        // When the left mouse button is pressed, record its position and save it in corner1
        if(event == EVENT_LBUTTONDOWN)
       {
            ldown = true;
            corner1.x = x;
            corner1.y = y;
            cout << "Corner 1 recorded at " << corner1 << endl;
        }
      // When the left mouse button is released, record its position and save it in corner2
      if(event == EVENT_LBUTTONUP)
     {
     // Also check if user selection is bigger than 20 pixels (jut for fun!)
         if(abs(x - corner1.x) > 20 && abs(y - corner1.y) > 20)
        {
            lup = true;
            corner2.x = x;
            corner2.y = y;
            cout << "Corner 2 recorded at " << corner2 << endl << endl;
        }
        else
       {
            cout << "Please select a bigger region" << endl;
            ldown = false;
       }
     }
      // Update the box showing the selected region as the user drags the mouse
     if(ldown == true && lup == false)
    {
        Point pt;
        pt.x = x;
        pt.y = y;
        Mat local_img = img.clone();
        rectangle(local_img, corner1, pt, Scalar(0, 0, 255));
        imshow("Cropping app", local_img);
     }
     // Define ROI and crop it out when both corners have been selected
    if(ldown == true && lup == true)
   {
        box.width = abs(corner1.x - corner2.x);
        box.height = abs(corner1.y - corner2.y);
        box.x = min(corner1.x, corner2.x);
        box.y = min(corner1.y, corner2.y);
       // Make an image out of just the selected ROI and display it in a new window
    Mat crop(img, box);
    namedWindow("Crop");
    imshow("Crop", crop);
    ldown = false;
    lup = false;
   }
}
int main()
{
     img = imread("image.jpg");
     namedWindow("Cropping app");
     imshow("Cropping app", img);
    // Set the mouse event callback function
    setMouseCallback("Cropping app", mouse_callback);
    // Exit by pressing 'q'
    while(char(waitKey(1)) != 'q') {}
    return 0;
}

表 4-2 裁剪程序
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值