QT 如何使图片像素变小的问题

            这里主要介绍了在Qt中使图片像素变小,编程的时候,一般都是要添加图片的,但是图片有很大,对于编程人员来说,编程那是小事,要实现快速化缩小图片,就是头疼的事儿了,在你百思不得其解时,请往下看……

以下的代码是将一个600*400的图片弄成400*580显示出来。

 
 
  1. QPixmap pixmap(":/set_background600x400.png");  
  2. QPixmap fitpixmap=pixmap.scaled(400,560, Qt::KeepAspectRatio);  
  3. label->setPixmap(fitpixmap); 

我是参照网上的这句QPixmap fitPixmap = pixmap.scaled(width(),height(), Qt::KeepAspectRatio);

我的例子:

#ifndef TEXT_H  

#define TEXT_H  

#include <QApplication> 

#include <QLabel> 

#include <QPainter> 

#include <QpaintEvent> 

class magic:public QWidget  

{  

public:  

           magic(QWidget *parent=0);  

private:  

          QLabel *label;  

       // void paintEvent(QPaintEvent *);  

};  

#endif                     // TEXT_H  

#include "text.h"  

magic::magic(QWidget *parent):QWidget(parent)  

{  

             setGeometry(20,20,800,600);  

             label=new QLabel(this);  

            label->setGeometry(QRect(10, 10, 400, 580));  

            QPixmap pixmap(":/set_background600x400.png");  

            QPixmap fitpixmap=pixmap.scaled(500, 500).scaled(400,560, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);  

            label->setPixmap(fitpixmap);  

}  

int main(int argc,char **argv)  

{  

            QApplication app(argc,argv);  

             magic M;  

            M.show();  

            return app.exec();  

还有网上的另外一个也有参考价值。 http://www.cuteqt.com/blog/?p=478

在Qt Labs Blog里提到了一个快速缩小大图片的方法, 而且作者说是“几乎所有图形程序员都知道的trick”。 大概因为笔者不是图形程序员的缘故, 对这个小技巧十分之陌生, 我想大家可能也有很大一部分人还不知道这个技巧, 特此memo, 希望对大家有用。

QImage提供了缩放图片的函数 scaled, 并且可以用参数指定”快速缩放(FastTransformation)”还是”平滑缩放(SmoothTransformation)”, 使用还是很方便的。 但是如果你用缩放函数去做大图片的缩略图可能会发现”快速缩放”得到的图片质量不佳, 而”平滑缩放”质量很好但速度欠佳, 特别是原图非常大的时候smoothscale简直就是个噩梦阿。 这里就可以使用被称为“Cheat Scaling”的缩小图片的技巧了, 那就是先使用”快速缩放”得到一个中等大小的图片以获得较快的缩放速度, 再使用”平滑缩放”缩小至需要的大小以获得较好的图片质量。

如下代码:

QImage result = img.scaled(800, 600).scaled(200, 150, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); 

一个公认比较好的方法是,先缩至缩略图4倍大小, 再进一步平滑缩放。 按照原作者的测试, 该算法甚至比“快速缩放”还要略快, 却能获得和“平滑缩放”极其接近的最终结果。

另外Qt中按钮背景图片的切换如下设置:

ui->btn_name->setStyleSheet("QPushButton{background-image: url(:/images/call_up.bmp);}"
"QPushButton:hover{background-image: url(:/images/call_hov.bmp);}"
"QPushButton:pressed{background-image: url(:/images/call_down.bmp);}");

或者设置为字符串的形式,在初始化时调用:

QString str="QPushButton#btn_name{background-image: url(:/images/call_up.bmp)}"
                    "QPushButton#btn_name:hover{background-image: url(:/images/call_hov.bmp);}"
                    "QPushButton#btn_name:pressed{background-image: url(:/images/call_down.bmp);}";

ui->btn_name->setFlat(true);//这句能够实现按钮透明

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Qt中实现图像清晰化的方法可以使用卷积(Convolution)操作。卷积操作是通过在图像上滑动一个卷积核(Convolution Kernel)来实现的,卷积核是一个二维矩阵,其值表示每个像素点在计算中的权重,通过计算每个像素点与其周围像素点的加权平均值来实现图像的清晰化。 以下是一个简单的Qt代码示例,可以实现图像清晰化的功能: ```cpp QImage sharpenImage(const QImage& input, double sigma, double threshold, double amount) { QImage output = input.convertToFormat(QImage::Format_RGB32); const int width = output.width(); const int height = output.height(); const int stride = output.bytesPerLine() / 4; const int radius = static_cast<int>(sigma * 3.0); QVector<double> kernel; kernel.resize(radius * 2 + 1); for (int i = -radius; i <= radius; i++) { kernel[i + radius] = exp(-(i * i) / (2 * sigma * sigma)); } double kernelSum = 0.0; for (int i = 0; i <= radius * 2; i++) { kernelSum += kernel[i]; } for (int i = 0; i <= radius * 2; i++) { kernel[i] /= kernelSum; } QVector<int> buffer; buffer.resize(width * height); for (int y = 0; y < height; y++) { const QRgb* src = reinterpret_cast<const QRgb*>(input.scanLine(y)); int* dst = &buffer[y * stride]; for (int x = 0; x < width; x++) { double sumR = 0.0; double sumG = 0.0; double sumB = 0.0; double sumA = 0.0; double sumWeight = 0.0; for (int i = -radius; i <= radius; i++) { if (x + i < 0 || x + i >= width) { continue; } const QRgb& pixel = src[x + i]; double intensity = qRed(pixel) * 0.299 + qGreen(pixel) * 0.587 + qBlue(pixel) * 0.114; if (intensity < threshold) { continue; } double weight = kernel[i + radius]; sumR += weight * qRed(pixel); sumG += weight * qGreen(pixel); sumB += weight * qBlue(pixel); sumA += weight * qAlpha(pixel); sumWeight += weight; } if (sumWeight > 0.0) { double alpha = qAlpha(src[x]); double newR = qBound(0.0, (1.0 - amount) * qRed(src[x]) + amount * (sumR / sumWeight), 255.0); double newG = qBound(0.0, (1.0 - amount) * qGreen(src[x]) + amount * (sumG / sumWeight), 255.0); double newB = qBound(0.0, (1.0 - amount) * qBlue(src[x]) + amount * (sumB / sumWeight), 255.0); double newA = qBound(0.0, (1.0 - amount) * alpha + amount * (sumA / sumWeight), 255.0); dst[x] = qRgba(static_cast<int>(newR), static_cast<int>(newG), static_cast<int>(newB), static_cast<int>(newA)); } else { dst[x] = src[x]; } } } return QImage(reinterpret_cast<const uchar*>(buffer.constData()), width, height, stride * 4, QImage::Format_RGB32); } ``` 其中,sigma参数控制卷积核的大小,threshold参数用于控制图像的锐化程度,amount参数用于控制锐化强度。您可以根据具体需求调整这些参数来实现您想要的效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值