将坐标映射到调整大小的图像

发布于 2025-01-14 15:49:41 字数 436 浏览 1 评论 0原文

我的原始图像尺寸为(969,348),调整后的图像尺寸为(696,484)。 我在调整大小的图像中有一个点(385,400),所以我想将其绘制到原始图像。 oldx = 385 oldy = 400 我使用 OpenCV C++ 调整了它的大小

resize(org, resizeimage, Size(org.rows / 0.5, org.cols / 2), 0, 0, INTER_AREA);

,我对行和列感到困惑,宽度和高度是多少。 我已经尝试过了,但它给了我 (0,484) 坐标。

Point(round(float((oldx / org.cols)) * resizeimage.cols), round(float((oldy / org.rows))) * resizeimage.rows)

I have original image of size (969,348) and the resized image is (696,484).
I have a point (385,400) in the resized image, so I want to plot it to the original image.
oldx = 385 oldy = 400
I have resized it using

resize(org, resizeimage, Size(org.rows / 0.5, org.cols / 2), 0, 0, INTER_AREA);

I am using OpenCV C++ and where I am confused with the rows and cols, what is the width and height.
I have tried this but it gives me (0,484) coordinate.

Point(round(float((oldx / org.cols)) * resizeimage.cols), round(float((oldy / org.rows))) * resizeimage.rows)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多孤肩上扛 2025-01-21 15:49:41

中心的距离与缩放比例(在每个轴上)成正比。

我们可以按如下方式计算 newxnewy

  • oldx 中减去 x_original_centery_original_center
    相减后,(0, 0) 是“新中心”(应用“居中坐标系”)。
  • scale_xscale_y 缩放“零中心”坐标。
  • 通过添加 x_scaled_center 和 y_scaled_center 将“缩放零中心”坐标转换为“左上角 (0, 0)”。

插图:

Origin:
(0,0)                     New coordinate system: (0,0) center 
     -----------------                       -----------------                
    |                 | x -= (cols-1)/2     |    (0,0)        |
    |                 | ===============>    |        +        |
    |                 | y -= (rows-1)/2     |                 |
     -----------------                       -----------------
     
     
    After scaling:
    The distance to the center is proportional to the scale.
    In x axis the distance is proportional to x_scale
    In y axis the distance is proportional to y_scale


                                        -------------
                                       |             |
                                       |      ^    o |
                                       | y*sy |      |
     --------^--------                 |      |      |
    |      y |     o  |    Scale       |      V      |
    |        +<--->   | ===========>   |      +<-->  |
    |           x     |                |       x*sx  |
     -----------------                 |             |
                                       |             |
                                       |             |
                                       |             |
                                        -------------    
                                        
Convert the scaled result to origin (0, 0): 
newx += (new_cols-1)/2
newy += (new_rows-1)/2

准确计算中心:
C++ 转换为:
(0, 0) 是左上角,(cols-1, rows-1) 是右下角坐标。
准确的中心坐标为:
x_original_center = (original_rows-1.0)/2.0
y_original_center = (original_cols-1.0)/2.0


C++ 代码示例:

#include <cmath>
#include <iostream>
#include "opencv2/opencv.hpp"
#include <opencv2/highgui/highgui.hpp>

int main()
{
    double scale_x = 2.0;
    double scale_y = 0.5;

    int oldx = 385;
    int oldy = 400;

    cv::Mat org = cv::Mat::zeros(cv::Size(969, 348), CV_8UC1);  //Fill with zeros (for example).
    cv::Mat resizeimage;

    //Height and width before resize:
    int rows = org.rows;    //384
    int cols = org.cols;    //969

    //cv::resize(org, resizeimage, Size(org.rows / 0.5, org.cols / 2.0), 0, 0, INTER_AREA);

    //Instead of dividing by 0.5, sacle by 2.0 (and Instead of dividing by 2.0, scale by 0.5)
    cv::resize(org, resizeimage, cv::Size((int)std::round(rows * scale_x), (int)std::round(cols * scale_y)), 0, 0, cv::INTER_AREA);

    //Height and width after resize:
    int resized_rows = resizeimage.rows;    //485
    int resized_cols = resizeimage.cols;    //696

    //Center before resize:
    double x_original_center = ((double)cols - 1.0) / 2.0;  //484.0
    double y_original_center = ((double)rows - 1.0) / 2.0;  //173.5

    //Center after resize:
    double x_scaled_center = ((double)resized_cols - 1.0) / 2.0;    //347.5
    double y_scaled_center = ((double)resized_rows - 1.0) / 2.0;    //242

    //Subtract the center, scale, and add the "scaled center".
    int newx = (int)std::round((oldx - x_original_center) * scale_x + x_scaled_center); //150
    int newy = (int)std::round((oldy - y_original_center) * scale_y + y_scaled_center); //355

    std::cout << "newx = " << newx << std::endl << "newy = " << newy << std::endl;

    return 0;
}

