MutedSeason
MutedSeason

Reputation: 31

How to relate an image with another image in OpenCV

I'm doing this project in OpenCV C++ where i make the reflection of a given image, just like the flip function but with the coordinates of each pixel. the problem is that the image output that i get is all blue with a line horizontally, i believe that my code is only affecting the first channel.

I tried to do imageReflectionFinal.at<Vec3b>(r,c) = image.at<Vec3b>(r,c); in order to solve it, but nothing changed. I'll leave the code below, thanks in advance.

    Mat image = imread("image_dir/image.jpg");
    Mat imageReflectionFinal = Mat::zeros(image.size(), image.type());
    for(unsigned int r=0; r<image.rows; r++) {
        for(unsigned int c=0; c<image.cols; c++) {
                imageReflectionFinal.at<Vec3b>(r,c) = image.at<Vec3b>(r,c);
                Vec3b sourcePixel = image.at<Vec3b>(r,c);
                imageReflectionFinal.at<Vec3b>(r, c) = (uchar)(c, -r + (220)/2);
        }
    }

Upvotes: 1

Views: 248

Answers (1)

Yunus Temurlenk
Yunus Temurlenk

Reputation: 4342

If you don't want to use flip function, you can change the x-coordinates(cols) of each rows mirrorly. Here is the code:

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

using namespace cv;

int main() {

    //You can change as "Mat3b" for the 3-channel images
    Mat1b image = imread("/ur/image/directory/image.jpg",CV_LOAD_IMAGE_GRAYSCALE);
    Mat1b imageReflectionFinal = Mat::zeros(image.size(), image.type());
    for(unsigned int r=0; r<image.rows; r++) {
        for(unsigned int c=0; c<image.cols; c++) {

            imageReflectionFinal(r, c) = image(r, image.cols - 1 - c);
            //y-axis(r) doesnt change only x-axis(cols) mirroring 
        }
    }

    imshow("Result",imageReflectionFinal);
    waitKey(0);
    return 0;
}

This answer is also my reference.

Upvotes: 3

Related Questions