// 放大原始框
#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));