简体   繁体   English

处理图像的RGB

[英]Dealing with RGB of the image

I am developing one application . 我正在开发一个应用程序。 I have requirement like my application should detect particular color of the image . 我有要求,例如我的应用程序应检测图像的特定颜色。 For eg if i want to detect the red color then i should see only the red areas of the image. 例如,如果我要检测红色,则应该只看到图像的红色区域。 I have tried this by following way : 我已经尝试通过以下方式:

psudocode : 伪码:

First i have got the information of the each pixel of the image in the form of RGB using: 首先,我使用以下方法以RGB的形式获取了图像的每个像素的信息:

    for(int i=0; i < width; ++i)
{
    for (int j=0; j <height; j++)
    {
         byteIndex = (bytesPerRow * j) + i * bytesPerPixel;

        CGFloat red   = (rawData[byteIndex]     * 1.0) /255.0;
        CGFloat green = (rawData[byteIndex + 1] * 1.0)/255.0 ;
        CGFloat blue  = (rawData[byteIndex + 2] * 1.0)/255.0 ;
        if (red>0.9 && red<1)
        {
            rgba[byteIndex]   =red*255;
            rgba[byteIndex+1] =green*0 ;
            rgba[byteIndex+2] = blue*0;
            count++;

        }

    }

}

then i am assigning this information to another image with null blue and green section hence i can only see the red areas of the image .It is working fine . 然后我将此信息分配给另一个图像,该图像的蓝色和绿色部分为空,因此我只能看到该图像的红色区域。它工作正常。

But the problem is with the for loop. 但是问题出在for循环上。 I have to iterate for loop depend on the the height and width of the image . 我必须根据图像的高度和宽度迭代循环。 For eg . 例如。 If height of the image is 300 and width is 400 then is have to iterate the for loop for 300 * 400 =120000 times Which i don't think is a better way .So is there any way to do this efficiently ? 如果图像的高度是300而宽度是400,则必须迭代for循环300 * 400 = 120000次,我认为这不是更好的方法。那么有什么方法可以有效地做到这一点? is there any open source library to achieve this ? 有没有开源库可以实现这一目标?

You can begin optimizing your code. 您可以开始优化代码。 There are a lot of unnecessary operations and float/integer conversions there. 那里有很多不必要的操作和浮点/整数转换。

for ( NSUInteger i = 0; i < width * height ; i ++ ) {
    NSUInteger red = rawData[i] ;
    if ( red > 230 ) {
        rawData[i+1] = 0 ;
        rawData[i+2] = 0 ;
    }
}

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

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