This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,40 @@
import std.math, grayscale_image;
Image!Gray houghTransform(in Image!Gray im,
in size_t hx=460, in size_t hy=360)
/*pure nothrow*/ in {
assert(im !is null);
assert(hx > 0 && hy > 0);
assert((hy & 1) == 0, "hy argument must be even.");
} body {
auto result = new Image!Gray(hx, hy);
result.clear(Gray.white);
immutable double rMax = hypot(im.nx, im.ny);
immutable double dr = rMax / (hy / 2.0);
immutable double dTh = PI / hx;
foreach (immutable y; 0 .. im.ny) {
foreach (immutable x; 0 .. im.nx) {
// if (im[x, y] == Gray.max) // Not pure.
if (im[x, y] == Gray(255))
continue;
foreach (immutable iTh; 0 .. hx) {
immutable double th = dTh * iTh;
immutable double r = x * cos(th) + y * sin(th);
immutable iry = hy / 2 - cast(int)floor(r / dr + 0.5);
if (result[iTh, iry] > Gray(0))
result[iTh, iry]--;
}
}
}
return result;
}
void main() {
auto im = new Image!RGB;
loadPPM6(im, "Pentagon.ppm")
.rgb2grayImage()
.houghTransform()
.savePGM("Pentagon_hough.pgm");
}