gpt4 book ai didi

c++ - 如果原始像素在新图像中为黑色,则 Qt 绘制蓝色像素

转载 作者:行者123 更新时间:2023-11-28 03:40:49 29 4
gpt4 key购买 nike

我已经编写了一些代码来生成新图像。我的背景图像有黑色区域,当 for 循环出现在黑色像素上时,它应该在新图像中绘制蓝色图像,否则它应该只绘制原始像素。我以为我可以这样做,但程序一直在运行。

   QApplication a(argc, argv);
int c, m, y, k, al;
QColor color;
QColor drawColor;
QImage background;
QImage world(1500, 768, QImage::Format_RGB32);
QSize sizeImage;
int height, width;
background.load("Background.jpg");
world.fill(1);
QPainter painter(&background);
sizeImage = background.size();
width = sizeImage.width();
height = sizeImage.height();

for(int i = 0; i < height; i++)
{
for(int z = 0; z < width; z++)
{
color = QColor::fromRgb (background.pixel(i,z) );
color.getCmyk(&c,&m,&y,&k,&al);

if(c == 0 && m == 0 && y == 0 && k == 0) //then we have black as color and then we draw the color blue
{
drawColor.setBlue(255);
painter.setPen(drawColor);
painter.drawPoint(i,z);
}
}

}


//adding new image to the graphicsScene
QGraphicsPixmapItem item( QPixmap::fromImage(background));
QGraphicsScene* scene = new QGraphicsScene;
scene->addItem(&item);

QGraphicsView view(scene);
view.show();

是我的for循环错了还是我画的?它说 QImage::pixel: coordinate (292,981) out of range 但是对于这么多像素,它也不够快。

最佳答案

如评论中所述,一个一个地绘制像素可能会非常慢。甚至逐像素访问也可以是quite slow .例如。以下可能更快,但仍然不是很好:

  const QRgb black = 0;
const QRgb blue = 255;
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
if (background.pixel(x,y) == black) {
background.SetPixel(blue);
}
}
}

更快的解决方案涉及通过 scanline() 进行直接位操作。您可能想先调用 convertToFormat(),这样您就不需要处理不同的可能扫描线格式。

作为创意技巧,调用 createMaskFromColor 使所有黑色像素透明,然后在蓝色背景上绘制。

关于c++ - 如果原始像素在新图像中为黑色,则 Qt 绘制蓝色像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9311738/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com