我需要比较两个图像的内容,看它们是否相似。我读过有关Graphics Content
,OpenCV
和Hashes-Check
的内容,但我无法弄清楚如何做到这一点。问题是我有一个缩略图,我必须比较全尺寸图像,看看它们是否相似,所以我不认为我必须检查图像的高度和宽度,因为它们的尺寸不一样。我读到了绘制图像内容并比较其字节但我不知道如何继续。
这是我的代码:
let url1 = NSData(contentsOf: URL(string: "https://url1.com/url1.jpg")!)
let url2 = NSData(contentsOf: URL(string: "https://url2.com/url2.jpg")!)
let img1 = UIImage(data: url1 as! Data)
let img2 = UIImage(data: url2 as! Data)
func convertCIImageToCGImage(inputImage: CIImage) -> CGImage! {
let context = CIContext(options: nil)
if context != nil {
return context.createCGImage(inputImage, from: inputImage.extent)
}
return nil
}
var ciImage = CIImage(image: img1!)
let cgImage = convertCIImageToCGImage(inputImage: ciImage!)
func createARGBBitmapContext(inImage: CGImage) -> CGContext {
var bitmapByteCount = 0
var bitmapBytesPerRow = 0
//Get image width, height
let pixelsWide = inImage.width
let pixelsHigh = inImage.height
// Declare the number of bytes per row. Each pixel in the bitmap in this
// example is represented by 4 bytes; 8 bits each of red, green, blue, and
// alpha.
bitmapBytesPerRow = Int(pixelsWide) * 4
bitmapByteCount = bitmapBytesPerRow * Int(pixelsHigh)
// Use the generic RGB color space.
let colorSpace = CGColorSpaceCreateDeviceRGB()
// Allocate memory for image data. This is the destination in memory
// where any drawing to the bitmap context will be rendered.
let bitmapData = malloc(bitmapByteCount)
// Create the bitmap context. We want pre-multiplied ARGB, 8-bits
// per component. Regardless of what the source image format is
// (CMYK, Grayscale, and so on) it will be converted over to the format
// specified here by CGBitmapContextCreate.
let context = CGContext(data: bitmapData, width: pixelsWide, height: pixelsHigh, bitsPerComponent: 8, bytesPerRow: bitmapBytesPerRow, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue)
// Make sure and release colorspace before returning
return context!
}
let context = createARGBBitmapContext(inImage: cgImage!)
context.draw(img1!.cgImage!, in: CGRect(x: 0.0,y: 0.0,width: img1!.size.width,height: img1!.size.height))