Compare images with identical and displayed identical pixels

I create this for iOS using Swift - either through CoreImage or GPUImage, but if I can create it in Python or Node / JavaScript, this will work too. Feel free to answer in abstract or in another language - I will agree to any answer that roughly describes how I can do this.

Consider the following two “images” (I created two grids of 3 × 3 pixels to represent two images, each 3 × 3 pixels for a total of 9 pixels).

Suppose I process the original image (on the left) with a shader that changes the color of some, but not all, pixels. The resulting image on the right is the same, but for 3 pixels - # 2, # 3 and # 6:

enter image description here

x, y , . , # 1, # 4, # 5, # 7, # 8 # 9 .

+4
1

, , , , - , . , , , , ( , 32 UInt32, , , RGBA, ), , - , Quartz Mac, , iOS UIKit , , ( X). ; , .

  func difference(leftImage: UIImage, rightImage: UIImage) {
      let width = Int(leftImage.size.width)
      let height = Int(leftImage.size.height)
      guard leftImage.size == rightImage.size else {
          return
      }
      if let cfData1:CFData = leftImage.cgImage?.dataProvider?.data,
         let l = CFDataGetBytePtr(cfData1),
         let cfData2:CFData = rightImage.cgImage?.dataProvider?.data,
         let r = CFDataGetBytePtr(cfData2) {
          let bytesPerpixel = 4
          let firstPixel = 0
          let lastPixel = (width * height - 1) * bytesPerpixel
          let range = stride(from: firstPixel, through: lastPixel, by: bytesPerpixel)
          for pixelAddress in range {
              if l.advanced(by: pixelAddress).pointee != r.advanced(by: pixelAddress).pointee ||     //Red
                 l.advanced(by: pixelAddress + 1).pointee != r.advanced(by: pixelAddress + 1).pointee || //Green
                 l.advanced(by: pixelAddress + 2).pointee != r.advanced(by: pixelAddress + 2).pointee || //Blue
                 l.advanced(by: pixelAddress + 3).pointee != r.advanced(by: pixelAddress + 3).pointee  {  //Alpha
                  print(pixelAddress)
                  // do stuff here
              }
          }
      }
  }

, , . , ( 0,0,0,0) , . , - . , , , , ( , ).

+4

Source: https://habr.com/ru/post/1666455/


All Articles