0

我编写了一个程序来比较两个文件夹(每个有 1000 个文件)的图像文件和一些逻辑(参见这个 SO question)

在执行时,它成功比较直到 900 个图像,但随后它给出了一个错误,如Use of uninitialized value within @tfiles2 in concatenation (.) or string at C:\dropbox\Image_Compare\image_magick.pl line 55(#3)。然后我收到一个类似的弹出错误Perl Command Line Interpreter has stopped working,所以我关闭了程序。

我的代码如下:

#!/usr/bin/perl
 use Image::Magick;
 no warnings 'uninitialized';
 use warnings;
 use diagnostics;
#use strict;
 use List::Util qw(first);

    my $directory1="C:/dropbox/Image_Compare/folder1";
    opendir(DIR, $directory1) or die "couldn't open $directory1: $!\n";
    my @files1 = grep { (!/^\./) && -f "$directory1/$_" } readdir(DIR);
    closedir DIR;
    print @files1;
    print 'end of files1';
    my $directory2="C:/dropbox/Image_Compare/folder2";
    opendir(DIR, $directory2) or die "couldn't open $directory2: $!\n";
    my @files2= grep { (!/^\./) && -f "$directory2/$_" } readdir(DIR);
    closedir DIR;
    print @files2;
    print 'end of files2';
    print $files1[0];
    foreach my $fils2 (@files2)
    {
        $g1 = Image::Magick->new;
        $g2 = Image::Magick->new;
        $temp1 = $g1->Read( filename=>"C:/dropbox/Image_Compare/folder1/".$files1[0].""); 
        $temp1 = $g2->Read( filename=>"C:/dropbox/Image_Compare/folder2/".$fils2."");
        $g3 = $g1->Compare( image=>$g2, metric=>'AE' ); # compare
        $error1 = $g3->Get( 'error' );
        #print $error1;
        if ($error1 == '0') 
        {
            print "Matching image is:"; 
            print $fils2 . "\n"; 
            my $tdirectory2="C:/dropbox/Image_Compare/folder2";
            opendir(DIR, $tdirectory2) or die "couldn't open $directory2: $!\n";
            my @tfiles2 = grep { (!/^\./) && -f "$tdirectory2/$_" } readdir(DIR);
            closedir DIR;
            #my $index = firstidx { $_ eq'"' .$fils2.'"' } @tfiles2;
            my $index = first { $tfiles2[$_] eq $fils2} 0..$#tfiles2;
            #print $fils2;
            print $index;
            my $i=0;
            foreach my $fils1 (@files1)
            {
                print 'ganesh';
                print $files1[$i];
                print $tfiles2[$index];
                print 'gowtham'; print "<br />";
                #print @tfiles2;
                $g4 = Image::Magick->new;
                $g5 = Image::Magick->new;
                $temp2 = $g4->Read( filename=>"C:/dropbox/Image_Compare/folder1/".$files1[$i].""); 
                $temp2 = $g5->Read( filename=>"C:/dropbox/Image_Compare/folder2/".$tfiles2[$index]."");
                $g6 = $g4->Compare( image=>$g5, metric=>'AE' ); # compare
                $error2 = $g6->Get( 'error' );
                $i++;
                $index++;
                if ($error2 == '0') {}
                else {print "Image not matching:"; print $tfiles2[$index]; last;}
                #if ($i == '800') {last;}


            }
            last
        }
    }

谁能帮忙,我在哪里做错了。

文件夹1文件名:0025.bmp到1051.bmp;

文件夹2文件名:0000.bmp到1008.bmp;

谢谢 Ganesh

4

1 回答 1

2

我不知道有问题的线路是哪一条,但其中一条很可能是候选线路:

$temp2 = $g5->Read( filename=>"C:/dropbox/Image_Compare/folder2/".$tfiles2[$index]."");

或者

else {print "Image not matching:"; print $tfiles2[$index]; last;}

请注意,$index无论它是否在数组边界内,您都会递增。您不检查 condition $index > $#tfiles,这应该会中断循环。

您可能想要断言两个输入数组都包含 >> 900 个元素,方法是打印类似print "length: ", scalar @array, "\n";.

undefined您可以通过测试数组中元素的定义性来测试错误实际发生在哪个索引处:

if (not defined $tfiles[$index] or not defined $files1[$i]) {
   die "There was an undefined element at index=$index, i=$i";
}

但是话又说回来,$i和之间的偏移量$index是恒定的(如我的回答中所述),因此您实际上不必携带两个变量。

一个简单的比较器子例程可以使您的代码更具可读性,从而有助于调试(请参阅过程编程)。

# return true if matching, false otherwise.
sub compare_images {
   my ($file1, $file2) = @_;
   my $image1 = Image::Magick->new;
   $image1->Read(filename => $file1);
   my $image2 = Image::Magick->new;
   $image2->Read(filename => $file2);
   my $result = $image1->Compare(image => $image2, metric => 'AE')->Get('error');
   # free memory
   undef $image1;
   undef $image2;
   return 0 == $result;
}

叫喜欢

my $image_root = "C:/dropbox/Image_Compare";
my ($folder1, $folder2) = qw(folder1 folder2);
unless (compare_images("$image_root/$folder1/$files1[$i]", 
                       "$image_root/$folder2/$tfiles[$index]")) {
   print "Images not matching at index=$index, i=$i\n";
   print "filename: $tfiles[$index]\n";
   last;
}

您可以阅读您的目录,例如

sub get_images_from_dir {
   my ($dirname) = @_;
   -d $dirname or die qq(The path "$dirname" doesn't point to a directory!);
   opendir my $dir => $dirname or die qq(Can't open "$dirname": $!);
   my @files = grep {!/^\./ and -f "$dirname/$_"} readdir $dir;
   closedir $dir;
   unless (@files) { die qq(There were no interesting files in "$dirname".) }
   return @files;
}

像这样的步骤使代码更具可读性,并使插入检查变得容易。

于 2012-12-18T13:52:42.053 回答