简体   繁体   中英

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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