Hough transform: Difference between revisions
Added Perl example
No edit summary |
SqrtNegInf (talk | contribs) (Added Perl example) |
||
Line 629:
* see [[Example:Hough transform/MATLAB]]
=={{header|Perl}}==
{{trans|Sidef}}
<lang perl>use Imager;
sub hough {
my($im) = shift;
my($width) = shift || 460;
my($height) = shift || 360;
$height = 2 * int $height/2;
$xsize = $im->getwidth();
$ysize = $im->getheight();
$ht = Imager->new(xsize => $width, ysize => $height);
for $i (0..$height) { for $j (0..$width) { $canvas[$i][$j] = 255 } }
$ht->box(filled => 1, color => 'white');
$rmax = sqrt($xsize**2 + $ysize**2);
$dr = 2 * $rmax / $height;
$dth = 3.141593 / $width;
for $x (0..$xsize-1) {
for $y (0..$ysize-1) {
my $col = $im->getpixel(x => $x, y => $y);
my($r,$g,$b) = $col->rgba;
next if $r==255; # && $g==255 && $b==255;
for $k (0..$width) {
$th = $dth*$k;
$r = ($x*cos($th) + $y*sin($th));
$iry = ($height/2 + int($r/$dr + 0.5));
$ht->setpixel(x => $k, y => $iry, color => [ ($canvas[$iry][$k]--) x 3] );
}
}
}
return $ht;
}
$img = Imager->new(file => 'pentagon.png');
$ht = hough($img);
$ht->write(file => 'hough_transform.png');
</lang>
=={{header|Python}}==
|