我在 ios 开发中使用核心图像框架来查找图像像素的颜色,我正在使用以下代码
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;
但现在我想要的是像素的 x 和 y 轴,所以有人可以帮我吗?或者有人已经完成了这段代码吗?请注意,我没有使用opencv,如果您知道opencv,请完全指导