簡體   English   中英

無法通過將 RBG 像素與數字相乘來獲得正確的灰度?

[英]Can't get greyscaling right by multiplying RBG pixels with numbers?

我真的經歷了我能找到的一切,但圖像處理仍然很混亂。 作為最簡單的任務,我想要灰度 4 個像素。 使用我發現的兩組數字中的任何一組輸出都不是灰色的問題。 感謝您的幫助!

我現在的算法(遺憾的是我不能只使用這個 cimg 東西的 opencv):

CImg<unsigned char> img1("/content/gdrive/My Drive/four.ppm");

int width = img1.width();
int height = img1.height();

int init;
unsigned char oldRed, oldGreen, oldBlue, newRed, newGreen, newBlue;
int index = 0;

for(int i = 0; i < width * height; i++){
    init = index;

    oldRed = img1.data()[index];
    index++;
    oldGreen = img1.data()[index];
    index++;
    oldBlue = img1.data()[index];

    newRed  = (oldRed * 0.21) + (oldGreen * 0.72) + (oldBlue * 0.07);
    newGreen = (oldRed * 0.21) + (oldGreen * 0.72) + (oldBlue * 0.07);
    newBlue = (oldRed * 0.21) + (oldGreen * 0.72) + (oldBlue * 0.07);

    index = init;
    img1.data()[index] = newRed;
    index++;
    img1.data()[index] = newGreen;
    index++;
    img1.data()[index] = newBlue;
    index++;
}

img1.save("out.ppm");

四.ppm:

P3
2 2
255

0 255 255
255 0 255
255 255 0
0 0 255

輸入:

在此處輸入圖片說明

輸出(應為灰色):

在此處輸入圖片說明

您錯誤地假設像素存儲在 R1G1B1R2G2B2R3G3B3 中。 在 CImg 中,像素存儲為 R1R2R3....G1G2G3...B1B2B3....參見文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM