迭代灰度图像

时间:2013-06-13 12:06:24

标签: ios image-processing

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

为了遍历图片,我尝试按照following tutorial提出了这段代码:

- (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) 在输出中

有人可以帮帮我吗?

非常感谢!

0 个答案:

没有答案