您的位置:首页 > 移动开发 > Swift

swift 判断/比较两张图片是否相同 - 感知哈希算法

2017-04-22 17:02 1141 查看
首先是对图片进行处理:

//1.缩小图片尺寸
func scaleToSize(img: UIImage, size: CGSize) -> UIImage {
UIGraphicsBeginImageContext(size)
img.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return scaledImage!
}


//2.简化色彩 将图片转换成灰度图片
func getGrayImage(sourceImage: UIImage) -> UIImage {
let imageRef: CGImage = sourceImage.cgImage!
let width: Int = imageRef.width
let height: Int = imageRef.height

let colorSpace = CGColorSpaceCreateDeviceGray()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue)
let context: CGContext = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)!

let rect: CGRect = CGRect.init(x: 0, y: 0, width: width, height: height)
context.draw(imageRef, in: rect)

let outPutImage: CGImage = context.makeImage()!

let newImage: UIImage = UIImage.init(cgImage: outPutImage)

return newImage
}


//3. 计算平均值, 比较像素的灰度
func pHashValueWithImage(image: UIImage) -> NSString {
let pHashString = NSMutableString()
let imageRef = image.cgImage!
let width = imageRef.width
let height = imageRef.height
let pixelData = imageRef.dataProvider!.data
let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)
var sum: Int = 0
for i in 0..<width * height {
if data[i] != 0 {
sum = sum + Int(data[i])
}
}
let avr = sum / (width * height)
for i in 0..<width * height {
if Int(data[i]) >= avr {
pHashString.append("1")
} else {
pHashString.append("0")
}
}
return pHashString
}


//4.计算哈希值 如果不相同的数据位不超过5,就说明两张图片很相似;如果大于10,就说明这是两张不同的图片。
func getDifferentValueCountWithString(str1: NSString, str2: NSString) -> NSInteger {
var diff: NSInteger = 0
let s1 = str1.utf8String!
let s2 = str2.utf8String!
for i in 0..<str1.length {
if s1[i] != s2[i] {
diff += 1
}
}
return diff
}


写个共用方法来比较两个图片是否相同

/// 比较两个图片是否相同, 这里比较尺寸为20*20
///
/// - Parameters:
///   - imageOne: 图片1
///   - imageTwo: 图片2
/// - Returns: 是否相同的布尔值
func isEqualImage(imageOne: UIImage, imageTwo: UIImage) -> Bool {
var equalResult = false
let mImageOne = self.getGrayImage(sourceImage: self.scaleToSize(img: imageOne, size: CGSize(width: 20, height: 20)))
let mImageTwo = self.getGrayImage(sourceImage: self.scaleToSize(img: imageTwo, size: CGSize(width: 20, height: 20)))
let diff = self.getDifferentValueCountWithString(str1: self.pHashValueWithImage(image: mImageOne), str2: self.pHashValueWithImage(image: mImageTwo))
print(diff)
if diff > 10 {
equalResult = false
} else {
equalResult = true
}
return equalResult
}


通过调用上面的共用方法,传入两个待比较的UIImage对象, 得到返回的Bool, 根据该布尔值进行相应的操作
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息