0

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))
Andrea Toso
  • 287
  • 4
  • 12
  • What you are asking for is a VERY difficult problem. OpenCV is a general purpose computer vision library that would be a good starting point for solving your problem, but it will take a fair amount of study to figure out how to use it to create your image comparison. – Duncan C Jan 08 '17 at 13:18
  • 1
    @DuncanC Yes it's a very difficult problem, but maybe there's someone who can help me. Anyway thank you for your comment. – Andrea Toso Jan 08 '17 at 13:22
  • Possible duplicate of [How to compare two UIImage objects](http://stackoverflow.com/questions/11342897/how-to-compare-two-uiimage-objects) – Ahmad F Jan 08 '17 at 13:25
  • @AhmadF It is not a duplicate. I've already tried and it is not what I'm looking for. Thank you anyway. – Andrea Toso Jan 08 '17 at 13:31
  • 1
    I doubt the I can help, but what you can do to help me know whether I can or nor is give me details on what makes two images "similar". Are you comparing a thumbnail to a larger image? If so check the dimensions, they should have a different scale but the same height-to-width ratio. Are you doing facial recognition, looking for the same amount of faces detected? Average color? It's up to you (or whomever gave you this very difficult problem) to give us this kind of detail. Images may be similar in many ways! –  Jan 08 '17 at 13:55
  • @dfd You're right, I didn't give enough details.I think the aspect ratio between the 2 images could be very helpful.This is an example:I have an image which is the thumbnail(250x250), and another one which is the full-size image(920x613).They are the same image with different dimensions.I'm not looking for a facial recognition or things like that. I tried to give the same height and width to both of the images and try to compare them but it seems it doesn't work.So the "only" think I want to compare is the content of the images. – Andrea Toso Jan 08 '17 at 14:23
  • @dfd I tried to compare them following this link: http://stackoverflow.com/questions/11342897/how-to-compare-two-uiimage-objects – Andrea Toso Jan 08 '17 at 14:26

0 Answers0