Grayscale image Color Conversion
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
Code:
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
Re: Grayscale image Color Conversion
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
Code:
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!
Re: Grayscale image Color Conversion
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
Re: Grayscale image Color Conversion
can you show the whole code you are using?
Re: Grayscale image Color Conversion
The Inversion Color of image
Loading Image function
Code:
dialog2.setNameFilter(tr("Images (*.png *.xpm *.jpg *.bmp)"));
if(dialog2.exec())
{
imageFileName2 = dialog2.selectedFiles().first();
}
primaryImage.load(imageFileName1);
primaryImage.invertPixels();
In paint Function:
Code:
painter.
drawImage(QPoint(0,
0), secondaryImage
);
The Changing color version of program ie(commented out until solution found)
Code:
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
Re: Grayscale image Color Conversion
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.
Code:
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.
Code:
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
Code:
if ( secondaryImage.pixel( x, y ) == Qt::black )
to
Code:
if ( secondaryImage.pixel( x, y ) == QRgb( #000000 ) // black
Re: Grayscale image Color Conversion
here is an example which shows you how to convert color mode. 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