Linnot
Linnot

Reputation: 29

Horizontal flip in opencv library

I have following problem. I am trying to make an horizontal flip, my code is compiling but after that i have runtime error. I post my code below, and some part of mat.inl.hpp header where the mistake occurs.

int HorizontalFlip(Mat img)
{
Mat img4 = img.clone();
for (int i = 0; i < img4.rows; i++)
{
    for (int j = 0; j < ceil(img4.cols / 2); j++)
    {
        Vec3b &intensity = img4.at<Vec3b>(i, j);
        Vec3b &intensity2 = img4.at<Vec3b>(i, img4.cols);
        Vec3b bufor;
        int k = ceil(img4.cols / 2);
        for (int channel = 0; channel < img4.channels(); channel++)
        {
            do 
            {
                bufor = intensity;
                intensity = intensity2;
                intensity2 = bufor;
            } while (ceil(img4.cols / 2) < k);
        }
    }
}
namedWindow("Horizontal Flip", WINDOW_AUTOSIZE);
imshow("Horizontal Flip", img4);
return 0;
}


 CV_DbgAssert((unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()));

Thank you for your time, which you spent on solving my problem.

Upvotes: 0

Views: 1191

Answers (1)

Micka
Micka

Reputation: 20130

try

Mat img4 = img.clone();
for (int i = 0; i < img4.rows; i++)
{
    for (int j = 0; j < ceil(img4.cols); j++)
    {
        img4.at <cv::Vec3b (i,j) = img.at <cv::Vec3b (i, img.cols-1-j);
    }
}
if (img4.empty ()) return 1;
cv::imshow ("flipped", img4);
cv::waitKey (0);

Upvotes: 2

Related Questions