【问题标题】:Comparing two images and finding the percent difference比较两个图像并找到百分比差异
【发布时间】:2017-01-04 04:41:39
【问题描述】:

我一直在尝试通过图像制作动物识别应用。我的方法是将所选图像与图像阵列中的其他图像进行比较,并列出任何产生 90% 以上相似度的比较。有没有其他方法可以比较两个相似但不相似的图像?任何建议,将不胜感激。这些计算还必须运行多次迭代,因此非常感谢一种非耗时的方法。

请尝试提供一些代码来回答,因为我对 Swift 不是很有经验。

【问题讨论】:

标签: ios swift


【解决方案1】:

您可以像我一样通过分析一帧到下一帧并对差异进行阈值处理(我还没有缩小图像,并且可能应该)

// called everytime a frame is captured
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {

    guard let imageBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer) else {
        return
    }

    CVPixelBufferLockBaseAddress(imageBufferRef, []);

    // set up for comparisons between prev and next images
    if prevPB == nil {
        prevPB = imageBufferRef
    }

    if (!isPlaying && motionIsDetected(prevPB, imageBufferRef) == 1) {
        isPlaying = true
        print("play video")
    }
    CVPixelBufferUnlockBaseAddress(imageBufferRef,[]);


    // if true, play the video
    prevPB = imageBufferRef
}


func pixelFrom(x: Int, y: Int, current: CVPixelBuffer) -> (UInt8, UInt8, UInt8) {
    let baseAddress = CVPixelBufferGetBaseAddress(current)
    let bytesPerRow = CVPixelBufferGetBytesPerRow(current)
    let buffer = baseAddress!.assumingMemoryBound(to: UInt8.self)
    let index = x*bytesPerRow+y

    let b = buffer[index]
    let g = buffer[index+1]
    let r = buffer[index+2]

    return (r, g, b)
}

func motionIsDetected(_ prev:CVPixelBuffer, _ current:CVPixelBuffer) -> Int {

    var differences = 0

    let baseAddress = CVPixelBufferGetBaseAddress(current)

    let width = CVPixelBufferGetWidth(current)
    let height = CVPixelBufferGetHeight(current)


    // THRESHOLDING: clamp by abs(aa-bb) for tuple of r,b,g  if 150 difference and at least 10 different pixels
    var MAGIC_THRESHOLD = 120
    var ENOUGH_DIFFERENCES = 10

    if (current != nil && prev != nil) {
        for x in 0..<height { //rows
            for y in 0..<width { //cols
                var setA = pixelFrom(x: x, y: y, current: prev)
                var setB = pixelFrom(x: x, y: y, current: current)
                if abs(Int(setA.0) - Int(setB.0)) > MAGIC_THRESHOLD && abs(Int(setA.1) - Int(setB.1)) > MAGIC_THRESHOLD && abs(Int(setA.2) - Int(setB.2)) > MAGIC_THRESHOLD {
                    differences = differences + 1
                }
            }
        }
    }
    print(" difference" )
    print(differences)

    if differences > ENOUGH_DIFFERENCES {
        return 1
    }
    return 0

}

【讨论】:

  • 它工作得很好但是太慢了。我可以获得任何替代解决方案以提高性能吗?
  • 缩小图像以便比较更少的像素
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-10
  • 1970-01-01
  • 2017-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多