大图中提取小图

版权声明:本文为博主原创文章,欢迎转载。转载请注明出处 https://blog.csdn.net/jobbofhe/article/details/84144242
  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);
		

猜你喜欢

转载自blog.csdn.net/jobbofhe/article/details/84144242
今日推荐