OpenCV中的图像数组

Image Array in OpenCV

本文关键字:数组 图像 OpenCV      更新时间:2023-10-16

我正在使用OpenCV进行基于模板匹配的项目。

如何创建一个图像数组?

cv::Mat ref_temp[7]; // Array Declaration as we do in c/c++
cv::Mat image = cv::imread("ref.jpg",1);  
cv::Mat ref_image;
image.copyTo( ref_image);   
cv::Mat ref_temp(1) =(ref_image, cv::Rect(550,85, 433, 455));
cv::Mat ref_temp[2] =(ref_image, cv::Rect(1042,85,433, 455));
cv::Mat ref_temp[3] =(ref_image, cv::Rect(1528,85,433, 455));
cv::Mat ref_temp[4] =(ref_image, cv::Rect(65, 1010, 423, 442));
cv::Mat ref_temp[5] =(ref_image, cv::Rect(548, 1010, 423, 442));
cv::Mat ref_temp[6] =(ref_image, cv::Rect(1025, 1010, 423, 442));
cv::Mat ref_temp[7] =(ref_image, cv::Rect(1529, 1010, 423, 442));

我不确定我做这件事的方式是否正确。

首先,从ref_image创建一个感兴趣区域(ROI),其中ROI的左上角为(550,85),宽度和高度为443 &455:

cv::Mat ref_img_roi(ref_image, cv::Rect(550, 85, 433, 455);

接下来,将ROI分配给图像数组:

ref_temp[0] = ref_img_roi;

现在,ref_temp[0]引用ref_imageref_img_roi中指定的区域。

在你的代码中,c++数组的用法是不正确的。当使用ref_temp时,您不必放置cv::Mat。并且,数组的索引应为0 ~ 6。下面的代码可以工作:

cv::Mat ref_temp[7];
cv::Mat image = cv::imread("ref.jpg",1);  
cv::Mat ref_image;
image.copyTo( ref_image);
ref_temp[0] = cv::Mat(ref_image, cv::Rect(550, 85, 433, 455));
ref_temp[1] = cv::Mat(ref_image, cv::Rect(1042, 85, 433, 455));
ref_temp[2] = cv::Mat(ref_image, cv::Rect(1528, 85, 433, 455));
ref_temp[3] = cv::Mat(ref_image, cv::Rect(65, 1010, 423, 442));
ref_temp[4] = cv::Mat(ref_image, cv::Rect(548, 1010, 423, 442));
ref_temp[5] = cv::Mat(ref_image, cv::Rect(1025, 1010, 423, 442));
ref_temp[6] = cv::Mat(ref_image, cv::Rect(1529, 1010, 423, 442));