几个关于函数方程编程语言h-a-s-k-e-l-l的问题
本帖最后由 lazyboy2046 于 2010-08-15 12:46 编辑
This question deals with the processing of grey-scale images (pictures). An image consists of a rectangular array of pixels, and each row of an image is represented in Haskell by a list of integers in which 0 represents black, 255 represents white, and intermediate integers represent shades of grey. The whole image consists of a list of these rows.
So, for example, the 3 x 4 pixel image:
Could be denoted by the Haskell list:
[[208, 152, 240, 29 ],
[0, 112, 255, 59],
[76, 185, 0, 152]]
(a) Write a Haskell type definition for pixel images.
(b) A common task in image processing is to darken the image. This can be achieved by reducing each pixel number by one third of its value.
(i) Write a function darkenRow that darkens each pixel in a row of pixel numbers.
(ii) Write a function darkenImage that darkens the whole image in this way.
(c) Another common image processing task is to increase the contrast of the image – darkening the darker pixels and lightening the lighter ones. Write a function increaseImageContrast that darkens (by one third) any pixel with a value that is less than 128 and lightens any pixel with a value of 128 or more (by adding (255 – x) / 3 to any such pixel with value x).
(d) A third image processing task is to shift an image one pixel to the right. The rightmost column of pixels in the original image is dropped and the leftmost column is duplicated so that the image retains its original dimensions.
In order to do this, first write a function dropLastPixel that takes a single row of pixels and drops the last pixel. Then write a function duplicateFirstPixel that adds a copy of the first pixel to the start of the row. Finally, use these two functions to construct a function shiftImageRight that shifts the hole image one pixel to the right.
(e) Write a function shiftImageLeft that shifts an image one pixel to the left, dropping the first column of pixels and adding a copy of the final column to the end. [Hint: You may find it helpful to write functions dropFirstPixel and duplicateLastPixel and structure your answer in a similar way to part (d).]
This question deals with the processing of grey-scale images (pictures). An image consists of a rectangular array of pixels, and each row of an image is represented in Haskell by a list of integers in which 0 represents black, 255 represents white, and intermediate integers represent shades of grey. The whole image consists of a list of these rows.
So, for example, the 3 x 4 pixel image:
QQ截图未命名.jpg (2.36 KB, 下载次数: 26)
Could be denoted by the Haskell list:
[[208, 152, 240, 29 ],
[0, 112, 255, 59],
[76, 185, 0, 152]]
(a) Write a Haskell type definition for pixel images.
(b) A common task in image processing is to darken the image. This can be achieved by reducing each pixel number by one third of its value.
(i) Write a function darkenRow that darkens each pixel in a row of pixel numbers.
(ii) Write a function darkenImage that darkens the whole image in this way.
(c) Another common image processing task is to increase the contrast of the image – darkening the darker pixels and lightening the lighter ones. Write a function increaseImageContrast that darkens (by one third) any pixel with a value that is less than 128 and lightens any pixel with a value of 128 or more (by adding (255 – x) / 3 to any such pixel with value x).
(d) A third image processing task is to shift an image one pixel to the right. The rightmost column of pixels in the original image is dropped and the leftmost column is duplicated so that the image retains its original dimensions.
In order to do this, first write a function dropLastPixel that takes a single row of pixels and drops the last pixel. Then write a function duplicateFirstPixel that adds a copy of the first pixel to the start of the row. Finally, use these two functions to construct a function shiftImageRight that shifts the hole image one pixel to the right.
(e) Write a function shiftImageLeft that shifts an image one pixel to the left, dropping the first column of pixels and adding a copy of the final column to the end. [Hint: You may find it helpful to write functions dropFirstPixel and duplicateLastPixel and structure your answer in a similar way to part (d).]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你想说哪不明白呢
type Pixel3 = Int
type Row3 = [Pixel]
type PixelImage3 = [Row]
img3 :: PixelImage3
img3 = [[208,152,240,29],[0,112,255,59],[76,185,0,152]]
scaleRow1 :: (RealFrac a) => PixelImage -> a -> Int -> PixelImage
scaleRow1 img3 x i = modifyRow img3 f i
where f :: Row -> Row
f = let clamp z | z < 0 = 0
| z > 255 = 255
| otherwise = truncate z
in map (clamp.(x *).fromIntegral)
这个是1a 和1b的答案 大家指点下呢
麻烦大家给下答案 救急
跪谢