【发布时间】:2012-02-16 21:45:20
【问题描述】:
我编写了一些代码来制作新图像。我的背景图像有黑色区域,当 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: 坐标 (292,981) 超出范围,但是对于这么多像素,它也不够快使用。
【问题讨论】:
-
“程序一直运行”是什么意思?它永远不会出现在 for 循环中?你没有得到你期望的结果?
-
仅仅是因为循环非常慢吗?在我看来,通过像这样读取和写入单个像素来转换大图像会有点滞后!
-
那有什么更好的方法呢?我想也许只重绘蓝色像素,但我不知道如何循环图像。
-
告诉我们问题出在哪里。它永远不会结束吗?它完成了,但它很慢吗?结果你错了吗? ....
-
.jpg 有多黑? JPEG 压缩优化图像大小以供人类消费。由于人类无法很好地区分黑色和深灰色,因此压缩可能会将黑色变为灰色。对于白色背景上的细黑线(