Assign one image's index location to another image

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