2012-05-16 37 views
2

我想從uiimage獲取數據以讀取rgb值。我想將這些數據用於卷積濾波器。在互聯網我發現只有一種方法:獲取數據圖像進行處理的最佳方法

// First get the image into your data buffer 
CGImageRef image = [UIImage CGImage]; 
NSUInteger width = CGImageGetWidth(image); 
NSUInteger height = CGImageGetHeight(image); 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
unsigned char *rawData_ = malloc(height * width * 4); 
NSUInteger bytesPerPixel = 4; 
NSUInteger bytesPerRow = bytesPerPixel_ * width; 
NSUInteger bitsPerComponent = 8; 
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent,   bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 
CGColorSpaceRelease(colorSpace); 

CGContextDrawImage(context, CGRectMake(0, 0, width, height)); 
CGContextRelease(context); 

// Now your rawData contains the image data in the RGBA8888 pixel format. 
int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel; 
red = rawData[byteIndex]; 
green = rawData[byteIndex + 1]; 
blue = rawData[byteIndex + 2]; 
alpha = rawData[byteIndex + 3]; 

及其確定,但我想與像素陣列的工作的一些這樣的:[X] [Y] [R,B,G]。

我該如何解決?

回答

0

作爲替代建議,我建議不要在CPU上進行圖像卷積。相反,我在我的GPUImage框架內有各種基於GPU的卷積,可以比CPU綁定實現快一個數量級。

這些包括重優化GPUImageSobelEdgeDetectionFilter,其上的圖像的亮度進行Sobel邊緣檢測,並且GPUImage3x3ConvolutionFilter,它可以讓你提供一個通用的3×3的卷積核應用到任意的圖像。對於實時視頻,這些可以實時輕鬆處理傳入幀。對於UIImages,這些濾波器可以比在CPU上執行的卷積運行速度快6-10倍。

根據你的具體問題,肯的建議是一個很好的建議,但實際上我發現蘋果在鏈接文章中建議的數據複製方法實際上最終比在位圖上下文中重繪圖像慢。這看起來很奇怪,但我在基準測試中得到了一致的結果。上面介紹的Core Graphics繪圖是我發現訪問原始UIImage像素數據的最快方式。

相關問題