2012-12-18 3 views
0

두 개의 폴더 (각각 1000 개의 파일)의 이미지 파일을 일부 논리 인 (see this SO question)과 비교하는 프로그램을 작성했습니다.연결 (.)에서 XXXX 내의 초기화되지 않은 값 또는 image_magick.pl에서 문자열 사용 XX

는 이미지 (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하는 단계;

감사 가네

+1

첫 인상 : 잉어 :: 항상을 사용하여, 당신이 손상된 이미지 파일이있을 수 있습니다 – gaussblurinc

+1

:) 도움을 도와 드릴까요? 다른 이미지 세트로 시도해 보셨습니까? 항상 정확히 같은 장소에서 멈추는가? – foundry

+4

이 코드는 일부 리팩터링을 사용할 수 있습니다. 코드를 쉽게 테스트 할 수있는 서브 루틴으로 분리하면 가독성과 테스트 가능성이 향상되고 간단한 오타가 발생할 수 있습니다.두 이미지를로드하고 비교하면 오류를 가져 오는 것이 일반적인 작업이며 디렉토리를로드하는 것과 같습니다. 또한 'strict'를 다시 켜십시오. – amon

답변

2

내가 잘못된 라인 인 모르겠지만, 이들 중 하나가 후보가 될 가능성이 높습니다 :

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

또는

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

하는 메모를 수행 $index가 배열 경계 내에 있는지 여부를 지정합니다. 루프를 깨뜨릴 수있는 상태 인 $index > $#tfiles을 확인하지 마십시오.

당신은 print "length: ", scalar @array, "\n"; 같은 길이를 인쇄하여, >> 모두 입력 배열이 포함되어 있는지 (900 개) 요소를 주장 할 수 있습니다.

undefined 오류가 실제로 배열의 요소 definedness에 대한 테스트에 의해 발생하는 인덱스

당신은 테스트 할 수 있습니다에서 언급 한 바와 같이

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

하지만 다시는이 $i 사이의 오프셋 및 $index은 (일정 my answer) 실제로 두 개의 변수를 가질 필요는 없습니다.

간단한 비교기 서브 루틴은 코드를 읽기 쉽도록 만들 수 있으므로 디버깅을 돕습니다 (procedural programming 참조). 당신은 이런

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; 
} 

단계처럼 디렉토리를 읽을 코드를보다 읽기 쉽게하고 쉽게 검사를 삽입 할 수

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; 
} 

처럼
# 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; 
} 

라는.

+0

지금까지 나는이 오류로 stuggling하고 어떻게 든 마침내 그것을 얻을 수있었습니다. 그것은 해결책이 아니라 대신 다른 방법을 사용하여 비교했습니다. 지원해 주셔서 감사합니다. –

관련 문제