简体   繁体   English

了解ImageProcessing代码

[英]Understanding ImageProcessing code

I am new to android ndk.I have started learning through the image processing example by 我是android ndk的新手,我已经开始通过图像处理示例学习
ruckus and by IBM blog . ruckusIBM博客 I am not getting few lines below.Can any one please help me to understand the code snippet?` 我下面没有几行,请问有人可以帮助我理解代码片段吗?

static void brightness(AndroidBitmapInfo* info, void* pixels, float brightnessValue){
    int xx, yy, red, green, blue;
    uint32_t* line;

    for(yy = 0; yy < info->height; yy++){
            line = (uint32_t*)pixels;
            for(xx =0; xx < info->width; xx++){


              //extract the RGB values from the pixel
                red     = (int) ((line[xx] & 0x00FF0000) >> 16);
                green   = (int)((line[xx] & 0x0000FF00) >> 8);
                blue    = (int) (line[xx] & 0x00000FF );

        //manipulate each value
        red = rgb_clamp((int)(red * brightnessValue));
        green = rgb_clamp((int)(green * brightnessValue));
        blue = rgb_clamp((int)(blue * brightnessValue));

        // set the new pixel back in
        line[xx] =
          ((red << 16) & 0x00FF0000) |
          ((green << 8) & 0x0000FF00) |
          (blue & 0x000000FF);
            }

            pixels = (char*)pixels + info->stride;
        }
}
`
    static int rgb_clamp(int value) {
  if(value > 255) {
    return 255;
  }
  if(value < 0) {
    return 0;
  }
  return value;
}

How the RGB value are getting extracted and wht does this rgb_clamp do.Why are we setting new Pixell back and how does pixels = (char*)pixels + info->stride; 为什么要提取RGB值,而rgb_clamp呢?为什么要重新设置新的Pixell,像素如何=(char *)pixels + info-> stride; works? 作品? I am not ac/c++ guys and not having much knowledge of Image processing. 我不是ac / c ++专家,对图像处理知识也不多。 Thanks 谢谢

At first lets talk about one pixel. 首先让我们谈一个像素。 As far as i can see, it is a composition of at least 3 channels: r,g and b, which are all stored in one uint32_t value and has the format 0x00RRGGBB(32bit / 4 channels = 8bit per channel and thus a value range from 0..255). 据我所知,它至少由3个通道组成:r,g和b,它们都存储在一个uint32_t值中,格式为0x00RRGGBB(32bit / 4 channel =每个通道8bit,因此是一个值范围从0..255开始)。 So, to get the separated r, g and b-values you need to mask them out, which is done in the three lines below //extract the RGB values . 因此,要获得分离的r,g和b值,您需要将它们屏蔽掉,这可以在下面的三行中完成//extract the RGB values For example the red component... With the mask 0x00FF0000 and the & operator, you set every bit to 0 except the bits that are set in the red channel. 例如,红色分量...使用掩码0x00FF0000和&运算符,可以将除红色通道中设置的位以外的每个位都设置为0。 But when you just mask them out with 0x00RRGGBB & 0x00FF0000 = 0x00RR0000 , you would get a very big number. 但是,当您仅使用0x00RRGGBB & 0x00FF0000 = 0x00RR0000屏蔽它们时,您会得到很大的数字。 To get a value between 0 and 255 you also have to shift the bits to the right and that is what is done with the >> -operator. 要获得介于0到255之间的值,还必须将这些位向右移动,这就是使用>> -operator进行的操作。 So for the latter example: After applying the mask, you get 0x00RR0000, and shifting this 16 bit right ( >> 16 )gives you 0x000000RR , which is a number between 0 and 255. The same happens with the green channel, but with an 8bit right shift and since the blue value is already on the "right" bit position, there is no need to shift. 因此对于后一个示例:应用遮罩后,您将获得0x00RR0000,并将此16位右移( >> 16 )将为您提供0x000000RR ,这是一个介于0到255之间的数字。绿色通道也会发生这种情况,但是右移8位,并且由于蓝色值已经在“右”位位置,因此无需进行移位。

Second question: What rgb_clamp does is easy to explain. 第二个问题: rgb_clamp作用很容易解释。 It ensures, that your r,g or b-value, multiplied with your brightness factor, never exceeds the value range 0..255. 它确保您的r,g或b值乘以亮度因子不会超过0..255的值范围。

