In Qt, we sometimes need to change the color of the Qimage class to grayscale, at first I was thinking of using the Qimage member function Converttoformat(), but tried a lot of parameters, the return to the conversion of the picture is a problem, not what we want gray-scale image, If you have successfully converted a grayscale image using the member function, please write your method in the comment area and let the blogger learn. Then there is a stupid way, is to manually r,g,b the value of the average, and then use the average to update each pixel value, you can also get a grayscale image. See the code below:
// image is the input color image, IMG is output grey image qimage img (image); for (int0; i < img.width (); + +i) {for (int0; j < Img.height (); + +J) {int gray = Qgray (Img.pixel (i, J)); Img.setpixel (i, J, Qcolor (Gray, Gray, gray). RGB ());} }
Qimage Color Convert to Gray to grayscale