qt生成一幅纯马赛克图像

由于项目需要,需生成一幅纯马赛克的图像作为背景,经过多次测试成功,记录下来。

方法一:未优化方法

1、代码:

#include <QImage>
#include <QDebug>

QImage generateMosaic(int width, int height, int blockSize) {
    QImage image(width, height, QImage::Format_RGB888);
    if (blockSize <= 0) {
        return QImage(); // 返回空图片或处理错误
    }

    // 确保blockSize是偶数,并且不会使图像尺寸变得太小
    blockSize = (blockSize % 2 == 0) ? blockSize : blockSize + 1;
    if (image.width() < blockSize || image.height() < blockSize) {
        return image; // 如果blockSize太大,直接返回原图
    }

    // 计算新图片的尺寸
    int newWidth = image.width() / blockSize * blockSize;
    int newHeight = image.height() / blockSize * blockSize;
    qDebug() << "newWidth = " << newWidth << ", newHeight = " << newHeight;

    //QImage newImage(newWidth, newHeight, image.format());

    // 遍历每个块
    for (int x = 0; x < newWidth; x += blockSize) {
        for (int y = 0; y < newHeight; y += blockSize) {
            // 计算块的平均颜色
            QColor averageColor = QColor(0, 0, 0); // 初始化平均颜色为黑色
            // 用平均颜色填充整个块
            if((y / blockSize) % 2 == 0) {
                if((x/blockSize) % 2 == 0) {
                    averageColor = QColor(60,60,60);
                } else {
                    averageColor = QColor(150,150,150);
                }
            } else {
                if((x/blockSize) % 2 == 0) {
                    averageColor = QColor(150,150,150);
                } else {
                    averageColor = QColor(60,60,60);
                }
            }
            for  (int bx = 0; bx < blockSize && x + bx < newWidth; ++bx) {
                for ( int by = 0; by < blockSize && y + by < newHeight; ++by) {
                    image.setPixel(x+bx, y+by, qRgb(averageColor.red(), averageColor.blue(), averageColor.green()));
                }
            }
        }
    }
    return image;
}

int main()
{
    QImage mosicImage = generateMosaic(1280,960,40);
    mosicImage.save("mosic.jpg");
    return 0;
}

2、效果:

方法二:优化后方法

更优化的方法:

1、代码

#include <QImage>
#include <QDebug>
#include <QPainter>

QImage generateMosaic(int width, int height, QImage::Format format)
{
    QColor color_a(102, 102, 102); QColor color_b(128, 128, 128);
    QImage empty_image(width, height, format);
    empty_image.fill(color_a);

    QPainter empty_painter(&empty_image);
    int stride = 32;
    for (int i=0; i<empty_image.width(); i+=stride) {
        for (int j=0; j<empty_image.height(); j+=stride) {
            if ((i+j) % (2*stride) == 0) {
                empty_painter.fillRect(QRect(i, j, stride, stride), color_b);
            } else {
                empty_painter.fillRect(QRect(i, j, stride, stride), color_a);
            }
        }
    }
    return empty_image;
}


int main()
{
    QImage mosicImage = generateMosaic(1280,960,QImage::Format_RGB888);
    mosicImage.save("mosic.jpg");
    return 0;
}

2、效果

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt中,可以使用QCustomPlot库来绘制FFT图像。QCustomPlot是一个基于Qt的开源绘图库,它提供了丰富的绘图功能,包括曲线、柱状图、散点图等。 以下是一个简单的示例代码,演示如何使用QCustomPlot绘制FFT图像: ``` #include <QApplication> #include "qcustomplot.h" #include <cmath> int main(int argc, char *argv[]) { QApplication a(argc, argv); // 创建QCustomPlot对象 QCustomPlot *customPlot = new QCustomPlot(); // 生成FFT数据 int N = 256; // FFT大小 QVector<double> x(N), y(N); for (int i=0; i<N; ++i) { x[i] = i; y[i] = qSin(2*M_PI*i/N) + qSin(4*M_PI*i/N) + qSin(6*M_PI*i/N); } // 创建FFT对象 QCPDataMap data; for (int i=0; i<N; ++i) { double re = 0, im = 0; for (int j=0; j<N; ++j) { double angle = 2*M_PI*i*j/N; re += y[j]*qCos(angle); im -= y[j]*qSin(angle); } data.insert(i, QCPData(i, sqrt(re*re + im*im))); } QCPGraph *graph = customPlot->addGraph(); graph->setData(data); // 设置坐标轴 customPlot->xAxis->setLabel("Frequency"); customPlot->yAxis->setLabel("Amplitude"); customPlot->xAxis->setRange(0, N/2); customPlot->yAxis->setRange(0, 3); // 显示图像 customPlot->replot(); customPlot->show(); return a.exec(); } ``` 在上面的示例代码中,我们首先创建了一个QCustomPlot对象,然后生成了FFT数据,接着创建了一个QCPDataMap对象,用于存储FFT数据,并将其添加到QCustomPlot中的一个QCPGraph对象中。最后,我们设置了坐标轴范围和标签,并显示了图像。 当我们运行这个程序时,就可以看到生成的FFT图像了。如果需要进一步的自定义,可以通过QCustomPlot的各种函数来实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值