Kunal Sonone
Kunal Sonone

Reputation: 43

Image Array in OpenCV

I am working on a project based on Template Matching using OpenCV.

How can I make an array of images ?

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));

I am not sure I am doing it in a right way. Please help me.

Upvotes: 0

Views: 1792

Answers (1)

mcchu
mcchu

Reputation: 3359

First, create a region of interest (ROI) from ref_image, where the top-left corner of the ROI is (550, 85), and the width and height is 443 & 455:

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

Next, assign the the ROI to your image array:

ref_temp[0] = ref_img_roi;

Now, the ref_temp[0] references to the region specified in ref_img_roi of theref_image.

In your code, the usage of the C++ array is incorrect. You don't have to put cv::Mat when using the ref_temp. And, the index of array should be 0 ~ 6. The following code will work:

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));

Upvotes: 1

Related Questions