像计数值对bmp像素值C ++ [英] Counting like values bmp to pixel value C++

查看:135
本文介绍了像计数值对bmp像素值C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我参加了一个位图图像,并从拉R,G,B为整数像素的指定行。转换的int值的字符串,这样我可以打印我的6特定的颜色。我无法弄清楚如何与INT做到这一点。

问题
我能打印排0-184(对应于像素的该行)作为一个连续的数据1234 ...或者红色,红色,红色,黑色,黑色,灰色......

不过,我需要像计数/颜色相同,显示的颜色一样的总和,并重置计数器,直到颜色再次出现则各显神通。我想,如果一个或人会做,但不完全是。这可能是导致一个问题,我的code结构?

所以,我的愿望是:

  5红,
10黑,
2红,
1灰,

等......

下面是我的code,我是初学者所以批评我的知识的缺乏,这样我可以好好学学。

 的#include<&iostream的GT;
#包括LT&;&sstream GT;
#包括LT&;串GT;
#包括EasyBMP.h
#包括EasyBMP_BMP.h
#包括EasyBMP_DataStructures.h
#包括EasyBMP_VariousBMPutilities.h//转换和比较功能
无效计算(INT I,INT X,INT P);INT主(INT ARGC,为const char * argv的[])
{BMP图像;
Image.ReadFromFile(BMP GOES HERE 24位);性病::法院LT&;< 图像的高度和宽度:<< Image.TellHeight()&所述;&下; ×&所述;&下; Image.TellWidth()&所述;&下;的std :: ENDL;性病::法院LT&;< 输入您行;INT pixX = 0;
给std :: cin>> pixX;//设置getpixel到行顶部
INT PIXY = 0;为(PIXY = 0; PIXY&下; Image.TellHeight(); PIXY ++)
{
    性病::法院LT&;< 像素:<< PIXY + 1;    RGBApixel温度= Image.GetPixel(pixX,PIXY);    //数组存储像素颜色整数
    INT魉[3];
    魉[0] = Temp.Red;
    魉[1] = Temp.Green;
    魉[2] = Temp.Blue;    计算(魉[0],PIXY [1],PIXY [2]);
}返回0;
}
无效计算(INT RNUM,诠释gnum,INT带bnum)
{//字符串,将包含结果
性病::字符串结果;//流用于转换
的std :: ostringstream转换;//插入字符'编号'的文本重新presentation流中
转换<< RNUM;转换<< gnum;转换<<带bnum;//设置'结果'的流的内容
结果= convert.str();//比较结果给我的给定值
如果(结果==25500)
{
    性病::法院LT&;< RED<<的std :: ENDL;
}
如果(结果==255255255)
{
    性病::法院LT&;< 白<<的std :: ENDL;
}
如果(结果==000)
{
    性病::法院LT&;< BLACK<<的std :: ENDL;
}
如果(结果==148148148)
{
    性病::法院LT&;< 灰色<<的std :: ENDL;
}
如果(结果==267326)
{
    性病::法院LT&;< 绿色<<的std :: ENDL;
}
如果(结果==2551260)
{
    性病::法院LT&;< ORANGE<<的std :: ENDL;
}
}

以下是工作code。需要注意的是,如果你使用它,我的形象只有6个特定的颜色。要更改打印出来按需要人们必须修改switch语句的情况。

 的#include<&iostream的GT;
