1
votes

I have an opencv mat which has M rows and 3 columns, is there a way to reorder the mat such that the first and last (i.e., third) columns are switched while the middle column kept in place without copying the data?

2
can you elaborate what is "without copying data " ? - I.Newton
@I.Newton : Sure, I mean without deep copy of going element by element but just "playing" with the mat's header - Benny K

2 Answers

3
votes

OpenCV data is an array of pixels. Sometimes you can get a column or a rectangle view of an image (like with col() ). In which the data is not continuous, and it is calculated, as far as I know, with a step between rows. However the data is shared and is still an array.

Then the question becomes: can I swap two portions of an array without copying the data? Not as far as I know.

You can use optimized functions of OpenCV to swap them, but the data will be copied.

Also, non continuous data is way slower than continuous data in OpenCV functions. More can be read here.

2
votes

You can use the OpenCV function flip for that. As an example the following code flips an image about the mid column.

int main ()

{
    Mat img, flipped;       //your mat

    img =imread("lena.jpg");

    flip(img,flipped ,1);  // flipped is the output

    imshow("img",flipped);
    waitKey(0);

}