The distance for the center is proportional to the scaling (in each axis).

We may compute newx and newy as follows:

  • Subtract x_original_center and y_original_center from oldx and oldy.
    After subtraction, (0, 0) is the "new center" (applies "centered coordinate system").
  • Scale the "zero centered" coordinates by scale_x and scale_y.
  • Convert the "scaled zero centered" coordinates to "top left (0, 0)" by adding x_scaled_center and y_scaled_center.

Illustration:

Origin:
(0,0)                     New coordinate system: (0,0) center 
     -----------------                       -----------------                
    |                 | x -= (cols-1)/2     |    (0,0)        |
    |                 | ===============>    |        +        |
    |                 | y -= (rows-1)/2     |                 |
     -----------------                       -----------------
     
     
    After scaling:
    The distance to the center is proportional to the scale.
    In x axis the distance is proportional to x_scale
    In y axis the distance is proportional to y_scale


                                        -------------
                                       |             |
                                       |      ^    o |
                                       | y*sy |      |
     --------^--------                 |      |      |
    |      y |     o  |    Scale       |      V      |
    |        +<--->   | ===========>   |      +<-->  |
    |           x     |                |       x*sx  |
     -----------------                 |             |
                                       |             |
                                       |             |
                                       |             |
                                        -------------    
                                        
Convert the scaled result to origin (0, 0): 
newx += (new_cols-1)/2
newy += (new_rows-1)/2

Computing the center accurately:
The C++ conversion is:
(0, 0) is the top left, and (cols-1, rows-1) is the bottom right coordinate.
The accurate center coordinate is:
x_original_center = (original_rows-1.0)/2.0
y_original_center = (original_cols-1.0)/2.0


C++ code sample:

#include <cmath>
#include <iostream>
#include "opencv2/opencv.hpp"
#include <opencv2/highgui/highgui.hpp>

int main()
{
    double scale_x = 2.0;
    double scale_y = 0.5;

    int oldx = 385;
    int oldy = 400;

    cv::Mat org = cv::Mat::zeros(cv::Size(969, 348), CV_8UC1);  //Fill with zeros (for example).
    cv::Mat resizeimage;

    //Height and width before resize:
    int rows = org.rows;    //384
    int cols = org.cols;    //969

    //cv::resize(org, resizeimage, Size(org.rows / 0.5, org.cols / 2.0), 0, 0, INTER_AREA);

    //Instead of dividing by 0.5, sacle by 2.0 (and Instead of dividing by 2.0, scale by 0.5)
    cv::resize(org, resizeimage, cv::Size((int)std::round(rows * scale_x), (int)std::round(cols * scale_y)), 0, 0, cv::INTER_AREA);

    //Height and width after resize:
    int resized_rows = resizeimage.rows;    //485
    int resized_cols = resizeimage.cols;    //696

    //Center before resize:
    double x_original_center = ((double)cols - 1.0) / 2.0;  //484.0
    double y_original_center = ((double)rows - 1.0) / 2.0;  //173.5

    //Center after resize:
    double x_scaled_center = ((double)resized_cols - 1.0) / 2.0;    //347.5
    double y_scaled_center = ((double)resized_rows - 1.0) / 2.0;    //242

    //Subtract the center, scale, and add the "scaled center".
    int newx = (int)std::round((oldx - x_original_center) * scale_x + x_scaled_center); //150
    int newy = (int)std::round((oldy - y_original_center) * scale_y + y_scaled_center); //355

    std::cout << "newx = " << newx << std::endl << "newy = " << newy << std::endl;

    return 0;
}
看轻我的陪伴 2025-01-21 15:49:41

我更喜欢以下方式的地图坐标:

float x_ratio = resizeimage.cols / static_cast<float>(org.cols);
float y_ratio = resizeimage.rows / static_cast<float>(org.rows);
cv::Point newPoint(static_cast<int>(oldx * x_ratio), 
                   static_cast<int>(oldy * y_ratio));

I prefer map coordinates in the following way:

float x_ratio = resizeimage.cols / static_cast<float>(org.cols);
float y_ratio = resizeimage.rows / static_cast<float>(org.rows);
cv::Point newPoint(static_cast<int>(oldx * x_ratio), 
                   static_cast<int>(oldy * y_ratio));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文