Hough transform: Difference between revisions

Content added Content deleted
(Added Sidef)
(Adding SequenceL)
Line 789: Line 789:
var ht = hough(img)
var ht = hough(img)
ht.write(file => 'Hough transform.png')</lang>
ht.write(file => 'Hough transform.png')</lang>


=={{header|SequenceL}}==
{{trans|Java}}
'''Tail-Recursive SequenceL Code:'''<br>
<lang sequencel>import <Utilities/Sequence.sl>;
import <Utilities/Math.sl>;

hough: int(2) * int * int * int -> int(2);
hough(image(2), thetaAxisSize, rAxisSize, minContrast) :=
let
initialResult[r,theta] := 0 foreach r within 1 ... rAxisSize, theta within 1 ... thetaAxisSize;
result := houghHelper(image, minContrast, 1, 1, initialResult);
max := vectorMax(vectorMax(result));
in
255 - min(round((result * 255 / max)), 255);

houghHelper(image(2), minContrast, x, y, result(2)) :=
let
thetaAxisSize := size(head(result));
rAxisSize := size(result);
width := size(head(image));
height := size(image);
maxRadius := ceiling(sqrt(width^2 + height^2));
halfRAxisSize := rAxisSize / 2;
rs[theta] := round((cos(theta) * x + sin(theta) * y) * halfRAxisSize / maxRadius) + halfRAxisSize
foreach theta within (0 ... (thetaAxisSize-1)) * pi / thetaAxisSize;
newResult[r,theta] := result[r,theta] + 1 when rs[theta] = r-1 else result[r,theta];
nextResult := result when not checkContrast(image, x, y, minContrast) else newResult;
nextX := 1 when x = width else x + 1;
nextY := y + 1 when x = width else y;
in
nextResult when x = width and y = height
else
houghHelper(image, minContrast, nextX, nextY, nextResult);
checkContrast(image(2), x, y, minContrast) :=
let
neighbors[i,j] := image[i,j] when i > 0 and i < size(image) and j > 0 and j < size(image[i])
foreach i within y-1 ... y+1,
j within x-1 ... x+1;
in
some(some(abs(image[y,x] - neighbors) >= minContrast));</lang>

'''C++ Driver Code:'''<br>
{{libheader|CImg}}
<lang c>#include "SL_Generated.h"
#include "CImg.h"

using namespace cimg_library;

int main( int argc, char** argv )
{
string fileName = "Pentagon.bmp";
if(argc > 1) fileName = argv[1];
int thetaAxisSize = 640; if(argc > 2) thetaAxisSize = atoi(argv[2]);
int rAxisSize = 480; if(argc > 3) rAxisSize = atoi(argv[3]);
int minContrast = 64; if(argc > 4) minContrast = atoi(argv[4]);
int threads = 0; if(argc > 5) threads = atoi(argv[5]);
char titleBuffer[200];
SLTimer t;

CImg<int> image(fileName.c_str());
int imageDimensions[] = {image.height(), image.width(), 0};
Sequence<Sequence<int> > imageSeq((void*) image.data(), imageDimensions);
Sequence< Sequence<int> > result;

sl_init(threads);

t.start();
sl_hough(imageSeq, thetaAxisSize, rAxisSize, minContrast, threads, result);
t.stop();
CImg<int> resultImage(result[1].size(), result.size());
for(int y = 0; y < result.size(); y++)
for(int x = 0; x < result[y+1].size(); x++)
resultImage(x,result.size() - 1 - y) = result[y+1][x+1];
sprintf(titleBuffer, "SequenceL Hough Transformation: %d X %d Image to %d X %d Result | %d Cores | Processed in %f sec\0",
image.width(), image.height(), resultImage.width(), resultImage.height(), threads, t.getTime());
resultImage.display(titleBuffer);

sl_done();
return 0;
}</lang>

{{out}}
[http://i.imgur.com/McCuZP3.png Output Screenshot]


=={{header|Tcl}}==
=={{header|Tcl}}==