After resizing an image with cv2, how to get the new bounding box coordinate

2024/10/6 22:22:06

I have an image of size 720 x 1280, and I can resize it to 256 x 256 like this

import cv2
img = cv2.imread('sample_img.jpg')
img_small = cv2.resize(img, (256, 256), interpolation=cv2.INTER_CUBIC)

Say I have a bounding box in the original image (top left corner (50, 100), bottom right corner (350, 300)), how do I get the coordinates of new bounding box?

Answer

You can do it by simply using the scale of your resize operation. Like this -

import numpy as np# Get the scaling factor
# img_shape = (y, x)
# reshaped_img_shape = (y1, x1)
# the scaling factor = (y1/y, x1/x)
scale = np.flipud(np.divide(reshaped_img_shape, img_shape))  # you have to flip because the image.shape is (y,x) but your corner points are (x,y)#use this on to get new top left corner and bottom right corner coordinates
new_top_left_corner = np.multiply(top_left_corner, scale )
new_bottom_right_corner = np.multiply(bottom_right_corner, scale )
https://en.xdnf.cn/q/70319.html

Related Q&A

convert a tsv file to xls/xlsx using python

I want to convert a file in tsv format to xls/xlsx..I tried usingos.rename("sample.tsv","sample.xlsx")But the file getting converted is corrupted. Is there any other method of doing…

How do you edit cells in a sparse matrix using scipy?

Im trying to manipulate some data in a sparse matrix. Once Ive created one, how do I add / alter / update values in it? This seems very basic, but I cant find it in the documentation for the sparse ma…

AttributeError: DataFrame object has no attribute _data

Azure Databricks execution error while parallelizing on pandas dataframe. The code is able to create RDD but breaks at the time of performing .collect() setup: import pandas as pd # initialize list of …

Python: Problem with overloaded constructors

WARNING: I have been learning Python for all of 10 minutes so apologies for any stupid questions!I have written the following code, however I get the following exception: Message FileName Li…

Validate inlines before saving model

Lets say I have these two models:class Distribution(models.Model):name = models.CharField(max_length=32)class Component(models.Model):distribution = models.ForeignKey(Distribution)percentage = models.I…

Grouping and comparing groups using pandas

I have data that looks like:Identifier Category1 Category2 Category3 Category4 Category5 1000 foo bat 678 a.x ld 1000 foo bat 78 l.o …

Transform a 3-column dataframe into a matrix

I have a dataframe df, for example:A = [["John", "Sunday", 6], ["John", "Monday", 3], ["John", "Tuesday", 2], ["Mary", "Sunday…

python multiline regex

Im having an issue compiling the correct regular expression for a multiline match. Can someone point out what Im doing wrong. Im looping through a basic dhcpd.conf file with hundreds of entries such as…

OpenCV Python Bindings for GrabCut Algorithm

Ive been trying to use the OpenCV implementation of the grab cut method via the Python bindings. I have tried using the version in both cv and cv2 but I am having trouble finding out the correct param…

showing an image with Graphics View widget

Im new to qt designer and python. I want to created a simple project that I should display an image. I used "Graphics View" widget and I named it "graphicsView". I wrote these funct…