Image 从图像文件读取像素颜色的打印数据重建
如何在Haskell中打开和读取图像文件特定像素的颜色?您推荐哪些软件包、功能 您可以查看下面引用的绘图和重建的数据,了解我想自动化的内容。我用Gimp处理这个特殊的图形,并手动在线条上标记点 如果你不能用Haskell的例子来回答这个问题,但是你知道有一个好的软件可以自动处理这种类型的重建工作,请告诉我他们的名字 致以最良好的祝愿, 塞汀塞特 更新:现在有一个跨平台的Haskell软件包:Image 从图像文件读取像素颜色的打印数据重建,image,haskell,colors,plot,pixel,Image,Haskell,Colors,Plot,Pixel,如何在Haskell中打开和读取图像文件特定像素的颜色?您推荐哪些软件包、功能 您可以查看下面引用的绘图和重建的数据,了解我想自动化的内容。我用Gimp处理这个特殊的图形,并手动在线条上标记点 如果你不能用Haskell的例子来回答这个问题,但是你知道有一个好的软件可以自动处理这种类型的重建工作,请告诉我他们的名字 致以最良好的祝愿, 塞汀塞特 更新:现在有一个跨平台的Haskell软件包: (来源:) 表中从上到下是从左到右 如图所示 可以使用和编写一些简单的扫描仪: module Main
(来源:) 表中从上到下是从左到右 如图所示 可以使用和编写一些简单的扫描仪:
module Main where
import System.Environment
import System.IO.Unsafe
import System.Exit
import Data.Word
import Foreign.Ptr
import Foreign.Storable
import Data.Array.Storable
import Control.Monad
import Control.Applicative
import Codec.Image.PNG
type Name = String
type Color = RGBA
data RGBA = RGBA Word8 Word8 Word8 Word8 deriving (Show, Read, Eq)
instance Storable RGBA where
sizeOf _ = sizeOf (0 :: Word8) * 4
alignment _ = 1
poke color (RGBA r g b a) = do
let byte :: Ptr Word8 = castPtr color
pokeElemOff byte 0 r
pokeElemOff byte 1 g
pokeElemOff byte 2 b
pokeElemOff byte 3 a
peek color = do
let byte :: Ptr Word8 = castPtr color
r <- peekElemOff byte 0
g <- peekElemOff byte 1
b <- peekElemOff byte 2
a <- peekElemOff byte 3
return $ RGBA r g b a
--
checkForAlpha :: PNGImage -> IO ()
checkForAlpha (hasAlphaChannel -> True) = return ()
checkForAlpha (hasAlphaChannel -> _ ) = putStrLn "no alpha channel going on!!!" >> exitWith (ExitFailure 1)
--
main :: IO ()
main = do
putStrLn $ "ExPloRe 0.0 : Experimental Plot Reconstructor"
args@(path:legend_:tr_:tg_:tb_:ta_:start_:step_:_) <- getArgs
-- initialize image
Right img <- loadPNGFile path
let bitmap = imageData img
let (wu,hu) = dimensions img
let (w,h) = (fromIntegral wu, fromIntegral hu)
putStrLn $ "-------------------------------------------------------------------"
putStrLn $ ""
putStrLn $ "call : " ++ tail (filter (/= '"') $ concatMap ((' ':) . show) args)
putStrLn $ ""
putStrLn $ "image : " ++ path
putStrLn $ "legend: " ++ legend_
putStrLn $ ""
putStrLn $ "width : " ++ show w
putStrLn $ "height: " ++ show h
checkForAlpha img -- !!
-- initialize lines
let [tr,tg,tb,ta] = map read [tr_,tg_,tb_,ta_] :: [Int]
mapM_ (\(n,v) -> putStrLn $ show n ++ " ~ : " ++ show v) $ zip "rgba" [tr,tg,tb,ta]
lines_ <- readFile legend_
let lines = read lines_ :: [(Name,Color)]
putStrLn $ "lines : " ++ (show $ length lines)
putStrLn $ ""
mapM_ (putStrLn . show) lines
-- initialize scan
let (@#) = mu w
let start = read start_ :: Double
let step = read step_ :: Double
let rows = [0..h]
let cols = takeWhile (< w) $ map (floor . (start +) . (step *)) [0..]
let icols = zip [1..] cols
-- scan bitmap
let (~=) = mcc tr tg tb ta
mapM_ (scan bitmap icols rows (@#) (~=)) lines
--
scan bitmap icols rows (@#) (~=) (name,color) = do
putStrLn $ ""
putStrLn $ "-------------------------------------------------------------------"
putStrLn $ show color
putStrLn $ ""
putStrLn $ name
putStrLn $ ""
withStorableArray bitmap $ \byte -> do
let pixel :: Ptr RGBA = castPtr byte
forM_ icols $ \(n,j) -> do
let matches = flip filter rows $ \i -> (pixel @# i) j ~= color
let m = median matches
putStrLn $ case not . null $ matches of
True -> show n ++ "\t" ++ show j ++ "\t" ++ show m ++ "\t" ++ show matches
False -> show n ++ "\t" ++ show j ++ "\t \t[]"
--
cb t x y = (abs $ (fromIntegral x) - (fromIntegral y)) < t
mcc :: Int -> Int -> Int -> Int -> RGBA -> RGBA -> Bool
mcc tr tg tb ta (RGBA a b c d) (RGBA x y z w) =
cb tr a x && cb tg b y && cb tb c z && cb ta d w
median :: [a] -> a
median xs = xs !! (fromIntegral . floor . (/ 2) . fromIntegral . length) xs
(@!) :: Storable a => Ptr a -> Int -> IO a
(@!) = peekElemOff
mu :: Storable a => Int -> Ptr a -> Int -> Int -> a
mu w p j i = unsafePerformIO $ p @! (i + j * w)
modulemain其中
导入系统。环境
导入System.IO不安全
导入系统。退出
导入数据.Word
进口外国货
进口国外。可储存
导入Data.Array.Storable
进口管制
导入控制
导入Codec.Image.PNG
类型名称=字符串
类型颜色=RGBA
数据RGBA=RGBA字8字8字8字8字派生(显示、读取、相等)
实例可存储RGBA,其中
sizeOf u=sizeOf(0::Word8)*4
对齐度=1
戳颜色(RGBA r g b a)=do
let byte::Ptr Word8=castpr color
pokeElemOff字节0 r
pokeElemOff字节1 g
pokeElemOff字节2 b
pokeElemOff字节3 a
peek color=do
let byte::Ptr Word8=castpr color
r>exitWith(ExitFailure 1)
--
main::IO()
main=do
putStrLn$“探索0.0:实验地块重建器”
args@(路径:图例\:tr \:tg \:tb \:ta \:start \:step \:do)do
让matches=flip filter rows$\i->(pixel@#i)j~=color
设m=中位数匹配
putStrLn$情况并非如此。的空$匹配项
True->显示n++“\t”+++显示j++”\t“+++显示m++”\t“+++显示匹配项
False->show n++“\t”+++show j++”\t\t[]
--
cb t x y=(abs$(从积分x)-(从积分y))Int->Int->Int->RGBA->RGBA->Bool
mcc tr tg tb ta(RGBA a b c d)(RGBA x y z w)=
cb tr a x和cb tg b y和cb tb c z和cb ta d w
中位数::[a]->a
中位数xs=xs!!(fromIntegral.floor.(/2).fromIntegral.length)xs
(@!)::可存储a=>Ptr a->Int->IO a
(@!)=peekElemOff
mu::可存储a=>Int->Ptr a->Int->Int->a
mu w p j i=不安全性能$p@!(i+j*w)
module Main where
import System.Environment
import System.IO.Unsafe
import System.Exit
import Data.Word
import Foreign.Ptr
import Foreign.Storable
import Data.Array.Storable
import Control.Monad
import Control.Applicative
import Codec.Image.PNG
type Name = String
type Color = RGBA
data RGBA = RGBA Word8 Word8 Word8 Word8 deriving (Show, Read, Eq)
instance Storable RGBA where
sizeOf _ = sizeOf (0 :: Word8) * 4
alignment _ = 1
poke color (RGBA r g b a) = do
let byte :: Ptr Word8 = castPtr color
pokeElemOff byte 0 r
pokeElemOff byte 1 g
pokeElemOff byte 2 b
pokeElemOff byte 3 a
peek color = do
let byte :: Ptr Word8 = castPtr color
r <- peekElemOff byte 0
g <- peekElemOff byte 1
b <- peekElemOff byte 2
a <- peekElemOff byte 3
return $ RGBA r g b a
--
checkForAlpha :: PNGImage -> IO ()
checkForAlpha (hasAlphaChannel -> True) = return ()
checkForAlpha (hasAlphaChannel -> _ ) = putStrLn "no alpha channel going on!!!" >> exitWith (ExitFailure 1)
--
main :: IO ()
main = do
putStrLn $ "ExPloRe 0.0 : Experimental Plot Reconstructor"
args@(path:legend_:tr_:tg_:tb_:ta_:start_:step_:_) <- getArgs
-- initialize image
Right img <- loadPNGFile path
let bitmap = imageData img
let (wu,hu) = dimensions img
let (w,h) = (fromIntegral wu, fromIntegral hu)
putStrLn $ "-------------------------------------------------------------------"
putStrLn $ ""
putStrLn $ "call : " ++ tail (filter (/= '"') $ concatMap ((' ':) . show) args)
putStrLn $ ""
putStrLn $ "image : " ++ path
putStrLn $ "legend: " ++ legend_
putStrLn $ ""
putStrLn $ "width : " ++ show w
putStrLn $ "height: " ++ show h
checkForAlpha img -- !!
-- initialize lines
let [tr,tg,tb,ta] = map read [tr_,tg_,tb_,ta_] :: [Int]
mapM_ (\(n,v) -> putStrLn $ show n ++ " ~ : " ++ show v) $ zip "rgba" [tr,tg,tb,ta]
lines_ <- readFile legend_
let lines = read lines_ :: [(Name,Color)]
putStrLn $ "lines : " ++ (show $ length lines)
putStrLn $ ""
mapM_ (putStrLn . show) lines
-- initialize scan
let (@#) = mu w
let start = read start_ :: Double
let step = read step_ :: Double
let rows = [0..h]
let cols = takeWhile (< w) $ map (floor . (start +) . (step *)) [0..]
let icols = zip [1..] cols
-- scan bitmap
let (~=) = mcc tr tg tb ta
mapM_ (scan bitmap icols rows (@#) (~=)) lines
--
scan bitmap icols rows (@#) (~=) (name,color) = do
putStrLn $ ""
putStrLn $ "-------------------------------------------------------------------"
putStrLn $ show color
putStrLn $ ""
putStrLn $ name
putStrLn $ ""
withStorableArray bitmap $ \byte -> do
let pixel :: Ptr RGBA = castPtr byte
forM_ icols $ \(n,j) -> do
let matches = flip filter rows $ \i -> (pixel @# i) j ~= color
let m = median matches
putStrLn $ case not . null $ matches of
True -> show n ++ "\t" ++ show j ++ "\t" ++ show m ++ "\t" ++ show matches
False -> show n ++ "\t" ++ show j ++ "\t \t[]"
--
cb t x y = (abs $ (fromIntegral x) - (fromIntegral y)) < t
mcc :: Int -> Int -> Int -> Int -> RGBA -> RGBA -> Bool
mcc tr tg tb ta (RGBA a b c d) (RGBA x y z w) =
cb tr a x && cb tg b y && cb tb c z && cb ta d w
median :: [a] -> a
median xs = xs !! (fromIntegral . floor . (/ 2) . fromIntegral . length) xs
(@!) :: Storable a => Ptr a -> Int -> IO a
(@!) = peekElemOff
mu :: Storable a => Int -> Ptr a -> Int -> Int -> a
mu w p j i = unsafePerformIO $ p @! (i + j * w)