xulingxin_branch
7799 2 years ago
parent dba2ffa1a7
commit b0462c2fc4

@ -0,0 +1,42 @@
#include <QCoreApplication>
#include <QDebug>
#include <cmath>
double calculateRegionSize(int pixel_size, int top_left_pixel_x, int top_left_pixel_y, int bottom_right_pixel_x, int bottom_right_pixel_y, int h, int size)
{
// 计算选择区域内像素数
int pixels_in_region = (bottom_right_pixel_x - top_left_pixel_x) * (bottom_right_pixel_y - top_left_pixel_y);
// 计算像素对应实际大小
double real_size = (h * 1000.0) * pixel_size / size; // 单位mm/像素
// 计算选定区域的实际大小(单位:米)
double region_size = std::sqrt(pixels_in_region * real_size * real_size) / 1000.0; // 单位m
return region_size;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 相机像素大小 像素与像素距离
int pixel_size = 5;
// 指定区域范围,左上角和右下角像素坐标
int top_left_pixel_x = 100;
int top_left_pixel_y = 150;
int bottom_right_pixel_x = 200;
int bottom_right_pixel_y = 250;
// 估计参数
int h = 10;
int size = 100;
// 计算选定区域的实际大小(单位:米)
double region_size = calculateRegionSize(pixel_size, top_left_pixel_x, top_left_pixel_y, bottom_right_pixel_x, bottom_right_pixel_y, h, size);
qDebug() << "The selected region size is:" << region_size << "m.";
return a.exec();
}

@ -0,0 +1,36 @@
#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QPixmap>
#include <QPainter>
// 将矩形范围内绘制为指定颜色的矩形,并添加透明度
void drawRectWithTransparency(QPixmap& pixmap, const QRect& rect, const QColor& color, int transparency)
{
QPainter painter(&pixmap);
painter.setCompositionMode(QPainter::CompositionMode_Clear);
painter.fillRect(rect, QColor(0, 0, 0, 0));
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
painter.fillRect(rect, QColor(color.red(), color.green(), color.blue(), transparency));
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
// 创建窗口和标签
QWidget window;
QLabel label(&window);
// 加载图片
QPixmap pixmap(":1.png");
QRect rect(360, 360, 300, 250);
drawRectWithTransparency(pixmap, rect, QColor(255, 0, 0), 100);
label.setPixmap(pixmap);
// 显示窗口和标签
window.show();
label.show();
return app.exec();
}

@ -0,0 +1,47 @@
#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QPixmap>
#include <QPainter>
#include <QVBoxLayout>
void drawRectOnImage(QString imagePath, QRect rect1, QRect rect2, QColor color1, QColor color2)
{
// 加载图片
QPixmap pixmap(imagePath);
// 指定范围并绘制透明红色和黑色矩形
QPainter painter(&pixmap);
painter.setCompositionMode(QPainter::CompositionMode_Clear);
painter.fillRect(rect1, QColor(0, 0, 0, 0));
painter.fillRect(rect2, color2); // 黑色矩形
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
painter.fillRect(rect1, color1);
// 显示图片
QWidget *mainWindow = new QWidget();
QVBoxLayout *layout = new QVBoxLayout(mainWindow);
QLabel *label = new QLabel(mainWindow);
layout->addWidget(label);
label->setFixedSize(pixmap.size());
label->setPixmap(pixmap);
mainWindow->show();
// 进入主事件循环
QApplication::exec();
delete mainWindow;
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
// 绘制矩形到图片中,并在标签上显示图片
QRect rect1(360, 360, 300, 250);
QRect rect2(400, 400, 300, 250);
QColor color1(255, 0, 0, 100);
QColor color2(0, 0, 255, 100);
drawRectOnImage(":1.png", rect1, rect2, color1, color2);
return 0;
}
Loading…
Cancel
Save