OpenCV - C++ - region of interest (ROI)

OpenCV - C++ - region of interest (ROI)

In this tutorial, we will learn how to select a bounding box or a rectangular region of interest (ROI) in an image in OpenCV.
在本教程中,我们将学习如何在 OpenCV 中选择图像中的矩形感兴趣区域 (ROI)。

Let’s start with a sample code. It allows you to select a rectangle in an image, crop the rectangular region and finally display the cropped image.
让我们从一个示例代码开始。它允许您选择图像中的矩形,裁剪矩形区域,最后显示裁剪的图像。

1. How to select a region of interest in OpenCV?

如何在 OpenCV 中选择感兴趣的区域?

//============================================================================
// Name        : using namespace cv;
// Author      : Yongqiang Cheng
// Version     : Feb 22, 2020
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>

#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;

int main(int, char**)
{
	Mat frame;
	Mat roi_image;

	//--- INITIALIZE VIDEOCAPTURE
	VideoCapture cap;
	// open the default camera using default API
	// cap.open(0);
	// OR advance usage: select any API backend
	int deviceID = 0;             // 0 = open default camera
	int apiID = cv::CAP_ANY;      // 0 = autodetect default API
	// open selected camera using selected API
	cap.open(deviceID + apiID);
	// check if we succeeded
	if (!cap.isOpened())
	{
		cerr << "ERROR! Unable to open camera\n";
		return -1;
	}

	namedWindow("source image", WINDOW_AUTOSIZE);
	namedWindow("roi image", WINDOW_AUTOSIZE);

	float scale = 0.5;

	int frame_width = cap.get(CAP_PROP_FRAME_WIDTH);
	int frame_height = cap.get(CAP_PROP_FRAME_HEIGHT);

	int roi_width = frame_width * scale;
	int roi_height = frame_height * scale;

	int x = 128; // x coordinate of the top-left corner
	int y = 64; // y coordinate of the top-left corner

	cv::Rect roi_rect;
	roi_rect.x = x;
	roi_rect.y = y;
	roi_rect.width = roi_width;
	roi_rect.height = roi_height;

	//--- GRAB AND WRITE LOOP
	cout << "Start grabbing" << endl << "Press any key to terminate" << endl;
	for (;;)
	{
		// wait for a new frame from camera and store it into 'frame'
		cap.read(frame);
		// check if we succeeded
		if (frame.empty())
		{
			cerr << "ERROR! blank frame grabbed\n";
			break;
		}

		/* Crop the original image to the defined ROI */
		roi_image = frame(roi_rect);

		// show live and wait for a key with timeout long enough to show images
		imshow("source image", frame);
		imshow("roi image", roi_image);

		if (waitKey(5) >= 0)
		{
			break;
		}
	}

	// the camera will be deinitialized automatically in VideoCapture destructor
	return 0;
}

09:20:40 **** Build of configuration Debug for project DisplayImage ****
make all 
Building file: ../src/DisplayImage.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -I/usr/local/include -I/usr/local/include/opencv4 -I/usr/local/include/opencv4/opencv2 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/DisplayImage.d" -MT"src/DisplayImage.o" -o "src/DisplayImage.o" "../src/DisplayImage.cpp"
Finished building: ../src/DisplayImage.cpp
 
Building target: DisplayImage
Invoking: GCC C++ Linker
g++ -L/usr/local/lib -o "DisplayImage"  ./src/DisplayImage.o   -lopencv_core -lopencv_video -lopencv_ml -lopencv_imgproc -lopencv_img_hash -lopencv_flann -lopencv_features2d -lopencv_calib3d -lopencv_dnn -lopencv_dnn_objdetect -lopencv_cvv -lopencv_text -lopencv_datasets -lopencv_aruco -lopencv_bgsegm -lopencv_shape -lopencv_imgcodecs -lopencv_videoio -lopencv_highgui -lopencv_bioinspired
Finished building target: DisplayImage
 

09:20:43 Build Finished (took 3s.175ms)

在这里插入图片描述

发布了473 篇原创文章 · 获赞 1762 · 访问量 104万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104510556