問題描述
我有一個 QImage,我需要將它轉換為灰度,然后再用顏色繪制它.我發現了一個 allGray()
和 isGrayScale()
函數來檢查圖像是否已經是灰度的,但沒有 toGrayScale()
或類似名稱功能.
I have a QImage and I need to convert it to grayscale, then later paint over that with colors. I found an allGray()
and isGrayScale()
function to check if an image is already grayscale, but no toGrayScale()
or similarly-named function.
現在我正在使用這段代碼,但它的性能不是很好:
Right now I'm using this code, but it's does not have a very good performance:
for (int ii = 0; ii < image.width(); ii++) {
for (int jj = 0; jj < image.height(); jj++) {
int gray = qGray(image.pixel(ii, jj));
image.setPixel(ii, jj, QColor(gray, gray, gray).rgb());
}
}
在性能方面,將 QImage 轉換為灰度的最佳方法是什么?
What would be the best way, performance-wise, to convert a QImage to grayscale?
推薦答案
從 Qt 5.5 開始,您可以調用 QImage::convertToFormat() 將 QImage 轉換為灰度,如下所示:
Since Qt 5.5, you can call QImage::convertToFormat() to convert a QImage to grayscale as follows:
QImage image = ...;
image.convertToFormat(QImage::Format_Grayscale8);
這篇關于將 QImage 轉換為灰度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!