View Full Version : Grayscale image Color Conversion
2lights
12th August 2013, 09:45
painting a grayscale image into label
How do i use
QImage convertToFormat
& set color
I need to make whatever is black in the image and make it transparent
& whatever is white or partially white and make another color
while(x < secondaryImage.height()){
while(y < secondaryImage.width()){
if (secondaryImage.pixel(x,y) == 0)
secondaryImage.setPixel(x, y, Qt::red);
y++;
}
x++;
}
Thats what i got so far from other post
But its not working
If someone could please show me an example of how to go about doing it
or direct me accordingly
Kind Regards
alainstgt
12th August 2013, 11:06
I am not sure wether I understood your request the right way. I am assuming that you want to convert all black pixels to full transparency and all other pixels to red.
The first point is your loop which is not correctly initialized. Even if you initialized x and y outside the loop, latest when scanning the second column your variable y will be out of range, so the inner loop will be performed only once.
Your loop should look like
for ( x = 0; x < secondaryImage.width(); ++x )
{
for ( y = 0; y < secondaryImage.height(); ++y )
{
if ( secondaryImage.pixel( x, y ) == Qt::black )
secondaryImage.setPixel( x, y, QColor( 0, 0, 0, 0 )); // fully transparency, color doesn´t matter
else
secondaryImage.setPixel( x, y, Qt::red );
}
}
I did not test the code!
2lights
12th August 2013, 12:57
Loop: Thanks that sorted the "out of scope problem"
Though the image editing.... Nothing happens
Any Ideas is there another way of changing colour
For the time being i'm inverting pixels of whole image until I get a more graphic appealing solution
alainstgt
12th August 2013, 14:12
can you show the whole code you are using?
2lights
12th August 2013, 14:25
The Inversion Color of image
Loading Image function
QFileDialog dialog2(this, "Upload secondary");
dialog2.setNameFilter(tr("Images (*.png *.xpm *.jpg *.bmp)"));
dialog2.setViewMode(QFileDialog::Detail);
if(dialog2.exec())
{
imageFileName2 = dialog2.selectedFiles().first();
}
primaryImage.load(imageFileName1);
primaryImage.invertPixels();
In paint Function:
painter.drawImage(QPoint(0, 0), secondaryImage);
The Changing color version of program ie(commented out until solution found)
for (int x = 0; x < secondaryImage.width(); ++x)
{
for (int y = 0; y < secondaryImage.height(); ++y)
{
if ( secondaryImage.pixel(x, y) == Qt::black)
secondaryImage.setPixel(x, y, QColor( 0, 0, 0, 0 )); //This line gives compiler error :confused:
else
secondaryImage.setPixel( x, y, Qt::red ); //Nothing happens here :(
}
}
Ideas most welcome
alainstgt
12th August 2013, 15:26
sorry, my code is not correct. As the documentation stipulates, the last argument of setPixel() should be an uint!
Try wether next code do what you want.
QRgba transparentPixel, redPixel;
transparentPixel = QRgba( 0, 0, 0, 0 ); // fully transparency, color doesn´t matter
redPixel = QRgba( 255, 0, 0, 255 ); // red, no transparency
for ( x = 0; x < secondaryImage.width(); ++x )
{
for ( y = 0; y < secondaryImage.height(); ++y )
{
if ( secondaryImage.pixel( x, y ) == Qt::black ) // check wether working right!
secondaryImage.setPixel( x, y, transparentPixel);
else
secondaryImage.setPixel( x, y, redPixel );
}
}
sorry, my code is not correct. As the documentation stipulates, the last argument of setPixel() should be an uint!
Try wether next code do what you want.
QRgba transparentPixel, redPixel;
transparentPixel = QRgba( 0, 0, 0, 0 ); // fully transparency, color doesn´t matter
redPixel = QRgba( 255, 0, 0, 255 ); // red, no transparency
for ( x = 0; x < secondaryImage.width(); ++x )
{
for ( y = 0; y < secondaryImage.height(); ++y )
{
if ( secondaryImage.pixel( x, y ) == Qt::black ) // check wether working right!
secondaryImage.setPixel( x, y, transparentPixel);
else
secondaryImage.setPixel( x, y, redPixel );
}
}
Added after 27 minutes:
change following line
if ( secondaryImage.pixel( x, y ) == Qt::black )
to
if ( secondaryImage.pixel( x, y ) == QRgb( #000000 ) // black
getlink
10th February 2014, 06:32
here is an example which shows you how to convert color mode (http://www.rasteredge.com/dotnet-imaging/image-processing-mode-conversion/). i t helps you convert between white and black,RGB and other image formats. and the above pixel color changing make sense as well.
hope i am being helpful.
best luck to you
Lily
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.