Tech Tips

  1. 要素技術
  2. 684 view

[Haskell]画像処理 水平方向のエッジ

Contents

やりたいこと

水平方向の微分で水平方向のエッジ画像を出力する。

プログラム

プログラムはこんな感じ。
import System.IO
import System.Environment (getArgs)

-- Calculation with difference between previous value
calcEdge :: [Int] -> [Int]
calcEdge [] = []
calcEdge (x:y:zs) = if (x-y) < 0 then (0 : (calcEdge (y:zs))) else ((x-y) : (calcEdge (y:zs)))
calcEdge (x:xs) = x : xs

main = do
    args <- getArgs
    if length args <= 1 then do
        print "usage : ./pgm-input input.pgm output.pgm"
    else do
        infile <- openFile (head args) ReadMode

        -- Read PGM Header
        pgm_type <- hGetLine infile
        pgm_comment <- hGetLine infile
        pgm_max_brightness <- hGetLine infile
        pgm_size <- hGetLine infile
        -- Read PGM Pixels-
        pixels_handle <- hGetContents infile
        let src_contents = take (length pixels_handle) $ pixels_handle
        let src_contents_lines = lines src_contents
        let src_pixels = concat (map (\x -> words x) src_contents_lines)
        let int_pixels = map (\x -> read x::Int) src_pixels
        let dist_pixels = calcEdge int_pixels

        -- Output PGM File
        fh <- openFile (args !! 1) WriteMode
        hPutStrLn fh pgm_type
        hPutStrLn fh pgm_comment
        hPutStrLn fh pgm_max_brightness
        hPutStrLn fh pgm_size
        hPutStr fh ((foldr (\x xs -> show(x) ++ " " ++ xs ) "") dist_pixels)

        hClose infile
        hClose fh

結果

入力画像

car-out

出力画像

car-out

要素技術の最近記事

  1. Udacity Self-Driving Car Engineer Nanodegree …

  2. TF-Agents で独自の環境を利用する方法

  3. Amazon Rekognition Video を使って動画内の顔検出と境界ボックスの描…

  4. [触ってみた]Microsoft Quantum Development Kit

  5. Tensorboardを使ってニューラルネットワークと学習の状況を可視化する

関連記事

PAGE TOP