2012-11-26 31 views
1

我拍了一張位圖圖像,並從其中拉出了R,G,B的整數,用於指定的一行像素。將int轉換爲一個字符串,以便我可以打印我的6種特定顏色。我無法弄清楚如何用int來做到這一點。計數像素值bmp到像素值C++

問題 我能夠將行0-184(對應於該行中的像素)作爲順序數據1234 ...或顏色紅色,紅色,紅色,黑色,黑色,灰色......打印出來。

但是,我需要計算像/相同的顏色,顯示相似顏色的總和,並重置計數器,直到該顏色再次出現,然後重新計算。我認爲如果還是其他人會這樣做,但不是很完美。這可能是我的代碼結構導致了一個問題?

所以我的願望是:

5 red, 
10 black, 
2 red, 
1 gray, 

等......

這裏是我的代碼,我是初學者所以批評我的知識的缺乏,讓我可以好好學學。

#include <iostream> 
#include <sstream> 
#include <string> 
#include "EasyBMP.h" 
#include "EasyBMP_BMP.h" 
#include "EasyBMP_DataStructures.h" 
#include "EasyBMP_VariousBMPutilities.h" 

//Conversion and comparison function 
void calculate(int i, int x, int p); 

int main(int argc, const char * argv[]) 
{ 

BMP Image; 
Image.ReadFromFile("BMP GOES HERE 24bit"); 

std::cout << "Image Height and Width: " << Image.TellHeight() << " x " << Image.TellWidth() << std::endl; 

std::cout << "Enter your row: "; 

int pixX = 0; 
std::cin >> pixX; 

//Set getpixel to top of row 
int pixY = 0; 

for(pixY = 0; pixY < Image.TellHeight() ; pixY++) 
{ 
    std::cout << "Pixel: " << pixY + 1; 

    RGBApixel Temp = Image.GetPixel(pixX,pixY); 

    //Array to store pixel color ints 
    int pixy[3]; 
    pixy[0] = Temp.Red; 
    pixy[1] = Temp.Green; 
    pixy[2] = Temp.Blue; 

    calculate(pixy[0], pixy[1], pixy[2]); 
} 

return 0; 
} 


void calculate(int rnum, int gnum, int bnum) 
{ 

//String which will contain the result 
std::string result; 

//Stream used for the conversion 
std::ostringstream convert; 

//Insert the textual representation of 'Number' in the characters in the stream 
convert << rnum;     

convert << gnum; 

convert << bnum; 

// set 'Result' to the contents of the stream 
result = convert.str();  

// compare result to my given value 
if (result == "25500") 
{ 
    std::cout << " RED " << std::endl; 
} 
if (result == "255255255") 
{ 
    std::cout << " WHITE " << std::endl; 
} 
if (result == "000") 
{ 
    std::cout << " BLACK" << std::endl; 
} 
if (result == "148148148") 
{ 
    std::cout << " GRAY " << std::endl; 
} 
if (result == "267326") 
{ 
    std::cout << " GREEN " << std::endl; 
} 
if (result == "2551260") 
{ 
    std::cout << " ORANGE " << std::endl; 
} 
} 

以下是工作代碼。請注意,如果您使用它,我的圖片只有6種特定顏色。要更改打印輸出,必須根據需要修改switch語句。

#include <iostream> 
#include <vector> 
#include "EasyBMP.h"  
#include "EasyBMP_BMP.h" 
#include "EasyBMP_DataStructures.h" 
#include "EasyBMP_VariousBMPutilities.h" 

long toRGB(long red, long grn, long blu); 


