Hough transform: Difference between revisions

m
(Add Rust implementation)
m (→‎{{header|Wren}}: Minor tidy)
 
(4 intermediate revisions by 3 users not shown)
Line 19:
BBC BASIC uses Cartesian coordinates so the image is 'upside down' compared with some other solutions.
[[Image:hough_bbc.gif|right]]
<langsyntaxhighlight lang="bbcbasic"> Width% = 320
Height% = 240
Line 65:
REPEAT
WAIT 1
UNTIL FALSE</langsyntaxhighlight>
 
=={{header|C}}==
Line 74:
{{trans|Go}}
This uses the module from the Grayscale image Task. The output image is the same as in the Go solution.
<langsyntaxhighlight lang="d">import std.math, grayscale_image;
 
Image!Gray houghTransform(in Image!Gray im,
Line 112:
.houghTransform()
.savePGM("Pentagon_hough.pgm");
}</langsyntaxhighlight>
 
=={{header|Go}}==
[[file:GoHough.png|right|thumb|Output png]]
{{trans|Python}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 187:
fmt.Println(err)
}
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
{{libheader|JuicyPixels}}
<langsyntaxhighlight Haskelllang="haskell">import Control.Monad (forM_, when)
import Data.Array ((!))
import Data.Array.ST (newArray, writeArray, readArray, runSTArray)
Line 307:
_ ->
putStrLn $
"Usage: " ++ prog ++ " <image-file> <out-file.png> <width> <height>"</langsyntaxhighlight>
'''Example use:'''
<syntaxhighlight lang="text">HoughTransform Pentagon.png hough.png 360 360</langsyntaxhighlight>
 
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j">NB.*houghTransform v Produces a density plot of image y in hough space
NB. y is picture as an array with 1 at non-white points,
NB. x is resolution (width,height) of resulting image
Line 323:
rho=. <. 0.5+ h * (rho-min) % max-min NB. Rescale rho from 0 to h and round to int
|.([: <:@(#/.~) (i.h)&,)"1&.|: rho NB. consolidate into picture
)</langsyntaxhighlight>
[[Image:JHoughTransform.png|320px200px|thumb|right|Resulting viewmat image from J implementation of Hough Transform on sample pentagon image]]'''Example use:'''
<langsyntaxhighlight lang="j"> require 'viewmat'
require 'media/platimg' NB. addon required pre J8
Img=: readimg_jqtide_ jpath '~temp/pentagon.png'
viewmat 460 360 houghTransform _1 > Img</langsyntaxhighlight>
<br style="clear:both" />
 
=={{header|Java}}==
'''Code:'''
<langsyntaxhighlight Javalang="java">import java.awt.image.*;
import java.io.File;
import java.io.IOException;
Line 474:
return;
}
}</langsyntaxhighlight>
 
[[Image:JavaHoughTransform.png|640px480px|thumb|right|Output from example pentagon image]]'''Example use:'''
Line 481:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using ImageFeatures
 
img = fill(false,5,5)
Line 487:
 
println(hough_transform_standard(img))
</langsyntaxhighlight> {{output}} <pre>
Tuple{Float64,Float64}[(3.0, 1.5708)]
</pre>
Line 493:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">import java.awt.image.BufferedImage
import java.io.File
import javax.imageio.ImageIO
Line 584:
val minContrast = if (args.size >= 4) 64 else args[4].toInt()
inputData(args[2].toInt(), args[3].toInt(), minContrast).writeOutputImage(args[1])
}</langsyntaxhighlight>
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">with(ImageTools):
img := Read("pentagon.png")[..,..,1]:
img_x := Convolution (img, Matrix ([[1,2,1], [0,0,0],[-1,-2,-1]])):
Line 629:
end proc:
result :=HoughTransform(img,row,col);
Embed(Scale(FitIntensity(Create(result)), 1..500,1..500));</langsyntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
 
<syntaxhighlight lang="mathematica">
<lang Mathematica>
Radon[image, Method -> "Hough"]
</syntaxhighlight>
</lang>
 
