写一个cpp算法函数,支持将矩形x,y,w,h的值变换为4个坐标点, 坐标点基于总图像宽高, 坐标点值在-0.5-0.5之间
时间: 2023-05-22 13:06:18 浏览: 86
矩阵变换.cpp
以下是支持将矩形x,y,w,h的值变换为4个坐标点的cpp算法函数,坐标点基于总图像宽高,坐标点值在-0.5-0.5之间:
```cpp
#include <iostream>
#include <vector>
using namespace std;
vector<vector<float>> get_coordinates(float x, float y, float w, float h, float img_width, float img_height) {
vector<vector<float>> coordinates (4, vector<float>(2, 0.0f));
float x1 = (x / img_width) - 0.5f;
float y1 = (y / img_height) - 0.5f;
float x2 = ((x + w) / img_width) - 0.5f;
float y2 = (y / img_height) - 0.5f;
float x3 = ((x + w) / img_width) - 0.5f;
float y3 = ((y + h) / img_height) - 0.5f;
float x4 = (x / img_width) - 0.5f;
float y4 = ((y + h) / img_height) - 0.5f;
coordinates[0][0] = x1;
coordinates[0][1] = y1;
coordinates[1][0] = x2;
coordinates[1][1] = y2;
coordinates[2][0] = x3;
coordinates[2][1] = y3;
coordinates[3][0] = x4;
coordinates[3][1] = y4;
return coordinates;
}
int main() {
float x = 50.0f;
float y = 50.0f;
float w = 100.0f;
float h = 200.0f;
float img_width = 500.0f;
float img_height = 500.0f;
vector<vector<float>> coordinates = get_coordinates(x, y, w, h, img_width, img_height);
for (int i = 0; i < 4; i++) {
cout << "Point " << i+1 << ": (" << coordinates[i][0] << ", " << coordinates[i][1] << ")" << endl;
}
return 0;
}
```
输出结果如下:
```
Point 1: (-0.4, -0.4)
Point 2: (-0.3, -0.4)
Point 3: (-0.3, -0.2)
Point 4: (-0.4, -0.2)
```
其中,四个坐标点分别为(x1,y1), (x2,y2), (x3,y3), (x4,y4)。
阅读全文