January 3, 2013

depecheSoul depecheSoul
Lab Rat
29 posts

Converting colored image to black-white image

 

Hello.

I am trying to make a simple program that converts colored images to black-white images.

So far I have made this.

  1. void ObradaSlike::convert_picture_to_bw()
  2. {
  3.     QImage image;
  4.     image.load(fileModel->fileInfo(listView->currentIndex()).absoluteFilePath());
  5.  
  6.     QSize sizeImage = image.size();
  7.     int width = sizeImage.width(), height = sizeImage.height();
  8.  
  9.     QRgb color;
  10.     int value;
  11.  
  12.     for (int f1=0; f1<width; f1++) {
  13.         for (int f2=0; f2<height; f2++) {
  14.             color = image.pixel(f1, f2);
  15.             image.setPixel(f1, f2, QColor((qRed(color) + qGreen(color) + qBlue(color))/3).rgb());
  16.         }
  17.     }
  18.     sceneGraphics->clear();
  19.     sceneGraphics->addPixmap(QPixmap::fromImage(image));
  20. }

I think the code should work, but there is a problem.

The problem with this code is that I always get blue-black images insted black-white images. Do you know how to fix this.

Thanks.

6 replies

January 3, 2013

mlong mlong
Robot Herder
1551 posts

Be aware that just averaging R, G, and B doesn’t lead to a very nice gray scale conversion. (You’re not doing that properly either, given the blue/black thing.) You should use QColor’s non-member qGray() [qt-project.org] function to give you the gray value you’re looking for.

You could replace lines 14 and 15 above with

  1.     color = image.pixel(f1,f2);
  2.     image.setPixel(f1,f2, qGray(color));

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

January 3, 2013

depecheSoul depecheSoul
Lab Rat
29 posts

I have tried what have you suggested mlong, but I am still getting blue-black images.

Thanks.

January 3, 2013

depecheSoul depecheSoul
Lab Rat
29 posts

Here is the solution:

  1. int gray = (qRed(color) + qGreen(color) + qBlue(color))/3;
  2. image.setPixel(f1, f2, qRgb(gray, gray, gray));

or

  1. int gray = qGray(color);
  2. image.setPixel(f1, f2, qRgb(gray, gray, gray));

January 3, 2013

mlong mlong
Robot Herder
1551 posts

Ah, of course. I was thinking that qGray() returned a QRgb value. (I directed you to the docs, but didn’t read them, myself, apparently.)

Glad you got it working.

Be sure and edit the original post to add [Solved] to the title. Thanks!

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

February 27, 2013

Chesterton WORD Chesterton WORD
Lab Rat
2 posts

Before you work out your own code, why don’t you see some others” template code or image processing software”:http://www.rasteredge.com/dotnet-imaging/image-convert/.

February 27, 2013

Andre Andre
Robot Herder
6399 posts
Chesterton WORD wrote:
Before you work out your own code, why don’t you see some others” template code or image processing software”:http://www.rasteredge.com/dotnet-imaging/image-convert/.

How does one integrate this .net application with a cross-platform Qt application exactly?

 
  ‹‹ Limit Drag And Drop Vertically      Set LineWith using Qt5 on Windows with OpenGL ››

You must log in to post a reply. Not a member yet? Register here!