计算像bmp到像素值C ++的值

时间:2012-11-26 06:19:43

标签: c++ bitmap count pixel

我拍摄了一个Bitmap图像并从中拉出了R,G,B整数,用于指定的像素行。将int转换为字符串,以便我可以打印我的6种特定颜色。我无法弄清楚如何使用int。

问题 我能够将0-184行(对应于该行中的像素)打印为顺序数据1234 ...或者颜色为红色,红色,红色,黑色,黑色,灰色....

然而,我需要计算相同颜色,显示相似颜色的总和,并重置计数器,直到颜色再次出现然后重新计数。我认为if或if else会这样做但不完全。可能是我的代码结构导致了问题?

所以我想要的是:

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;
}

2 个答案:

答案 0 :(得分:0)

您可以通过许多不同的方法来计算给定颜色的像素数。如果您感兴趣的颜色列表相当短,则可以创建一个大小等于初始化为全0的颜色数的数组,然后编写一个函数,根据传递给该颜色的颜色返回该数组的索引。它(例如红色= 0,白色= 1,任意顺序;常量可能是跟踪它的好方法),然后循环调用每个像素上的函数并在给定索引处递增数组。

这很简单但非常不优雅。

答案 1 :(得分: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。