简体   繁体   English

实时摄像机馈送中的差异检测

[英]difference detection in live camera feed

I am facing a challenge where i am given a picture and i need to find the difference between an object in that picture and an object in front of me that i will see through an IP camera using python.我面临着一个挑战,给我一张图片,我需要找出该图片中的对象与我将使用 python 通过 IP 摄像机看到的我面前的对象之间的区别。 this is the originally handed photo这是最初递交的照片

原始照片

this is what the feed looks like from the IP camera这是来自 IP 摄像机的提要的样子

在此处输入图像描述

now i should write a program to detect the differences between the two objects and color code every different part.现在我应该编写一个程序来检测两个对象之间的差异,并对每个不同部分进行颜色编码。 its all easy if it included only those 2 pictures but the latter should be a live feed from the camera so the program i wrote gets overwhelmed with differences due to different camera positioning or different light exposure.如果它只包含那两张图片,一切都很容易,但后者应该是来自相机的实时馈送,所以我编写的程序会因不同的相机位置或不同的曝光而产生差异。

#!/usr/bin/env python
import cv2
from skimage import measure
import imutils

cap = cv2.VideoCapture(0)
img_gray = cv2.imread("pic1.png", 0)
img = cv2.imread("pic1.png")

while True:
    _, frame = cap.read()

    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    (score, diff) = measure.compare_ssim(img_gray, frame_gray, full=True)#we won't use the score
    diff = (diff * 255).astype("uint8") # converting the float score from a (-1,1) range to 8 bit 0-255 range

    thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

    contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    contours = imutils.grab_contours(contours)



    for cnt in contours:
        area = cv2.contourArea(cnt)
        if 3150 < area:
            (x,y,w,h) = cv2.boundingRect(cnt)
            cv2.rectangle(frame, (x,y), (x+w, y+h), (0,255,0))

    cv2.imshow("Current", frame)
    cv2.imshow("Back then", img)
    cv2.imshow("mask", thresh)


    if cv2.waitKey(1) & 0xFF == ord('q'):
        break



cap.release()
cv2.destroyAllWindows()

now the output on the colored video feed should be like this现在彩色视频提要的输出应该是这样

在此处输入图像描述

the color coding is not a problem颜色编码不是问题

i tried giving the drawn contours a set area to draw in and obviously it didn't work neither did the ssim score calculation.我试着给绘制的轮廓一个设定的区域来绘制,显然它没有用,ssim 分数计算也没有。 how should i approach this as it feels like i am trying to reinvent something already built but i couldn't find after 2 weeks of trying.我应该如何处理这个问题,因为感觉我正在尝试重新发明已经构建的东西,但经过 2 周的尝试后我找不到。

I write again the answer that I put on your other post .我再次写下我在您的其他帖子上的答案。 You can use SimpleElastix .您可以使用SimpleElastix I updated the code for those new images.我更新了这些新图像的代码。 Note that a part of the image is missing with the top pipe, so we actually don't know what's happening after the image border and the current algorithm only estimate the difference for the small part.请注意,图像的一部分与顶部管道缺失,因此我们实际上不知道图像边界之后发生了什么,当前算法仅估计了一小部分的差异。

    import cv2
    import numpy as np
    import matplotlib.pyplot as plt
    import SimpleITK as sitk

    movingImage = sitk.ReadImage('b1.png', sitk.sitkFloat32)
    fixedImage = sitk.ReadImage('b2.png', sitk.sitkFloat32)

    elastixImageFilter = sitk.ElastixImageFilter()

    affine_registration_parameters = sitk.GetDefaultParameterMap('affine')
    affine_registration_parameters["NumberOfResolutions"] = ['6']
    affine_registration_parameters["WriteResultImage"] = ['false']
    affine_registration_parameters["MaximumNumberOfSamplingAttempts"] = ['4']

    spline_registration_parameters = sitk.GetDefaultParameterMap('bspline')
    spline_registration_parameters["NumberOfResolutions"] = ['1']
    spline_registration_parameters["WriteResultImage"] = ['false']
    spline_registration_parameters["MaximumNumberOfSamplingAttempts"] = ['4']
    spline_registration_parameters["Metric"] = ['AdvancedMattesMutualInformation']
    spline_registration_parameters["GridSpacingSchedule"] = ['3']

    print(spline_registration_parameters["Registration"])

    parameterMapVector = sitk.VectorOfParameterMap()
    parameterMapVector.append(affine_registration_parameters)
    parameterMapVector.append(spline_registration_parameters)

    elastixImageFilter.SetFixedImage(fixedImage)
    elastixImageFilter.SetMovingImage(movingImage)
    elastixImageFilter.SetParameterMap(parameterMapVector)
    elastixImageFilter.Execute()

    registeredImage = elastixImageFilter.GetResultImage()
    transformParameterMap = elastixImageFilter.GetTransformParameterMap()

    resultImage = sitk.Subtract(registeredImage, fixedImage)
    resultImageNp = (np.sqrt(sitk.GetArrayFromImage(resultImage) ** 2)  > 60)*255

    cv2.imwrite('b_1.png', sitk.GetArrayFromImage(fixedImage))
    cv2.imwrite('b_2.png', sitk.GetArrayFromImage(movingImage))
    cv2.imwrite('b_2r.png', sitk.GetArrayFromImage(registeredImage))
    cv2.imwrite('b_diff.png', resultImageNp.astype(np.uint8))

Reference image:参考图片:
在此处输入图像描述
Registered second images:注册的第二张图片:
在此处输入图像描述
Thresholded difference:阈值差异:
在此处输入图像描述

sorry for the later answer but one of my team members used @fmw42 's comment and it worked.很抱歉后来的回答,但我的一名团队成员使用了@fmw42 的评论并且它起作用了。

and sorry for having no details as the script was lost很抱歉没有详细信息,因为脚本丢失了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在对来自IP摄像机的RTSP实时供稿执行对象检测时,Python脚本会在几秒钟后自动关闭? - Python script closes automatically after few seconds when it performs object detection on RTSP live feed from IP camera? python opencv实时摄像机提要,缩放对象 - python opencv live camera feed, scale object 在 django 网页中显示 ip 摄像头实时馈送 - show ip camera live feed in django webpage 如何在 python 中进行实时摄像头 object 检测 - how to do live camera object detection in python 在实时摄像头 object 中检测如何关闭摄像头并离开盒子 - In live camera object detection how to swich off the camera and leave the boxes 如何在opencv中将png img添加到实时相机提要中? - How to add a png img to live camera feed in opencv? OpenCV-使用实时相机源检测托盘中的硬币丢失 - OpenCV - detecting missing coins from a tray using a live camera feed Python OpenCV - 使用实时摄像机馈送帧作为输入的模板匹配 - Python OpenCV - Template Matching using the live camera feed frame as input 从摄像机到 tkinter window 和 opencv 的实时视频馈送 - Live video feed from camera to tkinter window with opencv 使用 Qt Gui 执行慢速实时视频源的 Opencv 人脸检测 - Opencv face detection on live video feed using Qt Gui performing slow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM