我試圖從多個影像中分離出一個背景,這些影像彼此之間有一些不同的東西,即與背景重疊。我擁有的影像在此處單獨列出:
我想在一系列影像中做到這一點,因為我正在閱讀一些視頻提要,并通過獲取最后一幀來處理它們以隔離背景,如下所示:
import os
import cv2
first = True
bwand = None
for filename in os.listdir('images'):
curImage = cv2.imread('images/%s' % filename)
if(first):
first = False
bwand = curImage
continue
bwand = cv2.bitwise_and(bwand,curImage)
cv2.imwrite("and.png",bwand)
從這段代碼中,我總是用按位運算來增加我的緩沖區,但我得到的結果不是我想要的:按位和:

就視頻過濾和性能而言,并發添加到緩沖區的方式對我來說是最好的方法,但如果我把它當作一個串列,我可以像這樣尋找中值:
import os
import cv2
import numpy as np
sequence = []
for filename in os.listdir('images'):
curImage = cv2.imread('images/%s' % filename)
sequence.append(curImage)
imgs = np.asarray(sequence)
median = np.median(imgs, axis=0)
cv2.imwrite("res.png",median)
結果是我:

這仍然不完美,因為我正在尋找中值,如果我要尋找眾數值,性能會顯著下降。
是否有一種方法可以像第一個替代方案一樣獲得作為緩沖區的結果,但以良好的性能輸出給我最好的結果?
--Edit 正如@Christoph Rackwitz 所建議的,我使用了 OpenCV 背景減法器,它作為請求的功能之一,它是一個緩沖區,但結果并不是最令人愉快的:

代碼:
import os
import cv2
mog = cv2.createBackgroundSubtractorMOG2()
for filename in os.listdir('images'):
curImage = cv2.imread('images/%s' % filename)
mog.apply(curImage)
x = mog.getBackgroundImage()
cv2.imwrite("res.png",x)
uj5u.com熱心網友回復:
由于scipy.stats.mode需要很長時間才能完成它,我手動做了同樣的事情:
- 計算直方圖(對于每個影像的每一行的每個像素的每個通道)
argmax獲取模式- 重塑和鑄造
仍然不是視頻速度,但很好。numba可能可以加快速度。
filenames = ...
assert len(filenames) < 256, "need larger dtype for histogram"
stack = np.array([cv.imread(fname) for fname in filenames])
sheet = stack[0]
hist = np.zeros((sheet.size, 256), dtype=np.uint8)
index = np.arange(sheet.size)
for sheet in stack:
hist[index, sheet.flat] = 1
result = np.argmax(hist, axis=1).astype(np.uint8).reshape(sheet.shape)
del hist # because it's huge
cv.imshow("result", result); cv.waitKey()

如果我不使用直方圖和大量記憶體,而是使用固定數量的作業表和快取友好的資料訪問,它可能會更快。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/404758.html
標籤:
上一篇:在彩色點的影像中查找中心坐標
