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

首先是对图片进行处理:

//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,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,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,根据该布尔值进行相应的操作

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...