简体   繁体   中英

Finding the coordinates of pixel using ios

I am using core image framework in ios development for finding the colors of the pixels of image for that i am using the following code

CGImageRef imgSource = self.ImageView.image.CGImage;
CFDataRef m_DataRed1 = CGDataProviderCopyData(CGImageGetDataProvider(imgSource));
UInt8 *dataOrignal = (UInt8 *)CFDataGetBytePtr(m_DataRed1);
double lenghtSource = CFDataGetLength(m_DataRed1);
NSLog(@"lenght : %f",lenghtSource);
int redPixel;
int greenPixel;
int bluePixel;

for (int index=0 ; index<lenghtSource; index+=4) {
    if (dataOrignal[index+1] ==236 || dataOrignal[index+2]==236 || dataOrignal[index+3]==236) {
        dataOrignal[index + 1] = 205;
        dataOrignal[index +2] = 25;
        dataOrignal[index+3]= 55;
        NSLog(@"this the value of red channel %d", index);
    }


}


NSUInteger width = CGImageGetWidth(imgSource); //202
NSUInteger height = CGImageGetHeight(imgSource);//173
size_t bitsPerComponent = CGImageGetBitsPerComponent(imgSource);
size_t bitsPerPixel = CGImageGetBitsPerPixel(imgSource);
size_t bytesPerRow = CGImageGetBytesPerRow(imgSource);


NSLog(@"the width = %u and height=%u of the image is ",width,height );
NSLog(@"the bits per component is %zd", bitsPerComponent);
NSLog(@"the bits per pixel is %zd", bitsPerPixel);
NSLog(@"the bytes per row is %zd" , bytesPerRow);


CGColorSpaceRef colorspace = CGImageGetColorSpace(imgSource);
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imgSource);
CFDataRef newData = CFDataCreate(NULL, dataOrignal, lenghtSource);
CGDataProviderRef provider = CGDataProviderCreateWithCFData(newData);

CGContextRef context = CGBitmapContextCreate(newData, width, height, bitsPerComponent, bytesPerRow, colorspace, bitmapInfo);
CGPoint point = CGContextGetTextPosition(context);
NSInteger xx =  point.x;
point.y;

NSLog(@"this is the value of x axis %d",xx);

CGImageRef newImg = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorspace, bitmapInfo, provider, NULL, true, kCGRenderingIntentDefault);





UIImage *newImage1 = [UIImage imageWithCGImage:newImg];
self.ImageView.image = newImage1;

but now what i want is the x and y axis of the pixel , so can anybody please help me ? or anyone have this code done alreay ? note that i am not using opencv and if you know with the opencv then please guide completely

I hope this is what you are looking for.

I moved the function calls getting the width and height to above the for loop so they could be used in the for loop for the x and y coordinate calculations. Also, you might have been printing the wrong value for the red channel. I added in the appropriate release calls for your CG objects as well. I don't know about whether or not you have to release CF objects.

CGImageRef imgSource = self.ImageView.image.CGImage;
CFDataRef m_DataRed1 = CGDataProviderCopyData(CGImageGetDataProvider(imgSource));
UInt8 *dataOrignal = (UInt8 *)CFDataGetBytePtr(m_DataRed1);
double lenghtSource = CFDataGetLength(m_DataRed1);
NSLog(@"lenght : %f",lenghtSource);
int redPixel;
int greenPixel;
int bluePixel;

int triggeredIndex = 0;
NSUInteger width = CGImageGetWidth(imgSource); //202
NSUInteger height = CGImageGetHeight(imgSource);//173

for (int index=0 ; index<lenghtSource; index+=4)
{
    if (dataOrignal[index+1] ==236 || dataOrignal[index+2]==236 || dataOrignal[index+3]==236)
    {
        dataOrignal[index+1] = 205;
        dataOrignal[index+2] = 25;
        dataOrignal[index+3] = 55;

        int xcoord = index / (int)(4*width);    //Don't think the casts are necessary, but just in case...
        int ycoord = index % (int)(4*width);

        //use xcoord and ycoord here...

        //NSLog(@"this the value of red channel %d", index);
        //did you mean this?
        NSLog(@"this the value of red channel %d", dataOrginal[index]);
    }
}


//moved width and height to before the loop
size_t bitsPerComponent = CGImageGetBitsPerComponent(imgSource);
size_t bitsPerPixel = CGImageGetBitsPerPixel(imgSource);
size_t bytesPerRow = CGImageGetBytesPerRow(imgSource);


NSLog(@"the width = %u and height=%u of the image is ",width,height );
NSLog(@"the bits per component is %zd", bitsPerComponent);
NSLog(@"the bits per pixel is %zd", bitsPerPixel);
NSLog(@"the bytes per row is %zd" , bytesPerRow);


CGColorSpaceRef colorspace = CGImageGetColorSpace(imgSource);
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imgSource);
CFDataRef newData = CFDataCreate(NULL, dataOrignal, lenghtSource);
CGDataProviderRef provider = CGDataProviderCreateWithCFData(newData);

CGContextRef context = CGBitmapContextCreate(newData, width, height, bitsPerComponent,     bytesPerRow, colorspace, bitmapInfo);
CGPoint point = CGContextGetTextPosition(context);
NSInteger xx =  point.x;
point.y;    //this is unnecessary and does nothing

NSLog(@"this is the value of x axis %d",xx);

CGImageRef newImg = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorspace, bitmapInfo, provider, NULL, true, kCGRenderingIntentDefault);





UIImage *newImage1 = [UIImage imageWithCGImage:newImg];
self.ImageView.image = newImage1;

//always remember to clean up after yourself!
CGImageRelease(imgSource);
CGColorSpaceRelease(colorspace);
CGDataProviderRelease(provider);
CGContextRelease(context);
CGImageRelease(newImg);

Remember, don't forget to release your CG objects!

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