tag*_*aga 5 python opencv image-processing heatmap
我想编写一个代码,将热图应用于有运动的地方的视频。我写了一个检测运动的代码,有轮廓,但我不知道如何制作热图。
这是我拥有的代码
import cv2
import numpy as np
# upload video
cap = cv2.VideoCapture('test_video.mp4')
#reading two frames
ret, frame1 = cap.read()
ret, frame2 = cap.read()
while cap.isOpened():
# get diference between two frames
diff = cv2.absdiff(frame1, frame2)
# convert diference in gray
gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
# bluring and treshold
blur = cv2.GaussianBlur(gray, (5,5), 0)
_, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
dilated = cv2.dilate(thresh, None, iterations = 3)
# define contours
contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# draw contours
cv2.drawContours(frame1, contours, -1, (255,0,0), 1)
# show frames
cv2.imshow('frame', frame1)
frame1 = frame2
ret, frame2 = cap.read()
if cv2.waitKey(60) == 60:
break
cv2.destroyAllWindows()
cap.release()
Run Code Online (Sandbox Code Playgroud)
我看过这个链接:Build a Motion Heatmap Video Using OpenCV With Python。我想重现代码,但是很多东西如fourcc, image_folder, 和images没有定义,所以我试着用另一种方式来做。
你能帮我解决这个问题吗?基本上,我想将热图应用于有运动的视频。
这是一个想法。您知道如何使用 opencv 循环播放视频帧,对吗?那么,对于while循环所在的每一帧,将当前帧之后的帧存储在变量中,并比较当前帧和未来帧之间的差异。
通过两帧之间的差异,您可以检测运动的轮廓。假设我们用绿色在图像上绘制轮廓。
在循环之前定义一个空白数组while作为热图;循环的每次迭代,向帧上坐标为绿色的热图的每个坐标添加一定量,并从图像上坐标不是绿色的热图中删除一定量。
import cv2
import numpy as np
Run Code Online (Sandbox Code Playgroud)
def process(img):
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_blur = cv2.GaussianBlur(img_gray, (5, 5), 25)
img_canny = cv2.Canny(img_blur, 5, 50)
kernel = np.ones((3, 3))
img_dilate = cv2.dilate(img_canny, kernel, iterations=4)
img_erode = cv2.erode(img_dilate, kernel, iterations=1)
return img_erode
Run Code Online (Sandbox Code Playgroud)
def get_contours(img, img_original):
img_contours = img_original.copy()
contours, hierarchies = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(img_contours, contours, -1, (0, 255, 0), -1)
return img_contours
Run Code Online (Sandbox Code Playgroud)
while;第一个是循环每次迭代的当前帧while,第二个是循环每次迭代的未来帧while。另外,定义热图的空白图像heat_map:cap = cv2.VideoCapture("Bar fight security cam.mp4")
success, img1 = cap.read()
success, img2 = cap.read()
heat_map = np.zeros(img1.shape[:-1])
Run Code Online (Sandbox Code Playgroud)
while循环中,找到两个帧之间的差异,并获取每个循环的当前帧,并根据差异绘制轮廓:while success:
diff = cv2.absdiff(img1, img2)
img_contours = get_contours(process(diff), img1)
Run Code Online (Sandbox Code Playgroud)
3每个坐标添加一个数字,例如 ,并从图像上非绿色的每个坐标中减去 3。为了确保没有颜色导致通道值小于和大于,请将边界应用于热图:heat_mapget_contoursheat_map0255 heat_map[np.all(img_contours == [0, 255, 0], 2)] += 3
heat_map[np.any(img_contours != [0, 255, 0], 2)] -= 3
heat_map[heat_map < 0] = 0
heat_map[heat_map > 255] = 255
Run Code Online (Sandbox Code Playgroud)
heat_map为灰度图像,然后转换为热图像: img_mapped = cv2.applyColorMap(heat_map.astype('uint8'), cv2.COLORMAP_JET)
Run Code Online (Sandbox Code Playgroud)
cv2.imshow("Original", img1)
cv2.imshow("Heat Map", img_mapped)
img1 = img2
success, img2 = cap.read()
if cv2.waitKey(1) == ord('q'):
break
Run Code Online (Sandbox Code Playgroud)
共:
import cv2
import numpy as np
def process(img):
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_blur = cv2.GaussianBlur(img_gray, (5, 5), 25)
img_canny = cv2.Canny(img_blur, 5, 50)
kernel = np.ones((3, 3))
img_dilate = cv2.dilate(img_canny, kernel, iterations=4)
img_erode = cv2.erode(img_dilate, kernel, iterations=1)
return img_erode
def get_contours(img, img_original):
img_contours = img_original.copy()
contours, hierarchies = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(img_contours, contours, -1, (0, 255, 0), -1)
# If you want to omit smaller contours, loop through the detected contours, and only draw them on the image if they are at least a specific area. Don't forget to remove the line above if you choose the below block of code.
# for cnt in contours:
# if cv2.contourArea(cnt) > 500:
# cv2.drawContours(img_contours, [cnt], -1, (0, 255, 0), -1)
return img_contours
cap = cv2.VideoCapture("Bar fight security cam.mp4")
success, img1 = cap.read()
success, img2 = cap.read()
heat_map = np.zeros(img1.shape[:-1])
while success:
diff = cv2.absdiff(img1, img2)
img_contours = get_contours(process(diff), img1)
heat_map[np.all(img_contours == [0, 255, 0], 2)] += 3 # The 3 can be tweaked depending on how fast you want the colors to respond
heat_map[np.any(img_contours != [0, 255, 0], 2)] -= 3
heat_map[heat_map < 0] = 0
heat_map[heat_map > 255] = 255
img_mapped = cv2.applyColorMap(heat_map.astype('uint8'), cv2.COLORMAP_JET)
# img1[heat_map > 160] = img_mapped[heat_map > 160] Use this line to draw the heat map on the original video at a specific temperature range. For this it's where ever the temperature is above 160 (min is 0 and max is 255)
cv2.imshow("Original", img1)
cv2.imshow("Heat Map", img_mapped)
img1 = img2
success, img2 = cap.read()
if cv2.waitKey(1) == ord('q'):
break
Run Code Online (Sandbox Code Playgroud)