ifryed
ifryed

Reputation: 605

How to flip a cv::mat in c++?

I have a cv::mat with 3 layers, and I want to switch between the first and the last layers. Some thing like this:(matlab style)

cv::mat mt = image;
mt = [mt[:,:,2],mt[:,:,1],mt[:,:,0]];

Upvotes: 1

Views: 1377

Answers (2)

marol
marol

Reputation: 4074

More polished ifryed's solution:

#include <algorithm>

cv::Mat im = getImage();
std::vector<cv::Mat> img_rgb;
cv::split(im,img_rgb);
std::iter_swap(img_rgb, img_rgb+2); 
cv::merge(img_rgb,im);

Upvotes: 2

ifryed
ifryed

Reputation: 605

        cv::Mat im = getImage();
        cv::Mat tmp = cv::Mat::zeros(cv::Size(im.rows,im.cols),CV_8UC1);
        std::vector<cv::Mat> img_rgb;
        cv::split(im,img_rgb);

        img_rgb[0].copyTo(tmp);
        img_rgb[2].copyTo(img_rgb[0]);
        tmp.copyTo(img_rgb[2]);
        cv::merge(img_rgb,im);

Upvotes: 1

Related Questions