Assign one image's index location to another image

I want to assign one image’s index location to another image’s i(current row) and j (current column) location. (I hope that this introduction clarified my situation.)

Also, I need to mention I can only use openCV for I\O functions.

I code these kinds of things in Matlab with the combination of two nested for loop and with one line. For instance:

for R=1:Height
   for  C=1: Width

      % some operations.
    outimage(R,C,  :) = processimage(XPrime,YPrime,:); 


I want to learn how can I code this line in C++. I am also using OpenCV.
outimage(R,C,:) = processimage(XPrime,YPrime,:)

To implement this In C++, I read the image like this:

Mat img = imread(imgPath);
    int height = img.rows; 
    int width = img.cols;  

Then, I created for loops :

for (i = 0; i <= height; i++)


        for (j = 0; j <= width; j++)

            // some operations

                for (k=0;k<=2;k++) 


        if(Xprime >= 1 && Yprime >= 1 && Xprime <= height && Yprime <= width)
                returnedOutImage[i][j][k] = img[Xprime][Yprime][k];
                //out_image[i][j]=rgb_image[(Xprime-1)*width +Yprime];


helo, please tell us, what your code is trying to achieve !
(do you have a formula / algorithm, you can show us ?)

you probably should not write per-pixel loops like that.
apart from the current out-of-bounds errors (j <= width , Xprime <= height, etc.) , code like that is slow and error-prone.

maybe we can help you finding something more safe & fast, actually using opencv

I want to rotate an image with a given input degree. I implemented a rotation matrix. [Link] You can check from here. (Rotation matrix - Wikipedia), but as I said I can benefit from OpenCV by using I\O functions etc. I mean, I can’t use imrotate functions…

Also if we have a choice to write this line using openCV we can do it here:
returnedOutImage[i][j][k] = img[Xprime][Yprime][k];

please explain, why it is so ? you could use one of the cv::warpXXX functions

Because I am not allowed to

you need the “at” method:<Vec3b>(i,j) (if you have a picture with CV_8UC3 data). it works for reading a value and for SETTING a value.

1 Like