# Assign one image's index location to another image

**URL:** <https://forum.opencv.org/t/assign-one-images-index-location-to-another-image/2172>\
**Category:** C++\
**Tags:** mat, core\
**Created:** [March 15, 2021, 11:48am UTC](https://forum.opencv.org/t/assign-one-images-index-location-to-another-image/2172 "2021-03-15T11:48:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vazabdul](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/vazabdul/32/332_2.png) [@Vazabdul](https://forum.opencv.org/u/Vazabdul)\
**Post date:** [March 15, 2021, 11:48am UTC](https://forum.opencv.org/t/assign-one-images-index-location-to-another-image/2172/1 "2021-03-15T11:48:53Z")

</div>

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,:); 

end 
end 

```

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];

                waitKey(0);
            }
       }
```

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [March 15, 2021, 12:20pm UTC](https://forum.opencv.org/t/assign-one-images-index-location-to-another-image/2172/2 "2021-03-15T12:20:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Vazabdul](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/vazabdul/32/332_2.png) [@Vazabdul](https://forum.opencv.org/u/Vazabdul)\
**Post date:** [March 15, 2021, 12:31pm UTC](https://forum.opencv.org/t/assign-one-images-index-location-to-another-image/2172/3 "2021-03-15T12:31:46Z")

</div>

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](https://en.wikipedia.org/wiki/Rotation_matrix)), 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];`

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [March 15, 2021, 12:44pm UTC](https://forum.opencv.org/t/assign-one-images-index-location-to-another-image/2172/4 "2021-03-15T12:44:11Z")

</div>

> [@Vazabdul](#):
>
> I mean, I can’t use imrotate functions…

please explain, why it is so ? you could use one of the [cv::warpXXX](https://docs.opencv.org/master/da/d54/group __imgproc__ transform.html#ga0203d9ee5fcd28d40dbc4a1ea4451983) functions

---

<div class="post-metadata">

**Author:** ![Vazabdul](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/vazabdul/32/332_2.png) [@Vazabdul](https://forum.opencv.org/u/Vazabdul)\
**Post date:** [March 15, 2021, 1:03pm UTC](https://forum.opencv.org/t/assign-one-images-index-location-to-another-image/2172/5 "2021-03-15T13:03:36Z")

</div>

Because I am not allowed to

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [March 15, 2021, 1:35pm UTC](https://forum.opencv.org/t/assign-one-images-index-location-to-another-image/2172/6 "2021-03-15T13:35:34Z")

</div>

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

[https://docs.opencv.org/master/d3/d63/classcv\_1\_1Mat.html](https://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html)
