Ask Your Question

Resize and Remap

asked 2019-03-05 03:47:15 -0500

Raki gravatar image

I have a camera which only provides 4K image, and I cannot really do the image processing I want to do on these images. It's extremely slow.

I thought of resizing the image before the processing starts, but then all the pixel coordinates shift and I cannot get the world coordinates correct, so remapping is needed.

In order to learn how the function works, I am having the following:

import cv2
import numpy as np
import argparse

RESIZE_FACTOR = 0.3 # make it this smaller
POINT = (447, 63)

if __name__ == '__main__':

    # read the input image
    ap = argparse.ArgumentParser()
    ap.add_argument('-i', '--image', required=True,  help='Path to the image')
    args = vars(ap.parse_args())
    raw_img = cv2.imread(args['image'], 1)

    # draw a point on the raw image,POINT, 63, (0,0,255), -1)

    # resize the raw image and redraw
    raw_img = cv2.imread(args['image'], 1)
    resized_img = cv2.resize(raw_img, (0,0), fx=RESIZE_FACTOR, fy=RESIZE_FACTOR),POINT, 63, (0,0,255), -1)

   # use a function to map the pixels

    # compare the results

As you can imagine, the red dot I drew shifted greatly between the raw and resized images, expectedly. Now I would like to use a function (possibly called remap()) to get the actual pixel coordinate of the dot on the raw image.

However, I am not sure if this function would do the magic, neither I am sure how to use it. I think I need to find homography between these images, and it's not a straightforward process.

Is there a simple way of doing what I am having in mind?

edit retag flag offensive close merge delete


i don't think, you need remapping at all.

if you scale an image by a size factor, you scale the coords by the same, that should be simply it, no ?

berak gravatar imageberak ( 2019-03-05 04:05:34 -0500 )edit

I don't think we could simply multiply the pixels with a float (factor). That sounds too good to be true.

Raki gravatar imageRaki ( 2019-03-05 04:14:31 -0500 )edit

You can give a try at least.

HYPEREGO gravatar imageHYPEREGO ( 2019-03-05 04:26:59 -0500 )edit

the coords, not the pixels ....

berak gravatar imageberak ( 2019-03-05 04:28:20 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-03-05 05:56:51 -0500

Raki gravatar image

updated 2019-03-05 06:00:16 -0500

So, the approach suggested by @berak somehow works.

# If you take a point in the original 4K image, say:
POINT = (447, 63) # take a point in the coordinate plane xy 

# and you have the resizing factor of
RESIZE_FACTOR = 0.3 # make it this smaller

# and you want to map it in the resized image, then you can do the following:
lpoint = list(POINT) # convert the tuple to list
scaled_point = (round(lpoint[0]*RESIZE_FACTOR), round(lpoint[1]*RESIZE_FACTOR)) 

which prints:

(134, 19) and that is more or less the scaled version of the original point (447,63) by 0.3, which was the resizing factor. I say more or less, because we are using the function round() which may cause some loss depending on the case. But in general, this works.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2019-03-05 03:47:15 -0500

Seen: 871 times

Last updated: Mar 05 '19