1、功能实现
上下左右键可以实现移动底图
数据通过json输出过来,目标显示在底图相应的位置
2、代码实现
void yourclass::showAddImage(QJsonArray dataWithin20km)
{
// 背景图
QString backgroundImagePath = QCoreApplication::applicationDirPath() + "/pictrue/qing.jpg";
std::string backgroundImagePathStr = backgroundImagePath.toStdString();
cv::Mat background = cv::imread(backgroundImagePathStr);
// 飞机图
QString airplaneImagePath;
std::string airplaneImagePathStr;
for (int i = 0; i < dataWithin20km.size(); i++) {
QJsonObject jsonObject = dataWithin20km[i].toObject();
if (jsonObject.contains("azimuth") && jsonObject.contains("elevationAngleDeg")) {
double azimuth = jsonObject["azimuth"].toDouble();
double elevationAngleDeg = jsonObject["elevationAngleDeg"].toDouble();
// 现在你可以使用 azimuth 和 elevationAngleDeg 值
qDebug() << "azimuth: " << azimuth << ", elevationAngleDeg: " << elevationAngleDeg;
double x = azimuth * 4000.0 / 360.0;
double y = 2000.0 - elevationAngleDeg * 2000.0 / 20.0;
// 确定要加载的飞机图像
int platformKind = jsonObject["platformKind"].toInt();
if (platformKind == 4) {
airplaneImagePath = QCoreApplication::applicationDirPath() + "/pictrue/DD.png"; // 小飞机图路径
} else if (platformKind == 2) {
airplaneImagePath = QCoreApplication::applicationDirPath() + "/pictrue/panle2.png"; // 大飞机图路径
}
airplaneImagePathStr = airplaneImagePath.toStdString();
cv::Mat airplaneImage = cv::imread(airplaneImagePathStr); // 加载飞机图像
// 确保 x 和 y 在有效范围内
if (x >= 0 && y >= 0 && (x + airplaneImage.cols) <= background.cols && (y + airplaneImage.rows) <= background.rows) {
// 创建矩形区域
cv::Rect regionOfInterest(static_cast<int>(x), static_cast<int>(y), airplaneImage.cols, airplaneImage.rows);
cv::Mat backgroundROI = background(regionOfInterest);
// 将飞机图像复制到背景图像,使用透明通道作为掩码
airplaneImage.copyTo(backgroundROI, airplaneImage);
}
}
}
image = background;
// 创建裁剪区域的坐标
cv::Rect cropRegion(currentX, currentY, 2000, 500);
// 裁剪合并后的背景图
cv::Mat croppedMergedImage = background(cropRegion);
if (!croppedMergedImage.empty()) {
// 将裁剪后的图像转换为 Qt 图像
QImage qImage(croppedMergedImage.data, croppedMergedImage.cols, croppedMergedImage.rows, croppedMergedImage.step, QImage::Format_BGR888);
// 显示在 QLabel 中
ui->label_3->setPixmap(QPixmap::fromImage(qImage));
ui->label_3->setScaledContents(true);
} else {
qDebug() << "无法加载图像";
}
}