How can i compare more number of images in two folders using Perl

不问归期 提交于 2020-01-15 04:19:27

问题


I am little new to Perl and i need anyone's help in completing a strange requirement.

  1. I want to compare two videos frame by frame. I can compare them directly using AVISYNTH but the issue is both videos are same but will have initial frame starting at different timings.

  2. For this i have splitted each video into frames / images and stored them seperately in two folder

  3. Now i want to compare the frame1/image1 of first folder with the all the initial 10-15 images of second folder.

  4. If 4th image of second folder is matching, then further the process needs to be stopped and Frame 4 should be stored / treated as the index for second folder.

  5. Once this is done, i want to start the actual comparision of images between two folders. i.e Image 1 of folder 1 with Image 4 of folder 2 Image 2 of folder 1 with Image 5 of folder 2 Image 3 of folder 1 with Image 6 of folder 2... and so on

I want to do all this process in perl / Matlab. Can any body help me please with your thoughts or suggestions.

Thanks Ganesh


回答1:


Lets assume we have a subroutine compare_frames that can compare two frames and returns true if they are identical.

Then we can find the offset by looping over the first few frames in folder 2, and comparing the frames:

# return the offset of the first equal image,
# searching frames between "from" (defaulting to 1) and "upto" (defaulting to 15)
sub find_offset (%) {
   my ($min_index, $max_index) = @{+{ from=>1, upto=>15, @_ }}{qw(from upto)};
   for my $index ($min_index .. $max_index) { # assuming 1-based indexing scheme
      return $index - 1 if compare_frames(1, $index);
   }
   warn "no offset found";
   return undef; # or better, exit the program.
}

In your main part of the script, you would then run my $offset = find_offset upto=>15 and use that later for the bulk of the processing. However, this assumes that Folder 1 is always a subset of Folder 2.

In our comparision subroutine, we want to return "false" as soon as possible. Depending on the image format, we could say that two images cannot be equivalent if their size is different. However, bitmaps have a fixed size, so this would not make sense here as all frames have the same resolution. Other image formats have different compression densities, where two pictures might be equal, even if one is more compressed. In such cases, remove the stat code.

# decide if two frames are equal.
# $frame1 is from the first set of frames,
# $frame2 is the frame from the second set.
sub compare_frames {
   my ($frame1, $frame2) = @_;
   my $file1 = "folder1/$frame1";
   my $file2 = "folder2/$frame1";
   # stat the files for the size.
   my $size1 = (stat $file1)[7];
   my $size2 = (stat $file2)[7];
   return 0 if $size1 != $size2; # strict equality, maybe you want to include some sort of tolerance.
   # do main comparision
   $cmp->set_image1($file1);
   $cmp->set_image2($file2);
   return $cmp->compare;
}

where $cmp is a comparator object from Image::Compare, and a preset comparision method.

Then, during the main comparision, you would compare_frames($i, $i + $offset).



来源:https://stackoverflow.com/questions/13741033/how-can-i-compare-more-number-of-images-in-two-folders-using-perl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!