opencv getRectSubPix()函数

本文介绍了一个OpenCV中的函数getRectSubPix,该函数可以从原图像中提取一个感兴趣的矩形区域图像,并支持亚像素精度。文中详细解释了函数的参数含义及使用方法,并提供了一个示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

函数作用

从原图像中提取一个感兴趣的矩形区域图像。

函数原型

C++: void getRectSubPix(InputArray image, Size patchSize, Point2f center, OutputArray dst, int patchType=-1 )

参数解释

InputArray image:输入图像

Size patchSize:获取感兴趣区域矩形的大小

Point2f center:感兴趣区域矩形在原图像中的位置(即感兴趣区域矩形的中心点坐标)

OutputArray patch:输出的图像

int patchType=-1 :表示输出图像的深度。默认-1 ,深度不变

官方介绍 

getRectSubPix

Retrieves a pixel rectangle from an image with sub-pixel accuracy.

C++: void getRectSubPix(InputArray image, Size patchSize, Point2f center, OutputArray dst, int patchType=-1 )

Parameters:
  • src – Source image.
  • patchSize – Size of the extracted patch.
  • center – Floating point coordinates of the center of the extracted rectangle within the source image. The center must be inside the image.
  • dst – Extracted patch that has the size patchSize and the same number of channels as src .
  • patchType – Depth of the extracted pixels. By default, they have the same depth as src .

The function getRectSubPix extracts pixels from src :

dst(x, y) = src(x +  \texttt{center.x} - ( \texttt{dst.cols} -1)*0.5, y +  \texttt{center.y} - ( \texttt{dst.rows} -1)*0.5)

where the values of the pixels at non-integer coordinates are retrieved using bilinear interpolation. Every channel of multi-channel images is processed independently. While the center of the rectangle must be inside the image, parts of the rectangle may be outside. In this case, the replication border mode (see borderInterpolate() ) is used to extrapolate the pixel values outside of the image.

示例

代码:

#include <iostream>
#include <opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	Mat srcImg = imread("HanJiaRen1.jpg");
	Mat dstImg;
	getRectSubPix(srcImg, Size(srcImg.cols / 2, srcImg.rows / 2), Point2f(srcImg.cols / 2, srcImg.rows / 2), dstImg, -1);

	imshow("srcImg", srcImg);
	imshow("dstImg", dstImg);
	waitKey(0);
	return 0;
}

结果显示:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值