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");
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");