I need to compare the content of two images to see if they are similar or not. I read something about Graphics Content
, OpenCV
and Hashes-Check
but I can't figure out how to do it. The problem is that I have a thumbnail image and I have to compare the full size image and see if they are similar or not, so I don't think that I have to check image's height and width because their dimensions are not the same. I read about drawing the content of the image and compare its bytes but I don't know how to go on.
Here is my code:
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))