#包括LT&;矢量>
#包括EasyBMP.h
#包括EasyBMP_BMP.h
#包括EasyBMP_DataStructures.h
#包括EasyBMP_VariousBMPutilities.h长toRGB(长红,长GRN,长BLU);
INT主(INT ARGC,为const char * argv的[])
{BMP图像;
Image.ReadFromFile(您的BMP HERE);性病::法院LT&;< 图像的高度和宽度:<< Image.TellHeight()&所述;&下; ×&所述;&下; Image.TellWidth()&所述;&下;的std :: ENDL;性病::法院LT&;< 输入您行;INT pixX = 0;
给std :: cin>> pixX;
如果(pixX!= 0)//如果不是0减去输入之一,形象开始于0,0
{
    pixX - = 1;
}长期流行= 0;
长POP1 = 0;RGBApixel电流= Image.GetPixel(pixX,0);长pixy1 [3]; //数组存储像素颜色整数
pixy1 [0] = current.Red;
pixy1 [1] = current.Green;
pixy1 [2] = current.Blue;POP1 = toRGB(pixy1 [0],pixy1 [1],pixy1 [2]);
诠释计数= 0;
对(INT PIXY = 0; PIXY&下; Image.TellHeight(); PIXY ++)
{
    RGBApixel温度= Image.GetPixel(pixX,PIXY);    长魉[3]; //数组存储像素颜色整数
    魉[0] = Temp.Red;
    魉[1] = Temp.Green;
    魉[2] = Temp.Blue;    弹出= toRGB(魉[0],PIXY [1],PIXY [2]);    如果(POP == POP1)
    {
        算上++;
    }
    其他
    {
        开关(POP1){
            情况下0:
                性病::法院LT&;< (<< COUNT<<)\\ t的<< 黑\\ n<<的std :: ENDL;
                打破;
            案例16711680:
                性病::法院LT&;< (<< COUNT<<)\\ t的<< 红\\ n<<的std :: ENDL;
                打破;
            案件9737364:
                性病::法院LT&;< (<< COUNT<<)\\ t的<< 灰色\\ n<<的std :: ENDL;
                打破;
            案例16777215:
                性病::法院LT&;< (<< COUNT<<)\\ t的<< WHITE \\ n<<的std :: ENDL;
                打破;
            案件1722650:
                性病::法院LT&;< (<< COUNT<<)\\ t的<< 绿色\\ n<<的std :: ENDL;
                打破;
            案例16743936:
                性病::法院LT&;< (<< COUNT<<)\\ t的<< 橙色\\ n<<的std :: ENDL;
                打破;
            默认:
                性病::法院LT&;< !!!无指定颜色!<< POP1<<的std :: ENDL;
                打破;
        }        POP1 =流行; //重置的次数和当前颜色
        数= 1;
    }
}
    如果(计数大于0)//返回最后一种颜色和数量
    {
        开关(POP1){
            情况下0:
                性病::法院LT&;< (<< COUNT<<)\\ t的<< 黑\\ n<<的std :: ENDL;
                打破;
            案例16711680:
                性病::法院LT&;< (<< COUNT<<)\\ t的<< 红\\ n<<的std :: ENDL;
                打破;
            案件9737364:
                性病::法院LT&;< (<< COUNT<<)\\ t的<< 灰色\\ n<<的std :: ENDL;
                打破;
            案例16777215:
                性病::法院LT&;< (<< COUNT<<)\\ t的<< WHITE \\ n<<的std :: ENDL;
                打破;
            案件1722650:
                性病::法院LT&;< (<< COUNT<<)\\ t的<< 绿色\\ n<<的std :: ENDL;
                打破;
            案例16743936:
                性病::法院LT&;< (<< COUNT<<)\\ t的<< 橙色\\ n<<的std :: ENDL;
                打破;
            默认:
                性病::法院LT&;< !!!无指定颜色!<< POP1<<的std :: ENDL;
                打破;
    }
}返回0;
}长toRGB(长,长的B,长C)//函数为R,G,B值转换为独特价值
{
长色= 0;
色| =(A和255)LT;< 16;
色| =(B&安培; 255)LT;< 8;
色| =(C&安培; 255);返回的颜色;
}


解决方案

我真的不前,理解你的问题,所以我写了一个新的答案更适合你的要求。

我觉得你可以得到你想要的东西像这样的东西(修改你的code):

  RGBApixel电流= Image.GetPixel(pixX,0);
诠释计数= 0;
为(PIXY = 0; PIXY&下; Image.TellHeight(); PIXY ++)
{
    RGBApixel温度= Image.GetPixel(pixX,PIXY);
    如果(临时==电流)
    {
         算上++;
    }
    其他
    {
         //相同颜色序列结束
         //添加code这里打印出当前颜色和数量
         ---你的输出code ----         现在//重置的次数和当前颜色
         当前=温度;
         数= 1;
    }
}//现在刚刚打印出来的最后一个序列
如果(计数大于0)
{
    ---你的输出code在这里再次---
}

有一件事情我不知道是怎么回事,如果在所有的 == 经营者为RGBApixel定义。如果没有定义或似乎并没有根据自己的颜色来画上等号像素,只是写像 pixelsAreEqual功能(RBGApixel P1,P2 RGBApixel)采用两个像素和如果它们具有相同RGB值返回真。

I took a Bitmap image and pulled the R, G, B ints from it for a specified row of pixels. Converted the int to a string so that I could print my 6 specific colors. I couldn't figure out how to do it with int.

The Problem I am able to print row 0-184 (corresponding to pixels in that row) out as a sequential data 1234... or color red, red, red, black, black, gray....

However I need to count like/same colors, display the sum of like colors, and reset a counter until that color comes up again then recount. I thought an if or if else would do it but not quite. It may be my code structure that is causing an issue?

So what I desire is:

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

etc......

Here is my code, I am a beginner so criticizes on my lack of knowledge so that I may learn properly.

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

Following is the working code. Note that if you use it that my image only has 6 specific colors. To change the print out one would have to modify the switch statement cases as desired.

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

解决方案

I didn't really understand your question before, so I wrote a new answer more appropriate to what you're asking.

I think you can get what you want with something like this (modifying your code):

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

One thing I'm not sure is how, if at all, the == operator is defined for RGBApixel. If it isn't defined or doesn't seem to equate pixels based on their color, just write a function like pixelsAreEqual(RBGApixel p1, RGBApixel p2) that takes two pixels and returns true if they have the same RGB values.

这篇关于像计数值对bmp像素值C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