=={{header|MATLAB}}==
Line 645:
{{libheader|nimPNG}}
We use the modules from tasks “Bitmap” and “Grayscale image”, adding necessary conversions to read and write PNG files.
<langsyntaxhighlight Nimlang="nim">import lenientops, math
import grayscale_image
 
Line 697:
for color in houghImage.pixels:
data.add([color.r, color.g, color.b])
discard savePNG24(Output, data, houghImage.w, houghImage.h)</langsyntaxhighlight>
 
=={{header|Perl}}==
{{trans|Sidef}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 745:
my $ht = hough($img);
$ht->write(file => 'hough_transform.png');
</syntaxhighlight>
</lang>
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
{{trans|Sidef}}
<!--<syntaxhighlight lang="phix">(notonline)-->
<lang Phix>-- demo\rosetta\Hough_transform.exw
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Hough_transform.exw</span>
include pGUI.e
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- IupImage, imImage, im_width/height/pixel, allocate,
 
-- imFileImageLoadBitmap, IupImageFromImImage</span>
function hypot(atom a,b) return sqrt(a*a+b*b) end function
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
 
function hough_transform(imImage im, integer width=460, height=360)
height = 2*floor(height / 2)
integer xsize = im_width(im),
ysize = im_height(im)
sequence canvas = repeat(repeat(255,width),height)
atom rmax = hypot(xsize, ysize),
dr = 2*(rmax / height),
dth = (PI / width)
for y=0 to ysize-1 do
for x=0 to xsize-1 do
integer {r,g,b} = im_pixel(im, x, y)
if r!=255 then
for k=1 to width do
atom th = dth*(k-1),
r2 = (x*cos(th) + y*sin(th))
integer iry = (height/2 + floor(r2/dr + 0.5))+1,
cik = canvas[iry][k] - 1
if cik>=0 then
canvas[iry][k] = cik
end if
end for
end if
end for
end for
canvas = flatten(canvas) -- (needed by IupImage)
Ihandle new_img = IupImage(width, height, canvas)
for c=0 to 255 do
IupSetStrAttributeId(new_img,"",c,"%d %d %d",{c,c,c})
end for
return new_img
end function
<span style="color: #008080;">function</span> <span style="color: #000000;">hypot</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">*</span><span style="color: #000000;">a</span><span style="color: #0000FF;">+</span><span style="color: #000000;">b</span><span style="color: #0000FF;">*</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
IupOpen()
 
<span style="color: #008080;">function</span> <span style="color: #000000;">hough_transform</span><span style="color: #0000FF;">(</span><span style="color: #000000;">imImage</span> <span style="color: #000000;">im</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">width</span><span style="color: #0000FF;">=</span><span style="color: #000000;">460</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">height</span><span style="color: #0000FF;">=</span><span style="color: #000000;">360</span><span style="color: #0000FF;">)</span>
atom pError = allocate(machine_word())
<span style="color: #000000;">height</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">height</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
imImage im1 = imFileImageLoadBitmap("Pentagon320.png",0,pError)
<span style="color: #004080;">integer</span> <span style="color: #000000;">xsize</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">im_width</span><span style="color: #0000FF;">(</span><span style="color: #000000;">im</span><span style="color: #0000FF;">),</span>
if im1=NULL then ?"error opening Pentagon320.png" abort(0) end if
<span style="color: #000000;">ysize</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">im_height</span><span style="color: #0000FF;">(</span><span style="color: #000000;">im</span><span style="color: #0000FF;">)</span>
Ihandln image1 = IupImageFromImImage(im1),
<span style="color: #004080;">sequence</span> <span style="color: #000000;">canvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">255</span><span style="color: #0000FF;">,</span><span style="color: #000000;">width</span><span style="color: #0000FF;">),</span><span style="color: #000000;">height</span><span style="color: #0000FF;">)</span>
image2 = hough_transform(im1),
<span style="color: #004080;">atom</span> <span style="color: #000000;">rmax</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">hypot</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xsize</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ysize</span><span style="color: #0000FF;">),</span>
label1 = IupLabel(),
<span style="color: #000000;">dr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*(</span><span style="color: #000000;">rmax</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">height</span><span style="color: #0000FF;">),</span>
label2 = IupLabel()
<span style="color: #000000;">dth</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #004600;">PI</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">width</span><span style="color: #0000FF;">)</span>
IupSetAttributeHandle(label1, "IMAGE", image1)
<span style="color: #008080;">for</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">ysize</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
IupSetAttributeHandle(label2, "IMAGE", image2)
<span style="color: #008080;">for</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">xsize</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
 
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">g</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">im_pixel</span><span style="color: #0000FF;">(</span><span style="color: #000000;">im</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">)</span>
Ihandle dlg = IupDialog(IupHbox({label1, label2}))
<span style="color: #008080;">if</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">255</span> <span style="color: #008080;">then</span>
IupSetAttribute(dlg, "TITLE", "Hough transform")
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">width</span> <span style="color: #008080;">do</span>
IupShow(dlg)
<span style="color: #004080;">atom</span> <span style="color: #000000;">th</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">dth</span><span style="color: #0000FF;">*(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
 
<span style="color: #000000;">r2</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">th</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">th</span><span style="color: #0000FF;">))</span>
IupMainLoop()
<span style="color: #004080;">integer</span> <span style="color: #000000;">iry</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">height</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r2</span><span style="color: #0000FF;">/</span><span style="color: #000000;">dr</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">0.5</span><span style="color: #0000FF;">))+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span>
IupClose()</lang>
<span style="color: #000000;">cik</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">canvas</span><span style="color: #0000FF;">[</span><span style="color: #000000;">iry</span><span style="color: #0000FF;">][</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">cik</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">canvas</span><span style="color: #0000FF;">[</span><span style="color: #000000;">iry</span><span style="color: #0000FF;">][</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cik</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">canvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">flatten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (needed by IupImage)</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">new_img</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupImage</span><span style="color: #0000FF;">(</span><span style="color: #000000;">width</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">height</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">255</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">IupSetStrAttributeId</span><span style="color: #0000FF;">(</span><span style="color: #000000;">new_img</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d %d %d"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">new_img</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">pError</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">allocate</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">machine_word</span><span style="color: #0000FF;">())</span>
<span style="color: #000000;">imImage</span> <span style="color: #000000;">im1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">imFileImageLoadBitmap</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Pentagon320.png"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pError</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">im1</span><span style="color: #0000FF;">=</span><span style="color: #004600;">NULL</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #008000;">"error opening Pentagon320.png"</span> <span style="color: #7060A8;">abort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">Ihandln</span> <span style="color: #000000;">image1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">IupImageFromImImage</span><span style="color: #0000FF;">(</span><span style="color: #000000;">im1</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">image2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">hough_transform</span><span style="color: #0000FF;">(</span><span style="color: #000000;">im1</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">label1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupLabel</span><span style="color: #0000FF;">(),</span>
<span style="color: #000000;">label2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupLabel</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupSetAttributeHandle</span><span style="color: #0000FF;">(</span><span style="color: #000000;">label1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"IMAGE"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">image1</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetAttributeHandle</span><span style="color: #0000FF;">(</span><span style="color: #000000;">label2</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"IMAGE"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">image2</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">IupHbox</span><span style="color: #0000FF;">({</span><span style="color: #000000;">label1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">label2</span><span style="color: #0000FF;">}))</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Hough transform"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- (no chance...)</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</syntaxhighlight>-->
 
=={{header|Python}}==
{{libheader|PIL}}
This is the classical Hough transform as described in wikipedia. The code does not compute averages; it merely makes a point on the transformed image darker if a lot of points on the original image lie on the corresponding line. The output is almost identical to that of the Tcl code. The code works only with gray-scale images, but it is easy to extend to RGB.
<langsyntaxhighlight lang="python">
from math import hypot, pi, cos, sin
from PIL import Image
Line 847 ⟶ 852:
if __name__ == "__main__": test()
 
</syntaxhighlight>
</lang>
 
{{omit from|PARI/GP}}
Line 858 ⟶ 863:
The <code>GD</code> module the output palette to 255 colors, so only transform darker pixels in the image.
{{trans|Perl}}
<syntaxhighlight lang="raku" perl6line>use GD;
 
my $filename = 'pentagon.ppm';
Line 883 ⟶ 888:
my $y = floor $pixel / $xsize;
 
(0..^$width).race.map: -> $k {
my $th = $dth*$k;
my $r = ($x*cos($th) + $y*sin($th));
Line 895 ⟶ 900:
my $png_fh = $image.open("hough-transform.png", "wb");
$image.output($png_fh, GD_PNG);
$png_fh.close;</langsyntaxhighlight>
See [https://github.com/thundergnat/rc/blob/master/img/hough-transform.png Hough Transform] (offsite .png image)
 
=={{header|Ruby}}==
 
<syntaxhighlight lang="ruby">
<lang Ruby>
require 'mathn'
require 'rubygems'
Line 934 ⟶ 939:
end
out
end</langsyntaxhighlight>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
//! Contributed by Gavin Baker <gavinb@antonym.org>
//! Adapted from the Go version
Line 1,077 ⟶ 1,082:
}
 
</syntaxhighlight>
</lang>
 
 
=={{header|Scala}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="scala">import java.awt.image._
import java.io.File
import javax.imageio._
Line 1,164 ⟶ 1,169:
 
private val dataArray = Array.ofDim[Int](width, height)
}</langsyntaxhighlight>
 
=={{header|SequenceL}}==
{{trans|Java}}
'''Tail-Recursive SequenceL Code:'''<br>
<langsyntaxhighlight lang="sequencel">import <Utilities/Sequence.sl>;
import <Utilities/Math.sl>;
 
Line 1,213 ⟶ 1,218:
j within x-1 ... x+1;
in
some(some(abs(image[y,x] - neighbors) >= minContrast));</langsyntaxhighlight>
 
'''C++ Driver Code:'''<br>
{{libheader|CImg}}
<langsyntaxhighlight lang="c">#include "SL_Generated.h"
#include "CImg.h"
 
Line 1,255 ⟶ 1,260:
sl_done();
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 1,262 ⟶ 1,267:
=={{header|Sidef}}==
{{trans|Python}}
<langsyntaxhighlight lang="ruby">require('Imager')
 
func hough(im, width=460, height=360) {
Line 1,297 ⟶ 1,302:
var img = %s|Imager|.new(file => 'Pentagon.png')
var ht = hough(img)
ht.write(file => 'Hough transform.png')</langsyntaxhighlight>
 
=={{header|Tcl}}==
Line 1,303 ⟶ 1,308:
 
* See [[Example:Hough transform/Tcl]]
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|DOME}}
<syntaxhighlight lang="wren">import "graphics" for Canvas, Color, ImageData
import "dome" for Window, Process
import "math" for Math
 
var Hypot = Fn.new { |x, y| (x*x + y*y).sqrt }
 
class ArrayData {
construct new(width, height) {
_width = width
_height = height
_dataArray = List.filled(width * height, 0)
}
 
width { _width }
height { _height }
 
[x, y] { _dataArray[y * _width + x] }
 
[x, y]=(v) { _dataArray[y * _width + x] = v }
 
transform(thetaAxisSize, rAxisSize, minContrast) {
var maxRadius = Math.ceil(Hypot.call(_width, _height))
var halfRAxisSize = rAxisSize >> 1
var outputData = ArrayData.new(thetaAxisSize, rAxisSize)
// x output ranges from 0 to pi
// y output ranges from -maxRadius to maxRadius
var sinTable = List.filled(thetaAxisSize, 0)
var cosTable = List.filled(thetaAxisSize, 0)
for (theta in thetaAxisSize - 1..0) {
var thetaRadians = theta * Num.pi / thetaAxisSize
sinTable[theta] = Math.sin(thetaRadians)
cosTable[theta] = Math.cos(thetaRadians)
}
for (y in _height - 1..0) {
for (x in _width - 1..0) {
if (contrast(x, y, minContrast)) {
for (theta in thetaAxisSize - 1..0) {
var r = cosTable[theta] * x + sinTable[theta] * y
var rScaled = Math.round(r * halfRAxisSize / maxRadius) + halfRAxisSize
outputData.accumulate(theta, rScaled, 1)
}
}
}
}
return outputData
}
 
accumulate(x, y, delta) { this[x, y] = this[x, y] + delta }
 
contrast(x, y, minContrast) {
var centerValue = this[x, y]
for (i in 8..0) {
if (i != 4) {
var newx = x + i % 3 - 1
var newy = y + (i / 3).truncate - 1
if (newx >= 0 && newx < width && newy >= 0 && newy < height &&
Math.abs(this[newx, newy] - centerValue) >= minContrast) return true
}
}
return false
}
 
max {
var max = _dataArray[0]
for (i in width * height - 1..1) {
if (_dataArray[i] > max) max = _dataArray[i]
}
return max
}
}
 
class HoughTransform {
construct new(inFile, outFile, width, height, minCont) {
Window.title = "Hough Transform"
Window.resize(width, height)
Canvas.resize(width, height)
_width = width
_height = height
_inFile = inFile
_outFile = outFile
_minCont = minCont
}
 
init() {
var dataArray = readInputFromImage(_inFile)
dataArray = dataArray.transform(_width, _height, _minCont)
writeOutputImage(_outFile, dataArray)
}
 
readInputFromImage(filename) {
var inputImage = ImageData.load(filename)
var width = inputImage.width
var height = inputImage.height
var rgbData = []
for (y in 0...height) {
for (x in 0...width) rgbData.add(inputImage.pget(x, y))
}
var arrayData = ArrayData.new(width, height)
// Flip y axis when reading image
for (y in 0...height) {
for (x in 0...width) {
var rgbValue = rgbData[y * width + x]
rgbValue = (rgbValue.r * 0.3 + rgbValue.g * 0.59 + rgbValue.b * 0.11).floor
arrayData[x, height - 1 - y] = rgbValue
}
}
return arrayData
}
 
writeOutputImage(filename, arrayData) {
var max = arrayData.max
var outputImage = ImageData.create(filename, arrayData.width, arrayData.height)
for (y in 0...arrayData.height) {
for (x in 0...arrayData.width) {
var n = Math.min(Math.round(arrayData[x, y] * 255 / max), 255)
var c = Color.new(n, n, 0x90)
outputImage.pset(x, arrayData.height - 1 - y, c)
}
}
outputImage.draw(0, 0)
outputImage.saveToFile(filename)
}
 
update() {}
 
draw(alpha) {}
}
 
var args = Process.args
System.print(args)
if (args.count != 7) Fiber.abort("There should be exactly 5 command line arguments.")
var inFile = args[2]
var outFile = args[3]
var width = Num.fromString(args[4])
var height = Num.fromString(args[5])
var minCont = Num.fromString(args[6])
var Game = HoughTransform.new(inFile, outFile, width, height, minCont)</syntaxhighlight>
 
{{out}}
<pre>
When called with: 'dome hough_transform.wren Pentagon.png Pentagon2.png 640 480 100' the resulting image is similar to that of the Java entry.
</pre>
 
=={{header|zkl}}==
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
{{trans|D}}
<langsyntaxhighlight lang="zkl">const WHITE=0xffFFff, X=0x010101;
fcn houghTransform(image,hx=460,hy=360){
if(hy.isOdd) hy-=1; // hy argument must be even
Line 1,323 ⟶ 1,474:
}
out
}</langsyntaxhighlight>
 
<langsyntaxhighlight lang="zkl">fcn readPNG2PPM(fileName){
p:=System.popen("convert \"%s\" ppm:-".fmt(fileName),"r");
img:=PPM.readPPM(p);
Line 1,332 ⟶ 1,483:
}
houghTransform(readPNG2PPM("pentagon.png"))
.write(File("pentagon_hough.ppm","wb"));</langsyntaxhighlight>
{{out}}
The output image looks the same as in the Go solution.
9,476

edits