Hough transform: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: added syntax colouring, marked p2js incompatible)
m (syntax highlighting fixup automation)
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}}
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Hough_transform.exw</span>
<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,
Line 809:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
 
=={{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 852:
if __name__ == "__main__": test()
 
</syntaxhighlight>
</lang>
 
{{omit from|PARI/GP}}
Line 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 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 939:
end
out
end</langsyntaxhighlight>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
//! Contributed by Gavin Baker <gavinb@antonym.org>
//! Adapted from the Go version
Line 1,082:
}
 
</syntaxhighlight>
</lang>
 
 
=={{header|Scala}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="scala">import java.awt.image._
import java.io.File
import javax.imageio._
Line 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,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,260:
sl_done();
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 1,267:
=={{header|Sidef}}==
{{trans|Python}}
<langsyntaxhighlight lang="ruby">require('Imager')
 
func hough(im, width=460, height=360) {
Line 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,312:
{{trans|Kotlin}}
{{libheader|DOME}}
<langsyntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color, ImageData
import "dome" for Window, Process
import "math" for Math
Line 1,448:
var height = Num.fromString(args[5])
var minCont = Num.fromString(args[6])
var Game = HoughTransform.new(inFile, outFile, width, height, minCont)</langsyntaxhighlight>
 
{{out}}
Line 1,458:
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,474:
}
out
}</langsyntaxhighlight>
 
<langsyntaxhighlight lang="zkl">fcn readPNG2PPM(fileName){
p:=System.popen("convert \"%s\" ppm:-".fmt(fileName),"r");
img:=PPM.readPPM(p);
Line 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.
10,333

edits