int main(int argc, const char * argv[]) 
{ 

BMP Image; 
Image.ReadFromFile("Your BMP HERE"); 

std::cout << "Image Height and Width: " << Image.TellHeight() << " x " << Image.TellWidth() << std::endl; 

std::cout << "Enter your row: "; 

int pixX = 0; 
std::cin >> pixX; 
if (pixX != 0)        //Subtract one from input if not 0, image starts at 0,0 
{ 
    pixX -= 1; 
} 

long pop = 0; 
long pop1 = 0; 

RGBApixel current = Image.GetPixel(pixX,0); 

long pixy1[3];          //Array to store pixel color ints 
pixy1[0] = current.Red; 
pixy1[1] = current.Green; 
pixy1[2] = current.Blue; 

pop1 = toRGB(pixy1[0], pixy1[1], pixy1[2]); 


int count = 0; 
for(int pixY = 0; pixY < Image.TellHeight() ; pixY++) 
{ 
    RGBApixel Temp = Image.GetPixel(pixX,pixY); 

    long pixy[3];          //Array to store pixel color ints 
    pixy[0] = Temp.Red; 
    pixy[1] = Temp.Green; 
    pixy[2] = Temp.Blue; 

    pop = toRGB(pixy[0], pixy[1], pixy[2]); 

    if (pop == pop1) 
    { 
     count++; 
    } 
    else 
    { 
     switch (pop1) { 
      case 0: 
       std::cout << "(" << count << ")\t" << "BLACK\n" << std::endl; 
       break; 
      case 16711680: 
       std::cout << "(" << count << ")\t" << "RED\n" << std::endl; 
       break; 
      case 9737364: 
       std::cout << "(" << count << ")\t" << "GRAY\n" << std::endl; 
       break; 
      case 16777215: 
       std::cout << "(" << count << ")\t" << "WHITE\n" << std::endl; 
       break; 
      case 1722650: 
       std::cout << "(" << count << ")\t" << "GREEN\n" << std::endl; 
       break; 
      case 16743936: 
       std::cout << "(" << count << ")\t" << "ORANGE\n" << std::endl; 
       break; 
      default: 
       std::cout << " !!!NO Specified COLOR For!!! " << pop1 << std::endl; 
       break; 
     } 

     pop1 = pop;          //Reset the count and current  color 
     count = 1; 
    } 
} 
    if (count > 0)          //Returns last color and count 
    { 
     switch (pop1) { 
      case 0: 
       std::cout << "(" << count << ")\t" << "BLACK\n" << std::endl; 
       break; 
      case 16711680: 
       std::cout << "(" << count << ")\t" << "RED\n" << std::endl; 
       break; 
      case 9737364: 
       std::cout << "(" << count << ")\t" << "GRAY\n" << std::endl; 
       break; 
      case 16777215: 
       std::cout << "(" << count << ")\t" << "WHITE\n" << std::endl; 
       break; 
      case 1722650: 
       std::cout << "(" << count << ")\t" << "GREEN\n" << std::endl; 
       break; 
      case 16743936: 
       std::cout << "(" << count << ")\t" << "ORANGE\n" << std::endl; 
       break; 
      default: 
       std::cout << " !!!NO Specified COLOR For!!! " << pop1 << std::endl; 
       break; 
    } 
} 

return 0; 
} 

long toRGB(long a, long b, long c)       //Function to convert R, G, B  values to unique value 
{ 
long color = 0; 
color |= (a & 255) << 16; 
color |= (b & 255) << 8; 
color |= (c & 255); 

return color; 
} 
+3

這只是打印出顏色的代碼?您是否有任何代碼用於您提及的其他事物(計數,總和等)? – FoolishSeth

回答

0

我之前並沒有真正理解你的問題,所以我寫了一個更適合你的問題的新答案。

我認爲你可以得到你想要的東西像這樣的東西(修改代碼):

RGBApixel current = Image.GetPixel(pixX,0); 
int count = 0; 
for(pixY = 0; pixY < Image.TellHeight() ; pixY++) 
{ 
    RGBApixel Temp = Image.GetPixel(pixX,pixY); 
    if (Temp == current) 
    { 
     count++; 
    } 
    else 
    { 
     // same-color sequence ended 
     // Add code here to print out current color and count 
     --- your output code ---- 

     // now reset the count and current color 
     current = Temp; 
     count = 1; 
    } 
} 

// Now just print out the last sequence 
if (count > 0) 
{ 
    --- your output code here again --- 
}  

有一件事我不知道是怎麼回事,如果在所有的==經營者爲RGBApixel定義。如果它沒有被定義,或者根據它們的顏色看起來並不相同,只需編寫一個函數,如pixelsAreEqual(RBGApixel p1, RGBApixel p2),它需要兩個像素,如果它們具有相同的RGB值,則返回true。

+0

你真棒我猜想我已經過去了。你對RBGApixel的懷疑是正確的。而RBGApixel不會返回一個int。我做了你的建議,但使用位移來獲得獨特的價值,現在我所要做的只是讓它變得漂亮並且打印顏色而不是值。然後我會發布工作代碼。謝謝你SOOOOOOOO很多! – user1852447

+0

嗯,我有點匆忙,這是有點工作,但我有計數和顏色之間的差異,但我會繼續努力,我會盡快發佈我的當前代碼。順便說一下,如果你想嘗試代碼FoolishSeth,EasyBMP可以免費下載 – user1852447

0

有很多不同的方法可以完成計算給定顏色的像素數目。如果您感興趣的顏色列表很短,您可以創建一個大小等於初始化爲全0的顏色數的數組,然後編寫一個函數,該函數根據傳遞的顏色返回該數組的索引它(例如,紅色= 0,白色= 1,以任意順序;常量可能是跟蹤它的一個好方法),然後循環遍歷調用每個函數的像素並在給定索引處遞增數組。

這很簡單,但非常不雅。

+0

首先謝謝你。第二我明白(大部分)你在說什麼。我有一個類似於你所說的修改版本。但是,如何將3個整數表示爲一個元素,即255,0,0 =紅色。無論如何,我一直在尋找並遇到問題。再次,我必須只計算連續像素,然後在該範圍內不再有任何連續像素時重新啓動計數器。 – user1852447

+0

所以我想出了一個更有幫助的步驟。我想如果我從顏色轉換到ARGB,我應該能夠轉換回ARGB。發現如何使用位移爲每個可能的值獲得唯一的int。我不需要阿爾法,所以我放棄了這一點。 – user1852447

+0

您是否試圖找到最長序列的相同顏色連續像素的長度?或所有序列的相同顏色順序pxiels的長度? – FoolishSeth