2

I am trying to find frames that matches an image using opencv. I also want to find the timeframe at which the the image is found. The video is a masked video. The code so far:

 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)
Daniel
  • 980
  • 9
  • 20

2 Answers2

5

Finding the same frame is not so easy issue. There are many possible solutions.I will describe here the possible solutions in a very general way.

Template Matching

Template matching is algorithm that calculate the similarity of the corresponding pixels in the images. So if You are looking for very similar image (without rotation, translation, large intenisty changes) it is not so bad algorithm. It is not so fast for whole images. It is rather used to find the same fragment on several images or smaller image on bigger image, not to check similarity of two images. https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_template_matching/py_template_matching.html

For whole images it is easier to just simply subtract the images then use template matching. It is much faster. There must be an assumption that they are really similar to each other.

Histogram Comparision

You can use histogram comparision. It is the fastest way, but it is not accurate. Grass and apples are both green, but dissimilar to each other. It's usually better to use the HSV color space when it comes to color. https://docs.opencv.org/3.4.1/d8/dc8/tutorial_histogram_comparison.html

Feature matching

Algorithm is searching for simillar characteristic points on images. There are many algorithms to find features on images. They should be insensitive to scale change and rotation etc. But it depends on the feature extraction algoritm. https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_features_meaning/py_features_meaning.html#features-meaning

Other alogithms

Other algoritms are PSNR or SSIM. I have never used it, but there are used to calculate similarity for original and blur image or for similarity of whole video sequence. https://docs.opencv.org/3.4.2/d5/dc4/tutorial_video_input_psnr_ssim.html

You can too try to compare hashes of images. It is very interessting algorithm (for me), but it is not well documented. https://www.pyimagesearch.com/2017/11/27/image-hashing-opencv-python/

Feature matching are the most-used algorithm for this type of task. The reason for that is feature matching alogrithms can detect similar fragments of images when images are taken from a different angle, in different conditions, or only partially overlap. Structure From Motion algorithms are often using feature matching. https://hub.packtpub.com/exploring-structure-motion-using-opencv/ The solution to the problem always depends on the data we have. So there is no one answer.

Piotr Kurowski
  • 346
  • 1
  • 3
2

If I am not mistaken what you want to do is called Template Matching, you can find opencv tutorial of that feature here. Also this thread might be useful for you, especially @Sam answer, which beyond Template Matching describe also Comparing Histograms and Feature Matching.

Daweo
  • 31,313
  • 3
  • 12
  • 25
  • 3
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/21957234) – scotty3785 Jan 17 '19 at 14:00
  • 2
    @scotty3785 This post doesn't meet the [criteria](https://meta.stackexchange.com/questions/225370/your-answer-is-in-another-castle-when-is-an-answer-not-an-answer) for being a link-only answer because it still contains some useful information if you were to remove the links: the fact that this is called Template Matching. Even if the links go down, there's still some information to go on here. – EJoshuaS - Stand with Ukraine Jan 17 '19 at 14:29