1

我使用CIMaximumComponent将屏幕上显示的图像转换为灰度。现在我想遍历灰度图像并应用一个阈值,这样如果强度 <150,它就存储为 0。否则它将是 1。

为了遍历图像,我尝试按照以下教程进行操作 并提出以下代码:

- (IBAction)thresholdImage:(id)sender {



CGImageRef sourceImage = self.imageView.image.CGImage;

CFDataRef theData; //creates a variable of CFDataRef to store data of the image.


theData = CGDataProviderCopyData(CGImageGetDataProvider(sourceImage)); //assigns theData variable to the actual image

UInt8 *pixelData = (UInt8 *) CFDataGetBytePtr(theData);

int dataLength = CFDataGetLength(theData);



for (int index = 0; index < dataLength; index += 4)
{
    if (pixelData[index]  < 150)
    {
        pixelData[index] = 0;

    } else
    {
        pixelData[index] = 1;

    }
}

CGContextRef context;


//gets info about the image such as its width, height, bytes per row, colorspace etc
context = CGBitmapContextCreate(pixelData,
                                CGImageGetWidth(sourceImage),
                                CGImageGetHeight(sourceImage),
                                8,
                                CGImageGetBytesPerRow(sourceImage),
                                CGImageGetColorSpace(sourceImage),
                                CGImageGetBitmapInfo(sourceImage));

CGImageRef newCGImage = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newCGImage];

CGContextRelease(context);
CFRelease(theData);
CGImageRelease(newCGImage);



//assign the output image to the imageview to display it

self.imageView.image = newImage;

}

但是,当我运行应用程序并按下阈值按钮(调用上述方法)时,应用程序崩溃并给我以下错误:

CoreFoundation`CFDataGetBytePtr:
0x32f6e0cc:  ldr    r1, [r0]

并在输出中说 (lldb)

有人可以帮我吗?

非常感谢!

4

0 回答 0