| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | package Imager::Filter::Statistic; | ||
| 4 | # ABSTRACT: Provide statistica-based pixel filters. | ||
| 5 | |||
| 6 | |||
| 7 | use List::Util qw<min max>; | ||
| 8 | use List::UtilsBy qw<max_by>; | ||
| 9 | |||
| 10 | use Imager; | ||
| 11 | use Imager::Color; | ||
| 12 | |||
| 13 | sub summerize { | ||
| 14 | my ($ctx, $pixels) = @_; | ||
| 15 | my $method = lc $ctx->{param}{method}; | ||
| 16 | my @c = map { [$_->rgba] } @$pixels; | ||
| 17 | |||
| 18 | my @sum = (0,0,0); | ||
| 19 | |||
| 20 | if ($method eq 'mean') { | ||
| 21 | my @sum; | ||
| 22 | for my $c (@c) { | ||
| 23 | $sum[0] += $c->[0]; | ||
| 24 | $sum[1] += $c->[1]; | ||
| 25 | $sum[2] += $c->[2]; | ||
| 26 | } | ||
| 27 | return [ | ||
| 28 | int($sum[0]/@c), | ||
| 29 | int($sum[1]/@c), | ||
| 30 | int($sum[2]/@c), | ||
| 31 | ] | ||
| 32 | } elsif ($method eq 'variance') { | ||
| 33 | my @sum; | ||
| 34 | for my $c (@c) { | ||
| 35 | $sum[0] += $c->[0]; | ||
| 36 | $sum[1] += $c->[1]; | ||
| 37 | $sum[2] += $c->[2]; | ||
| 38 | } | ||
| 39 | my @mean = (int($sum[0]/@c), int($sum[1]/@c), int($sum[2]/@c)); | ||
| 40 | my @variance = (0,0,0); | ||
| 41 | for my $c (@c) { | ||
| 42 | $variance[0] += ($c->[0] - $mean[0])**2 / $#c; | ||
| 43 | $variance[1] += ($c->[1] - $mean[1])**2 / $#c; | ||
| 44 | $variance[2] += ($c->[2] - $mean[2])**2 / $#c; | ||
| 45 | } | ||
| 46 | return \@variance; | ||
| 47 | } elsif ($method eq 'min') { | ||
| 48 | my @min = (255,255,255); | ||
| 49 | for my $c (@c) { | ||
| 50 | $min[0] = $c->[0] if $c->[0] < $min[0]; | ||
| 51 | $min[1] = $c->[1] if $c->[1] < $min[1]; | ||
| 52 | $min[2] = $c->[2] if $c->[2] < $min[2]; | ||
| 53 | } | ||
| 54 | return \@min; | ||
| 55 | } elsif ($method eq 'max') { | ||
| 56 | my @max = (0,0,0); | ||
| 57 | for my $c (@c) { | ||
| 58 | $max[0] = $c->[0] if $c->[0] > $max[0]; | ||
| 59 | $max[1] = $c->[1] if $c->[1] > $max[1]; | ||
| 60 | $max[2] = $c->[2] if $c->[2] > $max[2]; | ||
| 61 | } | ||
| 62 | return \@max; | ||
| 63 | } elsif ($method eq 'gradient') { | ||
| 64 | my @max = (0,0,0); | ||
| 65 | my @min = (255,255,255); | ||
| 66 | for my $c (@c) { | ||
| 67 | $min[0] = $c->[0] if $c->[0] < $min[0]; | ||
| 68 | $min[1] = $c->[1] if $c->[1] < $min[1]; | ||
| 69 | $min[2] = $c->[2] if $c->[2] < $min[2]; | ||
| 70 | $max[0] = $c->[0] if $c->[0] > $max[0]; | ||
| 71 | $max[1] = $c->[1] if $c->[1] > $max[1]; | ||
| 72 | $max[2] = $c->[2] if $c->[2] > $max[2]; | ||
| 73 | } | ||
| 74 | return [ | ||
| 75 | $max[0] - $min[0], | ||
| 76 | $max[1] - $min[1], | ||
| 77 | $max[2] - $min[2], | ||
| 78 | ] | ||
| 79 | } elsif ($method eq 'mode') { | ||
| 80 | my @freq; | ||
| 81 | for my $c (@c) { | ||
| 82 | $freq[0]{$c->[0]} += 1; | ||
| 83 | $freq[1]{$c->[1]} += 1; | ||
| 84 | $freq[2]{$c->[2]} += 1; | ||
| 85 | } | ||
| 86 | |||
| 87 | return [ | ||
| 88 | scalar(max_by { $freq[0]{$_} } (keys %{$freq[0]})), | ||
| 89 | scalar(max_by { $freq[1]{$_} } (keys %{$freq[1]})), | ||
| 90 | scalar(max_by { $freq[2]{$_} } (keys %{$freq[2]})), | ||
| 91 | ] | ||
| 92 | } elsif ($method eq 'median') { | ||
| 93 | my @pixels; | ||
| 94 | for my $c (@c) { | ||
| 95 | push @{$pixels[0]}, $c->[0]; | ||
| 96 | push @{$pixels[1]}, $c->[1]; | ||
| 97 | push @{$pixels[2]}, $c->[2]; | ||
| 98 | } | ||
| 99 | @{$pixels[0]} = sort {$a<=>$b} @{$pixels[0]}; | ||
| 100 | @{$pixels[1]} = sort {$a<=>$b} @{$pixels[1]}; | ||
| 101 | @{$pixels[2]} = sort {$a<=>$b} @{$pixels[2]}; | ||
| 102 | my $middle = int( (@c-1)/2 ); | ||
| 103 | return [ | ||
| 104 | $pixels[0][$middle], | ||
| 105 | $pixels[1][$middle], | ||
| 106 | $pixels[2][$middle], | ||
| 107 | ] | ||
| 108 | } | ||
| 109 | die "unknown statistic method = $method"; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub statistic_filter { | ||
| 113 | my %param = @_; | ||
| 114 | my $ctx = { param => \%param }; | ||
| 115 | |||
| 116 | my $img = $param{imager}; | ||
| 117 | my $img_copy = $img->copy(); | ||
| 118 | |||
| 119 | my ($w,$h) = split("x", $param{geometry}); | ||
| 120 | $w ||= $h; | ||
| 121 | my $w_half_1 = int(($w-1)/2); | ||
| 122 | my $w_half_2 = int($w/2); | ||
| 123 | my $h_half_1 = int(($h-1)/2); | ||
| 124 | my $h_half_2 = int($h/2); | ||
| 125 | my $img_bound_x = $img->getwidth - 1; | ||
| 126 | my $img_bound_y = $img->getheight - 1; | ||
| 127 | |||
| 128 | for my $y (0 .. $img_bound_y) { | ||
| 129 | for my $x (0..$img_bound_x) { | ||
| 130 | my $y_0 = max(0, $y - $h_half_1); | ||
| 131 | my $y_1 = min($y + $h_half_2, $img_bound_y); | ||
| 132 | my $x_0 = max(0, $x - $w_half_1); | ||
| 133 | my $x_1 = min($x + $w_half_2, $img_bound_x); | ||
| 134 | my $w_ = $x_1 - $x_0 + 1; | ||
| 135 | my @px = map { $img_copy->getscanline(y => $_, x => $x_0, width => $w_) } ($y_0 .. $y_1); | ||
| 136 | my $new_px = summerize($ctx, \@px); | ||
| 137 | $img->setpixel( y => $y, x => $x, color => $new_px); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | Imager->register_filter( | ||
| 143 | type => 'statistic', | ||
| 144 | callsub => \&statistic_filter, | ||
| 145 | callseq => ['image', 'method', 'geometry'], | ||
| 146 | defaults => {}, | ||
| 147 | ); | ||
| 148 | |||
| 149 | 1; | ||
| 150 | |||
| 151 | __END__ |