
Hi all: I already have one matrix of type [[a]] to store one image. What I want to do is to devide the image into severial small blocks in same size. To do that, I wrote this tool function. chop :: Int -> [a] -> [[a]] chop _ [] = [] chop n ls = take n ls : chop n (drop n ls) But I do not know how to use it to write the function. Thanks for any advice. Regards -------------- L.Guo 2007-06-13

On Wed, 13 Jun 2007, L.Guo wrote:
I already have one matrix of type [[a]] to store one image.
What I want to do is to devide the image into severial small blocks in same size.
To do that, I wrote this tool function.
chop :: Int -> [a] -> [[a]] chop _ [] = [] chop n ls = take n ls : chop n (drop n ls)
I assume that it is more efficient to use 'splitAt' instead of 'take' and 'drop'.
But I do not know how to use it to write the function.
chop blockHeight (map (chop blockWidth) image)

Henning Thielemann wrote:
On Wed, 13 Jun 2007, L.Guo wrote:
I already have one matrix of type [[a]] to store one image.
What I want to do is to devide the image into severial small blocks in same size.
To do that, I wrote this tool function.
chop :: Int -> [a] -> [[a]] chop _ [] = [] chop n ls = take n ls : chop n (drop n ls)
I assume that it is more efficient to use 'splitAt' instead of 'take' and 'drop'.
But I do not know how to use it to write the function.
chop blockHeight (map (chop blockWidth) image)
That would divide abcd efgh ijkl mnop into [ [ ab cd , ef gh ] , [ ij kl , mn op ] ] at block size 2. In any case, the original poster was not very clear about what they want... -- Dr. Janis Voigtlaender http://wwwtcs.inf.tu-dresden.de/~voigt/ mailto:voigt@tcs.inf.tu-dresden.de

Hi, Henning Thielemann. Thanks for your help. That is usful. I have wrote the target function like this, and tested. mkBlocks (w,h) = map concat . concat . transpose . chop h . map (chop w) Hi, Dr. Janis Voigtlaender. This is not a homework, though likely to be one. I just use Haskell to write tools being used in my work. This is one of them. I need to locate the difference between my coded image and standard coded image. And both coded in 16x16 macroblocks. That is why I ran into this problem. Anyway, thanks for your advice. ------------------ L.Guo 2007-06-14

L.Guo wrote:
I have wrote the target function like this, and tested.
mkBlocks (w,h) = map concat . concat . transpose . chop h . map (chop w)
I don't understand how this relates to your original problem description. But then, again, I probably did not understand that one too well.
This is not a homework, though likely to be one.
No offense meant, of course. Anyway, as a challenge to others on the list: write a one-liner that splits an "image" like ["abcd","efgh","ijkl","mnop"], interpreted as abcd efgh ijkl mnop into the list of images: [ ab ef , cd gh , ij mn , kl op ] for (w,h)=(2,2), into: [ a e , b f , c g , d h , i m , j n , k o , l p ] for (w,h)=(1,2), and so on. (Where an image like ab ef is represented as ["ab","ef"].) Have fun, Janis. -- Dr. Janis Voigtlaender http://wwwtcs.inf.tu-dresden.de/~voigt/ mailto:voigt@tcs.inf.tu-dresden.de

Hi Janis. I think either my data format or my original question confuses you. About the problem description. When I send the question, I have not decide how to manage the block data. If the rough question confuse you, I have to say sorry. About the data. The whole image is of type [[a]], and after being devided, it is also [[a]]. eg. for (w,h) = (2,2) a b c d e f g h i j k l m n o p makes a b e f c d g h i j m n k l o p in another word, mkBlocks makes [pixels-of-lines] into [pixels-in-blocks]. This cryptical type is easy to form and to form from type [[pixels-of-lines-in-block]], and also, is close to my future purpose. Thanks. ------------------ L.Guo 2007-06-14 ------------------------------------------------------------- From: Janis Voigtlaender At: 2007-06-14 15:42:40 Subject: Re: [Haskell-cafe] How to devide matrix into small blocks L.Guo wrote:
I have wrote the target function like this, and tested.
mkBlocks (w,h) = map concat . concat . transpose . chop h . map (chop w)
I don't understand how this relates to your original problem description. But then, again, I probably did not understand that one too well.

On Thu, 14 Jun 2007, L.Guo wrote:
About the data.
The whole image is of type [[a]], and after being devided, it is also [[a]].
I would store an image in an (Array (Int,Int)). This is not only more efficient, but also ensures statically that the image data is rectangular. I assume that you do not need infinite images. They can be represented by lists but not by arrays.

On Thu, 14 Jun 2007, Janis Voigtlaender wrote:
Anyway, as a challenge to others on the list: write a one-liner that splits an "image" like ["abcd","efgh","ijkl","mnop"], interpreted as
abcd efgh ijkl mnop
into the list of images:
[ ab ef , cd gh , ij mn , kl op ]
It's just an additional 'concat' in my solution, isn't it? Ah, you mean without the 'split' helper function?

Henning Thielemann wrote:
On Thu, 14 Jun 2007, Janis Voigtlaender wrote:
Anyway, as a challenge to others on the list: write a one-liner that splits an "image" like ["abcd","efgh","ijkl","mnop"], interpreted as
abcd efgh ijkl mnop
into the list of images:
[ ab ef , cd gh , ij mn , kl op ]
It's just an additional 'concat' in my solution, isn't it? Ah, you mean without the 'split' helper function?
No, I do mean using the chop function provided by the original poster. And I think that adding a concat to your solution, as in: concat (chop blockHeight (map (chop blockWidth) image)) gives the following, different from above: [ ab cd , ef gh , ij kl , mn op ] Ciao, Janis. -- Dr. Janis Voigtlaender http://wwwtcs.inf.tu-dresden.de/~voigt/ mailto:voigt@tcs.inf.tu-dresden.de

What about blocks w h = concatMap transpose . map (map (chop w)) . chop h ? @L. Guo: map concat . blocks w h is what you want. Cheers, Daniel Am Donnerstag, 14. Juni 2007 09:42 schrieb Janis Voigtlaender:
Anyway, as a challenge to others on the list: write a one-liner that splits an "image" like ["abcd","efgh","ijkl","mnop"], interpreted as
abcd efgh ijkl mnop
into the list of images:
[ ab ef , cd gh , ij mn , kl op ]
for (w,h)=(2,2), into:
[ a e , b f , c g , d h , i m , j n , k o , l p ]
for (w,h)=(1,2), and so on.
(Where an image like
ab ef
is represented as ["ab","ef"].)
Have fun, Janis.

Daniel Fischer wrote:
What about
blocks w h = concatMap transpose . map (map (chop w)) . chop h
Seems right. I arrived at something else: divide w h ls = concatMap (foldr (zipWith (:) . chop w) (repeat [])) (chop h ls) That uses fewer intermediate lists, and indeed should be equivalent to the above up to some fusion calculations (fusing concatMap with map, and fusing the foldr-based definition of transpose with the remaining map). Ciao, Janis. -- Dr. Janis Voigtlaender http://wwwtcs.inf.tu-dresden.de/~voigt/ mailto:voigt@tcs.inf.tu-dresden.de

L.Guo wrote:
Hi all:
I already have one matrix of type [[a]] to store one image.
What I want to do is to devide the image into severial small blocks in same size.
In the sense of dividing an image like abcd efgh ijkl mnop into the sequence of images [ ab ef , cd gh , ij mn , kl op ] when the block size is 2?
To do that, I wrote this tool function.
chop :: Int -> [a] -> [[a]] chop _ [] = [] chop n ls = take n ls : chop n (drop n ls)
But I do not know how to use it to write the function.
Hmm, if you have no idea how "chop" could help you to write your "divide" function, what was your motivation for writing "chop" in the first place? Could it be that the following applies: http://www.haskell.org/haskellwiki/Homework_help ? If the problem you are trying to solve corresponds to my example above, it might get you started to think about what you can do by combining tow applications of "chop" with two applications of "map". Ciao, Janis. -- Dr. Janis Voigtlaender http://wwwtcs.inf.tu-dresden.de/~voigt/ mailto:voigt@tcs.inf.tu-dresden.de
participants (4)
-
Daniel Fischer
-
Henning Thielemann
-
Janis Voigtlaender
-
L.Guo