qt生成一幅纯马赛克图像

发布于:2024-08-20 ⋅ 阅读:(171) ⋅ 点赞:(0)

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

方法一:未优化方法

1、代码:

#include <QImage>
#include <QDebug>
#include <QElapsedTimer>

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()
{
    QElapsedTimer elapsed_timer; elapsed_timer.start(); 
    QImage mosicImage = generateMosaic(1280,960,40);
    qDebug() << "Used" << elapsed_timer.elapsed() << "milliseconds.";
    mosicImage.save("mosic.jpg");
    return 0;
}

2、效果:

运行结果

newWidth =  1280 , newHeight =  960
Used 19 milliseconds.

方法二:优化后方法

更优化的方法:

1、代码

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

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()
{
    QElapsedTimer elapsed_timer; elapsed_timer.start(); 
    QImage mosicImage = generateMosaic(1280,960,QImage::Format_RGB888);
    qDebug() << "Used" << elapsed_timer.elapsed() << "milliseconds.";
    mosicImage.save("mosic.jpg");
    return 0;
}

2、效果

运行效果

Used 4 milliseconds.


网站公告

今日签到

点亮在社区的每一天
去签到