加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 06:19:27 所属栏目:百科 来源:网络整理
导读:首先是对图片进行处理: //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 = UIGraphicsGe

首先是对图片进行处理:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读