旋转图像.pbm Haskell [英] Rotate Image .pbm Haskell

查看:117
本文介绍了旋转图像.pbm Haskell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要关于haskell中的旋转或旋转矩阵的帮助

我有一个数据类型为RGB的列表列表:

  data RGBdata = RGB Int Int Int 

m = [[(RGB 0 255 255),(RGB 255 0 0)],[ (RGB 255 255 255),(RGB 255 0 0)]]

矩阵2x2:

  m = [[(RGB 1 2 3),(RGB 4 5 6)],
[(RGB 7 8 9),(RGB 1 5 9)]]

°旋转,我的意思是类似于:

  m = [[(RGB 7 8 9),(RGB 1 2 3)] 
[(RGB 1 5 9),(RGB 4 5 6)]]

我的解释是,我有2个数据类型:

pre $ data $ RGB数据RGB数据= RGB Int Int Int
数据PBMfile = PBM Int Int [[RGBdata]]

和我的函数收到:

  spin :: PBMfile  - > PBM文件
spin(PBM xyl)=(PBM xy((转置。反转)l))

其中'x'和'y'分别是柱子和柱子的数量(也许可以帮助做功能)。



我试着旋转90°到


$ b 我试试

  spin :: PBMfile  - > PBMfile 
spin(PBM xyl)=(PBM xy((reverse。transpose)l))

  spin :: PBMfile  - > PBM文件
spin(PBM xyl)=(PBM xy((转置。反转)l))

  spin :: PBMfile  - > PBMfile 
spin(PBM xyl)=(PBM xy(((map reverse)。)transpose)l))

旋转图片,但不起作用。



结果类似于

< a href =http://imageshack.us/photo/my-images/52/catmc.jpg/ =nofollow> http://imageshack.us/photo/my-images/52/catmc.jpg /

解决方案

TL; DR: transpose 反向
$ b

如果您打算使用列表来存储大图像,请记住它可能效率低下,因为有五个盒装值(a每个像素都有很多字节)。使用取消装箱的Vector或Array代替效率会更高。只是给了你一个提示。



这就是说,让我们看看他们对图像做什么的列表操作。

 > let demo f = mapM_ print $ f m 
>演示ID
[RGB 1 2 3,RGB 4 5 6]
[RGB 7 8 9,RGB 1 5 9]
>演示背面
[RGB 7 8 9,RGB 1 5 9]
[RGB 1 2 3,RGB 4 5 6]
>演示(地图反转)
[RGB 4 5 6,RGB 1 2 3]
[RGB 1 5 9,RGB 7 8 9]
>演示(转置)
[RGB 1 2 3,RGB 7 8 9]
[RGB 4 5 6,RGB 1 5 9]



  • reverse >


  • map reverse 水平翻转图像(颠倒每行中的像素)

  • >
  • transpose 沿着 \ 对角线翻转图像。现在,找到一张纸,并找出如何根据这些操作执行所需的旋转(建模

  • ),如果您的纸张是长方形的,code>转置
    会非常棘手)。请记住,只要您看到纸张的背面,图像就会出现镜像。由于每个操作都会将纸张转动,所以您需要偶数的纸张进行旋转。



    您可以使用反向地图反转会颠倒纸张。这意味着您需要转置来旋转图像90°;



    转置(对角翻转)后跟反转(垂直翻转)将图像旋转90°;到 left

     >演示(反向转置)
    [RGB 4 5 6,RGB 1 5 9]
    [RGB 1 2 3,RGB 7 8 9]
    reverse (垂直翻转),后面跟着转置
    $ b

    c $ c>(对角翻转)将图像旋转90°;到 right (你想要的):

     >演示(转置。反转)
    [RGB 7 8 9,RGB 1 2 3]
    [RGB 1 5 9,RGB 4 5 6]


    i need help about a rotation or spin matrix in haskell

    i have a list of list, of a data type RGB:

    data RGBdata= RGB Int Int Int
    
    m = [[(RGB 0 255 255),(RGB 255 0 0)],[(RGB 255 255 255),(RGB 255 0 0)]]
    

    to be seen better i have a matrix 2x2:

    m = [[(RGB 1 2 3),(RGB 4 5 6)],
         [(RGB 7 8 9),(RGB 1 5 9)]]
    

    and i need 90° rotation, i mean something like:

    m = [[(RGB 7 8 9),(RGB 1 2 3)]
         [(RGB 1 5 9),(RGB 4 5 6)]]
    

    Extends my explication, i have 2 data type:

    data RGBdata= RGB Int Int Int
    data PBMfile= PBM Int Int [[RGBdata]]
    

    and my function receive:

    spin :: PBMfile -> PBMfile
    spin (PBM x y l) = (PBM x y ((transpose . reverse) l))
    

    where 'x' and 'y' is the number of colums and rows respectively (maybe can help to do the function).

    I try rotate 90° to the left with your anwer and the image result is wrong.

    i try

    spin :: PBMfile -> PBMfile
    spin (PBM x y l) = (PBM x y ((reverse . transpose) l))
    

    and

    spin :: PBMfile -> PBMfile
    spin (PBM x y l) = (PBM x y ((transpose . reverse) l))
    

    and

    spin :: PBMfile -> PBMfile
    spin (PBM x y l) = (PBM x y (((map reverse) . transpose) l))
    

    to rotate the image but does not work.

    the result is something like

    http://imageshack.us/photo/my-images/52/catmc.jpg/

    解决方案

    TL;DR: transpose . reverse

    If you're planning to use lists to store large images, bear in mind that it will probably be inefficient, since there's five boxed values (a lot of bytes) for every pixel. It'd be more efficient to use an unboxed Vector or Array instead. Just giving you a heads up.

    That said, let's look at list operations in terms of what they do to the image.

    > let demo f = mapM_ print $ f m
    > demo id
    [RGB 1 2 3,RGB 4 5 6]
    [RGB 7 8 9,RGB 1 5 9]
    > demo reverse
    [RGB 7 8 9,RGB 1 5 9]
    [RGB 1 2 3,RGB 4 5 6]
    > demo (map reverse)
    [RGB 4 5 6,RGB 1 2 3]
    [RGB 1 5 9,RGB 7 8 9]
    > demo (transpose)
    [RGB 1 2 3,RGB 7 8 9]
    [RGB 4 5 6,RGB 1 5 9]
    

    • reverse flips the image vertically (by reversing the rows)

    • map reverse flips the image horizontally (by reversing the pixels in each row)

    • transpose flips the image along the \ diagonal line.

    Now, find a piece of paper, and figure out how to perform the desired rotation in terms of these operations (modeling transpose is tricky if your paper is rectangular). Remember that any time you see the back of the paper, the image will appear mirrored. Since each of these operations turns the paper around, you need an even number of them to perform a rotation.

    The only rotation you can do with reverse and map reverse is turn the paper upside down. This means you'll need transpose to rotate the image 90°.

    transpose (diagonal flip) followed by reverse (vertical flip) rotates the image 90° to the left:

    > demo (reverse . transpose)
    [RGB 4 5 6,RGB 1 5 9]
    [RGB 1 2 3,RGB 7 8 9]
    

    On the other hand, reverse (vertical flip) followed by transpose (diagonal flip) rotates the image 90° to the right (which you want):

    > demo (transpose . reverse)
    [RGB 7 8 9,RGB 1 2 3]
    [RGB 1 5 9,RGB 4 5 6]
    

    这篇关于旋转图像.pbm Haskell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