PDA

View Full Version : How to make a mask with QImage?



MeteorAndSusan
9th August 2010, 10:57
I want to make a mask image with QImage, I suppose the color of pixel (1, 1) is the brackground 's color and set that color to white, if the pixel's color(rgb) have big difference with the blackground's color then set the color to black.That's my code,but it doesn't work, when i set the mask, the picture doesnot show.


QImage image;
bool tt = image.load(fileName, 0);
qDebug() << "Bool:" << tt;
QColor white(Qt::white);
QColor black(Qt::black);

int i =image.size().width();
int j =image.size().height();

QImage ConvertImage (i, j, QImage::Format_RGB32);
QColor maskColor = QColor::fromRgb (image.pixel(1, 1) );

int red = maskColor.red();
int green = maskColor.green();
int blue = maskColor.blue();

for(int x= 0; x<i; x++)
{
for(int y = 0; y<j; y++)
{
QColor color = QColor::fromRgb (image.pixel(x, y) );

if((abs(red - color.red()) +
abs(green - color.green()) +
abs(blue - color.blue()))/3 <10 )
{
ConvertImage.setPixel(x, y, white.rgb()) ;
}
else
{
ConvertImage.setPixel(x, y, black.rgb()) ;
}
}
}
QImage tmp = ConvertImage.convertToFormat(QImage::Format_Mono );
tmp.save("test", "png");

koan
10th August 2010, 00:11
PNG does not support bilevel (1 bit per pixel) images, but you are converting to a bilevel image before saving.

Try ConvertImage.save("test.png","png"); instead of the last 2 lines ?

ChrisW67
10th August 2010, 01:22
Is QBitmap and QBitmap::fromImage() useful?

MeteorAndSusan
10th August 2010, 04:45
PNG does not support bilevel (1 bit per pixel) images, but you are converting to a bilevel image before saving.

Try ConvertImage.save("test.png","png"); instead of the last 2 lines ?
Actually if i change the format ,for example, bmp, it also doesn't work.

MeteorAndSusan
10th August 2010, 04:50
Is QBitmap and QBitmap::fromImage() useful?
I also have try this way, but it does not wrok


bitImage = QBitmap::fromImage(ConvertImage);
qDebug()<<bitImage.isQBitmap(); //print true
ui.toolButton_2->setIcon(QPixmap::fromImage(image));
ui.toolButton_2->setIconSize(QSize(32, 32));
ui.toolButton_2->setMask( bitImage );
ui.toolButton_2->setFixedSize(32, 32);

ChrisW67
10th August 2010, 09:37
This works fine for me:

#include <QtGui>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QImage image("source.png");

QColor white(Qt::white);
QColor black(Qt::black);

QImage ConvertImage (image.width(), image.height(), QImage::Format_RGB32);
QColor maskColor = QColor::fromRgb (image.pixel(1, 1) );

int red = maskColor.red();
int green = maskColor.green();
int blue = maskColor.blue();

for(int x= 0; x<image.width(); x++) {
for(int y = 0; y<image.height(); y++) {
QColor color = QColor::fromRgb (image.pixel(x, y) );

if((abs(red - color.red()) +
abs(green - color.green()) +
abs(blue - color.blue()))/3 <10 ) {
ConvertImage.setPixel(x, y, white.rgb()) ;
}
else {
ConvertImage.setPixel(x, y, black.rgb()) ;
}
}
}
ConvertImage.save("mask.png", "png");

QBitmap b = QBitmap::fromImage(ConvertImage, Qt::MonoOnly);

QLabel l;
l.setPixmap(QPixmap::fromImage(image));
l.setMask(b);
l.show();
return app.exec();
}

MeteorAndSusan
10th August 2010, 11:06
Yes, used the label the mask works fine, but with button, it doesnot work, How strange!