Submatrix in C++

1.3k views Asked by At

I have a programming issue regarding the extraction of a subimage (submatrix) from a bigger image (matrix). I have two points (upper and lower bound of the subimage i want to extract) and i want to extract the subimage from the bigger one based on these points. But I can't find how to do thins with C/C++.

I know it's very easy to do with matlab. Suppose these two points are (x_max,y_max) and (x_min,y_min). To extract the subimage I just need to code the following:

(MATLAB CODE)-> small_image=big_image(x_min:x_max,y_min,y_max); 

But in C i can't use an interval of indexes with : as i do with Matlab. Does anybody here faced this problem before?

2

There are 2 answers

1
Simon On

If you are doing image processing in C/C++, you should probably use OpenCV.

The cv::Mat class can do this using a Region Of Interest (ROI).

0
Frederick On

In straight c++, you'd use a loop.

int* small_im[]; // or whatever the syntax is

int i = 0, j = 0;
for (i = 0; i < (x_max-x_min); i++)
{
    for (j = 0; j < (y_max-y_min); j++)
    {
      small_im[i][j] = big_im[x_min+i][y_min+j];
    }
}