簡體   English   中英

我正在從BMP文件讀取像素的RGB值,但沒有獲得正確的值

[英]I'm reading the RGB values of pixels from a BMP file and not getting the correct values

我按照此鏈接上的代碼讀取bmp文件中的像素值,從而能夠讀取像素的RGB值,當我將整個圖像作為一種顏色並讀取隨機像素的值時,它們是正確的。 在此之后,我嘗試制作該函數,以便該函數還將嘗試查找有多少種獨特的顏色,因此我在圖像中添加了一個具有不同顏色的框,但該函數仍然只能找到一種顏色。 我想知道是否也許我不以某種方式查看BMP中包含的所有字節,但是我不確定如何嘗試,因為我是新手。

為了確保代碼沒有找到不同的彩色像素,但是沒有將它們添加到唯一像素列表中,我嘗試在發現一種不同於始終找到的顏色但沒有任何輸出的顏色時打印輸出。

struct Color {
int R = -1;
int G = -1;
int B = -1;
};

unsigned char* readBMP(char* filename) {
int i;
FILE* f = fopen(filename, "rb");
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f);

int width = *(int*)&info[18]; //the reason *(int*) is used here because there's an integer stored at 18 in the array that indicates how wide the BMP is
int height = *(int*)&info[22]; // same reasoning for *(int*)

int size = 3 * width * height;
unsigned char* data = new unsigned char[size];
fread(data, sizeof(unsigned char), size, f);
fclose(f);

// windows has BMP saved as BGR tuples and this switches it to RGB
for(i = 0; i < size; i += 3){
    unsigned char tmp = data[i];
    data[i] = data[i+2];
    data[i+2] = tmp;
}

i = 0; // i is the x value of the pixel that is having its RGB values checked
int j = 0; // j is the y value of the pixel that is having its RGB values checked
unsigned char R = data[3 * (i * width + j)]; // value of R of the pixel at (i,j)
unsigned char G = data[3 * (i * width + j) + 1]; // value of G of the pixel at (i,j)
unsigned char B = data[3 * (i * width + j) + 2]; // value of B of the pixel at (i,j)

std::cout << "value of R is " << int(R);
std::cout << " value of G is " << int(G);
std::cout << " value of B is " << int(B);

Color num_colors[5];
int count;
int z;
int flag;
int iterator;
int sum;
for(count = 0; count < size; count += 1){
    unsigned char R = data[3 * (i * width + j)];
    unsigned char G = data[3 * (i * width + j) + 1];
    unsigned char B = data[3 * (i * width + j) + 2];
    sum = int(R) + int(G) + int(B);
    if(sum != 301) {// 301 is the sum of the RGB values of the color that the program does manage to find
        std::cout << sum;
    }
    flag = 0;
    for(z = 0; z < 5; z += 1){
        if(num_colors[z].R == R && num_colors[z].G == G && num_colors[z].B == B){
            flag = 1;
        }
    }
    if(flag == 1){
        continue;
    }
    iterator = 0;
    while(num_colors[iterator].R != -1){
        iterator += 1;
    }
    num_colors[iterator].R = R;
    num_colors[iterator].G = G;
    num_colors[iterator].B = B;
}

int number = 0;
for(int r = 0; r < 5; r += 1){
    std::cout << "\nValue of R here: " << num_colors[r].R;
    if(num_colors[r].R != -1){
        number += 1;
    }
}
std::cout << "\nNumber of colors in image: " << number;
return data;
}

https://imgur.com/a/dXllIWL這是我正在使用的圖片,因此應該找到兩種顏色,但是代碼只能找到紅色像素。

您的問題是您始終檢查(0,0)處的RGB值

i = 0; // i is the x value of the pixel that is having its RGB values checked
int j = 0; // j is the y value of the pixel that is having its RGB values checked
...
for(count = 0; count < size; count += 1){
    unsigned char R = data[3 * (i * width + j)];
    unsigned char G = data[3 * (i * width + j) + 1];
    unsigned char B = data[3 * (i * width + j) + 2];

ij定義了要檢查的像素的X和Y位置,但請注意,您永遠不要在循環中更改像素位置。 您的循環將一遍又一遍地執行相同的操作。 您可能想要的是一個雙循環,遍歷圖像中的所有坐標:

   for(int y=0; y<height; y++)
       for(int x=0; x<width; x++){
          unsigned char R = data[3 * (y * width + x) + 0];
          unsigned char G = data[3 * (y * width + x) + 1];
          unsigned char B = data[3 * (y * width + x) + 2];

暫無
暫無

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

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