首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何利用HIP将图像类型的精度从双重转换为Word8?

如何利用HIP将图像类型的精度从双重转换为Word8?
EN

Stack Overflow用户
提问于 2020-06-11 18:09:10
回答 1查看 135关注 0票数 2

我有一个Image VS Y Double类型的图像(在使用了HIP库中的函数readImageY之后),我想将它转换为Image VS Y Word8。我该怎么做呢?我需要它在这个精度,为下一个函数,我要应用这个。

下面是相关代码的片段:

代码语言:javascript
复制
import Codec.Picture         
import Codec.Picture.Types
import Control.Arrow
import Data.Ratio 
import Data.Monoid
import Graphics.Image.Processing
import qualified Graphics.Image as I
import Graphics.Image.IO
import Graphics.Image.IO.Formats
import Graphics.Image.Interface.Vector
import qualified Graphics.Image.Interface as Interface
import Data.Word (Word8)
import qualified Data.Matrix as M
import System.FilePath.Posix (splitExtension)

to2DMatrix :: FilePath -> FilePath -> Border(Interface.Pixel I.Y Word8) -> (Int, Int) -> IO ()
to2DMatrix fp fpout bor (dim1, dim2)= do
    eimg <- I.readImageY VS fp
    let new_res :: Interface.Image VS I.Y Word8
        new_res = I.resize Bilinear bor (dim1, dim2) eimg 
    let rle = twoDToMatrix $ pixelToInt $ toJPImageY8 new_res
    let (name, _) = splitExtension fp
    writeFile (name ++ ".txt") (show rle)
    writeImage fpout rle

这是一个错误:

代码语言:javascript
复制
Couldn't match type ‘Double’ with ‘Word8’
      Expected type: Interface.Image VS I.Y Word8
        Actual type: Interface.Image VS I.Y Double
    • In the fourth argument of ‘resize’, namely ‘eimg’
      In the expression: resize Bilinear bor (dim1, dim2) eimg
      In an equation for ‘new_res’:
          new_res = resize Bilinear bor (dim1, dim2) eimg
   |
29 |         new_res = I.resize Bilinear bor (dim1, dim2) eimg
   |                                                      ^^^^

编辑:是灰度中的像素值,存储为图像类型中的VS向量类型。我遇到的问题是如何获得双打,以便能够转换它们。试图解释/找到一种方式的HIP库这里,但我是新的哈斯克尔,并没有弄清楚。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-12 05:25:09

在(HIP)库中的Graphics.Image.Interface中有一个类Graphics.Image.Interface,它允许您将像素精度从/更改为几个精度类型。

下面是更改代码的代码片段:

代码语言:javascript
复制
to2DMatrix :: FilePath  -> (Int, Int) -> IO (Maybe (M.Matrix Int)) 
to2DMatrix fp (dim1, dim2)= do
    eimg <- I.readImageY VS fp 
    let new_res :: Interface.Image VS I.Y Word8
        new_res = I.resize Bilinear Edge  (dim1, dim2) $ Interface.map conv eimg
    let rle = twoDToMatrix $ pixelToInt $ toJPImageY8 new_res
    return $ Just (rle)

conv :: Pixel Y Double -> Pixel Y Word8 
conv d = toWord8 <$> d

<$>fmap的infix运算符版本。因此,这与:

代码语言:javascript
复制
conv :: Pixel Y Double -> Pixel Y Word8 
conv d = fmap toWord8 d

将输入图像转换为矩阵的额外详细信息:

pixelToIntPixel8类型更改为[[Int]]

代码语言:javascript
复制
pixelToInt :: Image Pixel8 -> [[Int]]
pixelToInt =
  map reverse . reverse .  snd . pixelFold 
    (\(lastY, ps:pss) x y p ->           
      if y == lastY                        
        then (y, (fromIntegral p:ps):pss)
        else (y, [fromIntegral p]:ps:pss))
    (0,[[]])

然后,您可以根据需要使用Data.Matrix从[[Int]]更改为Data.Matrix矩阵类型。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62330791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档