我正在尝试使用 opencv 查找与图像匹配的帧。我还想找到找到该图像的时间范围。该视频为蒙版视频。到目前为止的代码:
def occurence_counter(self):
img = cv2.imread('ref_img.jpg', cv2.IMREAD_COLOR)
# shrink
img = cv2.resize(img, (10, 10))
# convert to b&w
img = color.rgb2gray(img)
similarities = []
result = self.parse_video(img, str(self.lineEdit.text()).strip(), 1, False)
print result
def parse_video(self, image, video, n_matches, break_point=False,
verbose=False):
similarities = [{'frame': 0, 'similarity': 0}]
frame_count = 0
cap = cv2.VideoCapture(video)
while cap.isOpened():
ret, frame = cap.read()
if type(frame) == type(None):
break
# increment frame counter
frame_count += 1
# resize current video frame
small_frame = cv2.resize(frame, (10, 10))
# convert to greyscale
small_frame_bw = color.rgb2gray(small_frame)
Run Code Online (Sandbox Code Playgroud)
找到相同的框架并不是那么容易的问题。有很多可能的解决方案。我将在这里以非常笼统的方式描述可能的解决方案。
模板匹配
模板匹配是计算图像中对应像素的相似度的算法。因此,如果您正在寻找非常相似的图像(没有旋转、平移、大的强度变化),那么它并不是那么糟糕的算法。对于整个图像来说并不是那么快。它更适合在多个图像上查找相同的片段或在较大图像上查找较小的图像,而不是检查两个图像的相似性。 https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_template_matching/py_template_matching.html
对于整个图像,简单地减去图像然后使用模板匹配会更容易。它要快得多。必须假设它们确实彼此相似。
直方图比较
您可以使用直方图比较。这是最快的方法,但并不准确。草和苹果都是绿色的,但彼此不同。就颜色而言,通常最好使用 HSV 颜色空间。 https://docs.opencv.org/3.4.1/d8/dc8/tutorial_histogram_comparison.html
特征匹配
算法正在寻找图像上的相似特征点。有许多算法可以查找图像上的特征。它们应该对尺度变化和旋转等不敏感。但这取决于特征提取算法。 https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_features_meaning/py_features_meaning.html#features-meaning
其他算法
其他算法有 PSNR 或 SSIM。我从未使用过它,但它用于计算原始图像和模糊图像的相似度或整个视频序列的相似度。 https://docs.opencv.org/3.4.2/d5/dc4/tutorial_video_input_psnr_ssim.html
您也可以尝试比较图像的哈希值。这是非常有趣的算法(对我来说),但没有很好的文档记录。 https://www.pyimagesearch.com/2017/11/27/image-hashing-opencv-python/
特征匹配是此类任务最常用的算法。原因是当图像从不同角度、不同条件下拍摄或仅部分重叠时,特征匹配算法可以检测到相似的图像片段。Structure From Motion 算法通常使用特征匹配。 https://hub.packtpub.com/exploring-struct-motion-using-opencv/ 问题的解决方案始终取决于我们拥有的数据。所以没有一个答案。
| 归档时间: |
|
| 查看次数: |
4119 次 |
| 最近记录: |