Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 比较两幅图像并找出差异的百分比_Ios_Swift - Fatal编程技术网

Ios 比较两幅图像并找出差异的百分比

Ios 比较两幅图像并找出差异的百分比,ios,swift,Ios,Swift,我一直在尝试通过图像制作一个动物识别应用程序。我的方法是将所选图像与图像数组中的其他图像进行比较,并列出任何产生90%以上相似度的比较。有没有其他方法来比较两幅相似但不相似的图像?如有任何建议,将不胜感激。这些计算还必须运行许多次迭代,因此非耗时方法将非常受欢迎 请尝试提供一些代码和答案,因为我在Swift方面不是很有经验。您可以像我一样,通过分析一帧到下一帧并对差异进行阈值化(我还没有缩小图像,可能应该这样做) //每次捕获帧时调用 func captureOutput(\uOutput:AV

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


请尝试提供一些代码和答案,因为我在Swift方面不是很有经验。

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

//每次捕获帧时调用
func captureOutput(\uOutput:AVCaptureOutput,didOutput sampleBuffer:CMSampleBuffer,from connection:AVCaptureConnection){
guard let imageBufferRef=CMSampleBufferGetImageBuffer(sampleBuffer)else{
返回
}
CVPixelBufferLockBaseAddress(imageBufferRef,[]);
//设置上一幅图像和下一幅图像之间的比较
如果prevPB==nil{
prevPB=imageBufferRef
}
如果(!isplay&&motionIsDetected(prevPB,imageBufferRef)==1){
isplay=true
打印(“播放视频”)
}
CVPixelBufferUnlockBaseAddress(imageBufferRef,[]);
//如果为true,则播放视频
prevPB=imageBufferRef
}
func pixelFrom(x:Int,y:Int,current:CVPixelBuffer)->(UInt8,UInt8,UInt8){
让baseAddress=CVPixelBufferGetBaseAddress(当前)
let bytesPerRow=CVPixelBufferGetBytesPerRow(当前)
让buffer=baseAddress!.AssumingMemoryBind(到:UInt8.self)
设索引=x*bytesPerRow+y
设b=缓冲区[索引]
设g=buffer[index+1]
设r=buffer[index+2]
返回(r、g、b)
}
检测到func motionIsDetected(\uPrev:CVPixelBuffer,\uCurrent:CVPixelBuffer)->Int{
var差异=0
让baseAddress=CVPixelBufferGetBaseAddress(当前)
let width=CVPixelBufferGetWidth(当前)
let height=CVPixelBufferGetHeight(当前)
//阈值:如果150个不同点和至少10个不同像素,则用abs(aa-bb)钳制r、b、g的元组
var MAGIC_阈值=120
var\u差值=10
如果(当前!=nil&&prev!=nil){
对于0中的x..MAGIC_阈值和abs(Int(setA.2)-Int(setB.2))>MAGIC_阈值{
差异=差异+1
}
}
}
}
打印(“差异”)
打印(差异)
如果差异>足够的差异{
返回1
}
返回0
}

使用第三方库;如果你没有计算机视觉方面的背景,不要试图自己构建这个。(不是苹果的苹果检测仪,因为它非常有限)。例如:或者。请检查这个答案,可能它对你有用,可能是它的复制品工作得很好,但太慢了。我可以得到任何快速性能的替代解决方案吗?缩小图像以便比较较少的像素
// 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

}