qt opencv 图片缩放
时间: 2023-11-01 08:08:07 浏览: 118
您可以使用OpenCV和Qt来实现图像的缩放。下面是一个示例代码,演示了如何在Qt中加载和显示图像,并使用OpenCV进行缩放:
```cpp
#include <QCoreApplication>
#include <QImage>
#include <QLabel>
#include <opencv2/opencv.hpp>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 加载图像
QImage image("path_to_image.jpg");
// 将图像转换为OpenCV Mat格式
cv::Mat srcImage(image.height(), image.width(), CV_8UC3, image.bits(), image.bytesPerLine());
// 缩放图像
cv::Mat resizedImage;
cv::resize(srcImage, resizedImage, cv::Size(), 0.5, 0.5); // 缩放比例为0.5
// 将缩放后的图像转换为Qt QImage格式
QImage scaledImage(resizedImage.data, resizedImage.cols, resizedImage.rows, resizedImage.step, QImage::Format_RGB888);
// 显示缩放后的图像
QLabel label;
label.setPixmap(QPixmap::fromImage(scaledImage));
label.show();
return a.exec();
}
```
请将 "path_to_image.jpg" 替换为您要加载的图像路径。代码中的缩放比例为0.5,您可以根据需求调整。
这段代码将加载图像,使用OpenCV进行缩放,然后将缩放后的图像显示在一个Qt窗口中。
阅读全文
相关推荐


















