[Qt-interest] Enhanced monochrome conversion
M. Bashir Al-Noimi
mbnoimi at gmx.com
Thu Nov 26 12:58:53 CET 2009
Andre Somers wrote:
> M. Bashir Al-Noimi wrote:
>
>> 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?*
>>
>> *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;
>> }
>>
> Hi, interesting problem, but I'd say there is a relatively straitforward
> way to get an answer: read the GIMP sourcecode and see how it does this!
> Of course, I have no idea if the source of the GIMP is actually
> readable, I never tried.
>
> André
> (use the Force, read the Source!)
>
>
This is the hard way for solving the problem!!!!
Any way, I asked same question in the forum and I got a response from
guy explained how GIMP deal with this problem (read the quote down here):
> It either uses 127 as the threshold or if it is smarter, it first
> calculates an average or median of lightness (which is an equivalent
> of colors->levels->auto) (aka value in HSV) and uses that. Try doing
> colors->levels->auto on your image and then convert it to monochrome
> using 127 as the threshold. If it works you can repeat the same
> process in your application.
so directly I tried to test an image in GIMP then I discovered that GIMP
behaves this way exactly, so I coded the following but it didn't give me
a correct result just like GIMP (threshold is 209 where it must around 165):
QImage result = pImage;
QList<int> pixelsValues;
for(int x=0; x<pImage.width();x++)
{
for(int y=0; y<pImage.height();y++)
{
QRgb rgb = pImage.pixel(x,y);
QColor color(rgb);
pixelsValues<<color.lightness();
}
}
qSort(pixelsValues);
int median;
int count = pixelsValues.count();
if(count%2==0)
median = (pixelsValues[count/2]+pixelsValues[(count/2)+1]) / 2;
else
median = pixelsValues[count/2];
int threshold = median;
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));
qDebug("threshold = %d", threshold);
// result.save("c:/res.jpg");
return result;
The resultant image:
Do you've any idea how I can fix this issue?
-----
Best Regards
Muhammad Bashir Al-Noimi
My Blog: http://mbnoimi.net/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091126/25907364/attachment.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: res.png
Type: image/png
Size: 32915 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091126/25907364/attachment.png
More information about the Qt-interest-old
mailing list