After the multiplication with the brightness factor, the new values are written back into memory, which happens in the reverse order of the above described extraction, this time shifting them leftwards and removing bits, that we don't want with the mask. 在与亮度因子相乘之后,新值将以上述提取的相反顺序写回到内存中,这次将它们向左移动并删除我们不希望使用掩码的位。

After one line of your image is processed, the info->stride is added, since for optimization purposes, the memory probably is aligned to fill 32byte boundaries and thus a single line can be longer than only image width and thus the "rest" of bytes are added to the pointer. 在处理完图像的一行后,添加了info->stride ,因为出于优化目的,内存可能会对齐以填充32字节边界,因此,一行可能比仅图像宽度长,因此,“剩余”字节被添加到指针。

First and foremost I suggest you read the C book here: http://publications.gbdirect.co.uk/c_book/ 首先,我建议您在这里阅读C书籍: http//publications.gbdirect.co.uk/c_book/

Next I'll go through your questions. 接下来,我将解决您的问题。

  1. How are the RGB values extracted 如何提取RGB值

    line is set to point to pixels parameter: line设置为指向像素参数:

    line = (uint32_t*)pixels;

    That is pixels is an array of 32 bit unsigned integers 也就是说, pixels是32位无符号整数的数组

    Then for the height and width of the bitmap the RGB values are extracted using a combination of bitwise ANDing ( & ) and bit shifting right ( >> ). 然后,针对位图的高度和宽度,使用按位&& )和右移( >> )的组合来提取RGB值。

    Lets see how you get red: 让我们看看您如何变红:

    red = (int) ((line[xx] & 0x00FF0000) >> 16);

    Here we get the current line, then AND with 0x00FF0000 as mask, this gets the bits 24-16 from the line . 在这里,我们得到当前行,然后以0x00FF0000作为掩码与AND,从该line获得位24-16。 So using RGB code #123456 as an example we will be left with 0x00120000 in the red variable. 因此,以RGB代码#123456为例, red变量中将保留0x00120000 But it's still in the 24-16 bit position so we right shift 16 bits to shift the bits down to 0x00000012 . 但是它仍然处于24-16位的位置,因此我们右移16位将这些位向下移至0x00000012

    We do this for the green and blue values, adjusting the AND mask and number of bits to shift right. 我们对绿色和蓝色值执行此操作,并调整“与”掩码和右移位数。

    More information on binary arithmetic can be found here: http://publications.gbdirect.co.uk/c_book/chapter2/expressions_and_arithmetic.html 有关二进制算术的更多信息,请参见: http : //publications.gbdirect.co.uk/c_book/chapter2/expressions_and_arithmetic.html

  2. What does rgb_clamp do rgb_clamp什么作用

    This function simply ensures the red, green, or blue values are 0 or above or 255 and below. 此功能仅确保红色,绿色或蓝色值等于或大于0或等于或小于255。

    If the parameter to rbg_clamp is -20 the it will return 0, which will be used to set the RGB value. 如果rbg_clamp的参数为-20,它将返回0,该值将用于设置RGB值。 If the parameter is rbg_clamp is 270 it will return 255. 如果参数rbg_clamp为270,它将返回255。

    RGB values for each colour must not exceed 225 or be below 0. In this example 255 being the brightest and 0 being the darkest value. 每种颜色的RGB值不得超过225或小于0。在此示例中,255是最亮的值,0是最暗的值。

  3. Why are we setting pixel back 我们为什么要放回像素

    It appears we are changing the brightness of the pixel, and setting the value back ready to be displayed. 看来我们正在更改像素的亮度,并将该值重新设置为可以显示。

  4. How does pixels = (char*)pixels + info->stride; pixels = (char*)pixels + info->stride; work? 工作?

    Without knowing the structure of info variable of AndroidBitmapInfo type, I would guess info->stride refers to the width of the bitmap in bytes so line will become the next line on the next loop iteration. 不知道AndroidBitmapInfo类型的info变量的结构,我想info->stride指的是位图的宽度(以字节为单位),因此该line成为下一个循环迭代的下一行。

Hope that helps 希望能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM