How to make a mask with QImage?
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.
Code:
bool tt = image.load(fileName, 0);
qDebug() << "Bool:" << tt;
int i =image.size().width();
int j =image.size().height();
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++)
{
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");
Re: How to make a mask with QImage?
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 ?
Re: How to make a mask with QImage?
Re: How to make a mask with QImage?
Quote:
Originally Posted by
koan
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.
Re: How to make a mask with QImage?
Quote:
Originally Posted by
ChrisW67
I also have try this way, but it does not wrok
Code:
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);
Re: How to make a mask with QImage?
This works fine for me:
Code:
#include <QtGui>
int main(int argc, char *argv[])
{
QImage ConvertImage
(image.
width(), image.
height(),
QImage::Format_RGB32);
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++) {
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");
l.
setPixmap(QPixmap::fromImage(image
));
l.setMask(b);
l.show();
return app.exec();
}
Re: How to make a mask with QImage?
Yes, used the label the mask works fine, but with button, it doesnot work, How strange!