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.
Qt Code:
  1. QImage image;
  2. bool tt = image.load(fileName, 0);
  3. qDebug() << "Bool:" << tt;
  4. QColor white(Qt::white);
  5. QColor black(Qt::black);
  6.  
  7. int i =image.size().width();
  8. int j =image.size().height();
  9.  
  10. QImage ConvertImage (i, j, QImage::Format_RGB32);
  11. QColor maskColor = QColor::fromRgb (image.pixel(1, 1) );
  12.  
  13. int red = maskColor.red();
  14. int green = maskColor.green();
  15. int blue = maskColor.blue();
  16.  
  17. for(int x= 0; x<i; x++)
  18. {
  19. for(int y = 0; y<j; y++)
  20. {
  21. QColor color = QColor::fromRgb (image.pixel(x, y) );
  22.  
  23. if((abs(red - color.red()) +
  24. abs(green - color.green()) +
  25. abs(blue - color.blue()))/3 <10 )
  26. {
  27. ConvertImage.setPixel(x, y, white.rgb()) ;
  28. }
  29. else
  30. {
  31. ConvertImage.setPixel(x, y, black.rgb()) ;
  32. }
  33. }
  34. }
  35. QImage tmp = ConvertImage.convertToFormat(QImage::Format_Mono );
  36. tmp.save("test", "png");
To copy to clipboard, switch view to plain text mode