Hough transform: Difference between revisions
→{{header|Haskell}}: Adjusted some names to avoid a wiki formatting glitch (+ hlint, hindent)
m (→{{header|J}}: Update for J8) |
(→{{header|Haskell}}: Adjusted some names to avoid a wiki formatting glitch (+ hlint, hindent)) |
||
Line 198:
-- Library JuicyPixels:
import Codec.Picture
(DynamicImage(ImageRGB8, ImageRGBA8),
import Codec.Picture.Types (extractLumaPlane, dropTransparency)
dot
dot :: Num a => (a, a) -> (a, a) -> a▼
:: Num a
dot (x1, y1) (x2, y2) = x1 * x2 + y1 * y2
mag
mag :: Floating a => (a, a) -> a▼
:: Floating a
mag a = sqrt $ dot a a
sub
sub :: Num a => (a, a) -> (a, a) -> (a, a)▼
:: Num a
sub (x1, y1) (x2, y2) = (x1 - x2, y1 - y2)
fromIntegralP
:: (Integral a, Num b)
=> (a, a) -> (b, b)
fromIntegralP (x, y) = (fromIntegral x, fromIntegral y)
Line 232 ⟶ 240:
xCenter = wMax `div` 2
yCenter = hMax `div` 2
lumaMap = extractLumaPlane image
gradient x y =
let orig = pixelAt lumaMap x y
in fromIntegralP (orig -
gradMap =
| x <- [0 .. wMax]
, y <- [0 .. hMax] ]
-- The longest distance from the center, half the hypotenuse of the image.
distMax :: Double
distMax = (sqrt . fromIntegral $ height ^ 2 + width ^ 2) / 2
{-
The accumulation bins of the polar values.
Line 253 ⟶ 259:
lines that go through that point in Hough space.
-}
accBin =
runSTArray $
forM_ gradMap $
let (x', y') = fromIntegralP $ (xCenter, yCenter) `sub` (x, y)▼
\((x, y), grad) -> do
\theta ->
dist = cos theta_ * x_ + sin theta_ * y_
▲ do old <- readArray arr idx
return arr
maxAcc = F.maximum accBin
-- The image representation of the accumulation bins.
hTransform x y =
let l = 255 -
in PixelRGB8 l l l
hImage = generateImage hTransform thetaSize distSize
Line 285 ⟶ 289:
image <- readImage path
case image of
Left err
Right (ImageRGB8
Right (ImageRGBA8
_
where
doImage image = do
Line 301 ⟶ 305:
[path, outpath, thetaSize, distSize] ->
houghIO path outpath (read thetaSize) (read distSize)
▲ _ ->
_ -> putStrLn $ "Usage: " ++ prog ++ " <image-file> <out-file.png> <width> <height>"</lang>▼
'''Example use:'''
<lang>HoughTransform Pentagon.png hough.png 360 360</lang>
|