1. 程式人生 > >大圖中提取小圖

大圖中提取小圖

  1. C程式碼
    傳入的RGB資料格式
    RRRRRRRRRRRRRR
    GGGGGGGGGGGGG
    BBBBBBBBBBBBBBB
typedef struct SC_RECT_
{
    int x, y, w, h;
}SC_RECT;

void visionCutImage(float *srcRgb, float *subRgb, SC_RECT *rect, int srcH, int srcW)
{
	if (NULL == srcRgb || NULL == subRgb)
	{
		return;
	}
	int i, j;
	int h = rect->h;
	int w = rect->w;
	int x = rect->x;
	int y = rect->y;
	
	float R, G, B;
	for (i = 0; i < h; i++)
	{
		for (j = 0; j < w; j++)
		{	
			R = srcRgb[y*srcW + i*srcW + x + j];
			G = srcRgb[srcW*srcH + y*srcW + i*srcW + x + j];
			B = srcRgb[srcW*srcH*2 + y*srcW + i*srcW + x +j];
			subRgb[i*w+j] = R;
			subRgb[h*w+i*w+j] = G;
			subRgb[h*w*2+i*w+j] = B;
		}
	}
}
  1. 呼叫opencv介面
cv::Mat mat;
cv::Rect roi; 
roi.x = x; 
roi.y = y; 
roi.width = w; 
roi.height = h; 
cv::Mat *cropMat = new cv::Mat; 
*cropMat = mat(roi); 
cv::cvtColor(*cropMat, *cropMat, CV_RGB2BGR);
cv::imwrite("./test.png", *cropMat);