我有2张图像,我想比较,如果像素颜色是一样的,以保存它.
我通过这个UI Image扩展功能来检测像素的颜色:
我通过这个UI Image扩展功能来检测像素的颜色:
func getPixelColor(pos: CGPoint) -> ??? { let pixelData = CGDataProviderCopyData(CGImageGetDataProvider(self.CGImage)) let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData) let pixelInfo: Int = ((Int(self.size.width) * Int(pos.y)) + Int(pos.x)) * 4 let r = CGFloat(data[pixelInfo]) / CGFloat(255.0) let g = CGFloat(data[pixelInfo+1]) / CGFloat(255.0) let b = CGFloat(data[pixelInfo+2]) / CGFloat(255.0) let a = CGFloat(data[pixelInfo+3]) / CGFloat(255.0) return ??? }
例如,我在图1上运行扫描仪并将其保存在数组中?还是字典?之后,我在图片2上运行扫描仪,当我从2张照片中得到的信息与什么功能进行比较?
我想看看哪个CGPoint的像素颜色是相同的从2个图像?
更新:
我更新了getPixelColor以返回我“(pos)(r)(g)(b)(a)”,之后我创建了这个仅保留重复项的功能(在使用此功能之前,您必须.sort()ARRAY!)
extension Array where Element : Equatable { var duplicates: [Element] { var arr:[Element] = [] var start = 0 var start2 = 1 for _ in 0...self.count{ if(start2<self.count){ if(self[start] == self[start2]){ if(arr.contains(self[start])==false){ arr.append(self[start]) } } start+=1 start2+=1 } } return arr } }
这给我这样的东西:
“(609.0,47.0)1.01.01.01.0”我知道这个颜色是黑色的,我做x-536来适应iPhone 5屏幕,当我试图再画一次它可能会出错…也许我无法做到正确..帮助?
解决方法
UIImage扩展名返回UIColor.使用这种方法来比较两个图像的每个像素.如果两个像素匹配,请将颜色添加到数组数组.
extension UIImage { func getPixelColor(pos: CGPoint) -> UIColor { let pixelData = CGDataProviderCopyData(CGImageGetDataProvider(self.CGImage)) let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData) let pixelInfo: Int = ((Int(self.size.width) * Int(pos.y)) + Int(pos.x)) * 4 let r = CGFloat(data[pixelInfo]) / CGFloat(255.0) let g = CGFloat(data[pixelInfo+1]) / CGFloat(255.0) let b = CGFloat(data[pixelInfo+2]) / CGFloat(255.0) let a = CGFloat(data[pixelInfo+3]) / CGFloat(255.0) return UIColor(red: r,green: g,blue: b,alpha: a) } } func findMatchingPixels(aImage: UIImage,_ bImage: UIImage) -> [[UIColor?]] { guard aImage.size == bImage.size else { fatalError("images must be the same size") } var matchingColors: [[UIColor?]] = [] for y in 0..<Int(aImage.size.height) { var currentRow = [UIColor?]() for x in 0..<Int(aImage.size.width) { let aColor = aImage.getPixelColor(CGPoint(x: x,y: y)) let colorsMatch = bImage.getPixelColor(CGPoint(x: x,y: y)) == aColor currentRow.append(colorsMatch ? aColor : nil) } matchingColors.append(currentRow) } return matchingColors }
这样使用:
let matchingPixels = findMatchingPixels(UIImage(named: "imageA.png")!,UIImage(named: "imageB.png")!) if let colorForOrigin = matchingPixels[0][0] { print("the images have the same color,it is: \(colorForOrigin)") } else { print("the images do not have the same color at (0,0)") }
为了简单起见,我发现findMatchingPixels()要求图像的大小相同,但不需要太多允许不同大小的图像.
UPDATE
如果你只想要匹配的像素,我会返回一个这样的元组:
func findMatchingPixels(aImage: UIImage,_ bImage: UIImage) -> [(CGPoint,UIColor)] { guard aImage.size == bImage.size else { fatalError("images must be the same size") } var matchingColors = [(CGPoint,UIColor)]() for y in 0..<Int(aImage.size.height) { for x in 0..<Int(aImage.size.width) { let aColor = aImage.getPixelColor(CGPoint(x: x,y: y)) guard bImage.getPixelColor(CGPoint(x: x,y: y)) == aColor else { continue } matchingColors.append((CGPoint(x: x,y: y),aColor)) } } return matchingColors }