(notonline)-->
-- demo\rosetta\Hough_transform.exw
without js -- IupImage, imImage, im_width/height/pixel, allocate,
-- imFileImageLoadBitmap, IupImageFromImImage
include pGUI.e
function hypot(atom a,b) return sqrt(a*a+b*b) end function
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
IupOpen()
atom pError = allocate(machine_word())
imImage im1 = imFileImageLoadBitmap("Pentagon320.png",0,pError)
if im1=NULL then ?"error opening Pentagon320.png" abort(0) end if
Ihandln image1 = IupImageFromImImage(im1),
image2 = hough_transform(im1),
label1 = IupLabel(),
label2 = IupLabel()
IupSetAttributeHandle(label1, "IMAGE", image1)
IupSetAttributeHandle(label2, "IMAGE", image2)
Ihandle dlg = IupDialog(IupHbox({label1, label2}))
IupSetAttribute(dlg, "TITLE", "Hough transform")
IupShow(dlg)
if platform()!=JS then -- (no chance...)
IupMainLoop()
IupClose()
end if