gpt4 book ai didi

ios - 比较两个图像并找出差异百分比

转载 作者:行者123 更新时间:2023-12-02 03:31:45 25 4
gpt4 key购买 nike

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

请尝试提供一些带有答案的代码,因为我对 Swift 经验不是很丰富。

最佳答案

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

// 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

}

关于ios - 比较两个图像并找出差异百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41456328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com