| 616 | } |
| 617 | |
| 618 | bool Utils::displayMatOnLabel(QLabel* label, const QImage& image) { |
| 619 | // 检查输入有效性 |
| 620 | if (!label) { |
| 621 | qWarning() << "QLabel指针为空!"; |
| 622 | return false; |
| 623 | } |
| 624 | if (image.isNull()) { |
| 625 | qWarning() << "QImage图像为空!"; |
| 626 | return false; |
| 627 | } |
| 628 | |
| 629 | // 直接转换为QPixmap |
| 630 | QPixmap pixmap = QPixmap::fromImage(image); |
| 631 | if (pixmap.isNull()) { |
| 632 | qWarning() << "QPixmap转换失败!"; |
| 633 | return false; |
| 634 | } |
| 635 | |
| 636 | // 计算缩放后的尺寸(保持宽高比) |
| 637 | const int labelWidth = label->width(); |
| 638 | const int labelHeight = label->height(); |
| 639 | const double ratio = qMin(static_cast<double>(labelWidth) / pixmap.width(), |
| 640 | static_cast<double>(labelHeight) / pixmap.height()); |
| 641 | const int scaledWidth = qRound(pixmap.width() * ratio); |
| 642 | const int scaledHeight = qRound(pixmap.height() * ratio); |
| 643 | |
| 644 | // 高质量缩放并设置到Label |
| 645 | QPixmap scaledPixmap = pixmap.scaled(scaledWidth, scaledHeight, |
| 646 | Qt::KeepAspectRatio, Qt::SmoothTransformation); |
| 647 | label->setPixmap(scaledPixmap); |
| 648 | label->setAlignment(Qt::AlignCenter); // 居中显示 |
| 649 | |
| 650 | return true; |
| 651 | } |
| 652 | |
| 653 | bool Utils::findPic(const cv::Mat& sourceImage, const cv::Mat& templateImage, double threshold, int& outX, int& outY) { |
| 654 | if (sourceImage.empty() || templateImage.empty()) { |