opencv 截取图像

// 放大原始框
#define CLAMP(a, s, m) ((a) < (s)? (s) : ((a) > (m) ? (m) : (a)))
// 输入矩形的两个点坐标,将坐标延申后输出
void BigRect(int width, int height, int x0, int x1, int y0, int y1, 
					int &x_left, int &x_right, int &y_top, int &y_bottom)
{

	int w = x1 - x0 + 1;
	int h = y1 - y0 + 1;

	float scaleW = 0.5f;
	float scaleH = 0.5f;

	int tempW = scaleW * w; // 一边增加这么多
	int tempH = scaleH * h;

	x_left = CLAMP(x0 - tempW, 0, width - 1);
	x_right = CLAMP(x1 + tempW, 0, width - 1);

	y_top = CLAMP(y0 - tempH, 0, height - 1);
	y_bottom = CLAMP(y1 + tempH, 0, height - 1);

}
opencv 截取图像
方法1:
	cv::Rect face;
	face.x = 0;
	face.y = 0;
	face.width = 10;
	face.height = 10;
	cv::Mat frame = cv::imread("1.jpg");
	cv::Mat facesROI
	// 此处也该保护一下,防止超出边界
	facesROI = frame(face);
方法2:


		cv::Mat cutMat;
		cv::Mat frame = cv::imread("1.jpg");
			
		int x_left, x_right, y_top, y_bottom;
		int x = 0;
		int y = 10;
		int w = 100;
		int h = 100;
		// 放大后防止超出边界
		BigRect(frame .cols, frame .rows, x, x + w-1, y , y + h-1, x_left, x_right, y_top, y_bottom);

		//printf("x_l  x_r y_t y_b = %d %d %d %d  \n", x_left, x_right, y_top, y_bottom);
		// 注意,先是y 方向,然后x 方向
		cutMat = frame (cv::Range(y_top, y_bottom), cv::Range(x_left, x_right));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值