ios - Swift - Compare colors at CGPoint -
i have 2 pictures want compare, if pixel color same save it. detect color of pixel uiimage
extension function:
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 = cgfloat(data[pixelinfo+3]) / cgfloat(255.0) return ??? }
for example, run scanner on picture 1 , save in array? or dictionary? , after run scanner on picture 2 , when have information 2 pictures compare function?
i want see on cgpoint pixels colors identical 2 images?
update: update getpixelcolor return me "(pos)(r)(g)(b)(a)" , after created function left duplicates (before using function have .sort() array!)
extension array element : equatable { var duplicates: [element] { var arr:[element] = [] var start = 0 var start2 = 1 _ 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 } }
this returns me this: "(609.0, 47.0)1.01.01.01.0" know color black @ point x-536 fit iphone 5 screen , when make attempt draw again draws wrong... maybe can't properly.. help?
have uiimage extension return uicolor. use method compare each pixel of 2 images. if both pixels match, add color array of arrays.
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 = 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 same size") } var matchingcolors: [[uicolor?]] = [] y in 0..<int(aimage.size.height) { var currentrow = [uicolor?]() 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 }
used this:
let matchingpixels = findmatchingpixels(uiimage(named: "imagea.png")!, uiimage(named: "imageb.png")!) if let colorfororigin = matchingpixels[0][0] { print("the images have same color, is: \(colorfororigin)") } else { print("the images not have same color @ (0,0)") }
for simplicity made findmatchingpixels()
require images same size, wouldn't take allow different sized images.
update
if want pixels match, i'd return tuple this:
func findmatchingpixels(aimage: uiimage, _ bimage: uiimage) -> [(cgpoint, uicolor)] { guard aimage.size == bimage.size else { fatalerror("images must same size") } var matchingcolors = [(cgpoint, uicolor)]() y in 0..<int(aimage.size.height) { 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 }
Comments
Post a Comment