M. Bashir
26th November 2009, 08:02
Hi All,
It's easy to convert any image to monochrome format by using QImage::convertToFormat like this:
pImage.convertToFormat(QImage::Format_Mono, Qt::MonoOnly|Qt::ThresholdDither|Qt::AvoidDither);
but convertToFormat always guessing that the value of conversion's threshold is 255 where in some cases we need to use another threshold's value. I checked out GIMP and I noticed that it uses an automatic method for guessing threshold's value (see the example below) so I'm wondering:
How I can convert images to monochrome format by using auto threshold's value just like GIMP conversion method?
Original
http://img134.imageshack.us/img134/9741/77363363.th.jpg (http://img134.imageshack.us/i/77363363.jpg/)
QImage conversion
http://img145.imageshack.us/img145/5563/43162085.th.jpg (http://img145.imageshack.us/i/43162085.jpg/)
Automatic GIMP conversion
http://img402.imageshack.us/img402/7148/20634127.th.jpg (http://img402.imageshack.us/i/20634127.jpg/)
P.S
I've wrote a stupid function for converting images to monochrome by using a specified threshold's value as following but I need to know how I can automatically specify threshold's value:
QImage getThresholdBW(QImage pImage, int threshold)
{
QImage result = pImage;
result.fill(255);
for(int x=0; x<pImage.width(); x++)
for(int y=0; y<pImage.height(); y++)
result.setPixel(x, y, qGray(pImage.pixel(x, y))>threshold?qRgb(255, 255, 255):qRgb(0, 0, 0));
return result;
}
It's easy to convert any image to monochrome format by using QImage::convertToFormat like this:
pImage.convertToFormat(QImage::Format_Mono, Qt::MonoOnly|Qt::ThresholdDither|Qt::AvoidDither);
but convertToFormat always guessing that the value of conversion's threshold is 255 where in some cases we need to use another threshold's value. I checked out GIMP and I noticed that it uses an automatic method for guessing threshold's value (see the example below) so I'm wondering:
How I can convert images to monochrome format by using auto threshold's value just like GIMP conversion method?
Original
http://img134.imageshack.us/img134/9741/77363363.th.jpg (http://img134.imageshack.us/i/77363363.jpg/)
QImage conversion
http://img145.imageshack.us/img145/5563/43162085.th.jpg (http://img145.imageshack.us/i/43162085.jpg/)
Automatic GIMP conversion
http://img402.imageshack.us/img402/7148/20634127.th.jpg (http://img402.imageshack.us/i/20634127.jpg/)
P.S
I've wrote a stupid function for converting images to monochrome by using a specified threshold's value as following but I need to know how I can automatically specify threshold's value:
QImage getThresholdBW(QImage pImage, int threshold)
{
QImage result = pImage;
result.fill(255);
for(int x=0; x<pImage.width(); x++)
for(int y=0; y<pImage.height(); y++)
result.setPixel(x, y, qGray(pImage.pixel(x, y))>threshold?qRgb(255, 255, 255):qRgb(0, 0, 0));
return result;
}