Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
fp:lab07 [2022/04/20 14:22]
pdmatei
fp:lab07 [2022/05/03 17:25] (current)
pdmatei
Line 27: Line 27:
 // define your functions here // define your functions here
  
-def main(args: Array[String]) = { +  ​def main(args: Array[String]) = { 
-// write your tests here +  // write your tests here 
-}+  }
 } }
 </​code> ​ </​code> ​
  
-**7.1.** Write a function+**7.1.** Write a function ​which converts an image to a string (Hint: you can draw inspiration from a similar one from the lecture): 
 +<code scala> 
 +def show(m: Img): String = ??? 
 +</​code>​ 
 + 
 +**7.2.** Write a function which performs a horizontal flip on an image. Try to first visualise (you may use pen and paper) how the transformation would look like. 
 +<code scala> 
 +def hFlip(img: Img): Img = ??? 
 +</​code>​ 
 + 
 +**7.3.** Write a function which performs vertical flip. 
 +<code scala> 
 +def vFlip(img: Img): Img = ??? 
 +</​code>​  
 + 
 +**7.4.** ​ Write a function which performs a 90 degrees rotation to the right. (Hint: you need an ingredient from the lecture. Also, note that there are multiple possible implementations.) 
 +<code scala> 
 +def rot90Right(img:​ Img): Img = ??? 
 +</​code>​ 
 + 
 +**7.5.** Write a function which performs a 90 degrees rotation to the left. 
 +<code scala> 
 +def rot90Left(img:​ Img): Img = ??? 
 +</​code>​ 
 + 
 +**7.6.** Write a function which inverts an image (values 0 become 255, 1 - 254, and so forth). 
 + 
 +**7.7.** Write a function which crops a given image, using two, two-dimensional coordinates:​ the higher-left point x and y, and the lower-right point x and y. An example is shown below: 
 +<code scala> 
 +val img = List(List(0,​0,​1,​0,​0),​ List(0,​1,​0,​1,​0),​ List(0,​1,​1,​1,​0),​ List(1,​0,​0,​0,​1),​ List(1,​0,​0,​0,​1)) 
 +/* 
 +      0 0 1 0 0 
 +*     0 1 0 1 0                                           1 0 1 
 +*     0 1 1 1 0     ​cropping from 1,1  to  2,3  yields: ​  1 1 1 
 +*     1 0 0 0 1 
 +*     1 0 0 0 1 
 + 
 + */ 
 + 
 +def cropAt(img: Img, xSt:Int, ySt:Int, xEnd: Int, yEnd: Int): Img = ?? 
 +</​code>​  
 + 
 +**7.8.** Write a function which returns a list of all positions which have pixels of larger intensity than x 
 +<code scala> 
 +def largerPos(img:​ Img, int: Int): List[(Int,​Int)] = ??? 
 +</​code>​ 
 + 
 +**7.9.** Write a function which adds ''​x''​ to the intensity of each pixel. 
 +<code scala> 
 +def contrast(x: Int)(img: Img): Img = ??? 
 +</​code>​ 
 + 
 +**7.10.** Write a function which takes two images ''​X''​ and ''​Y''​ and //glues// them on the horizontal axis (the resulting image will be ''​XY''​) 
 +<code scala> 
 +def hglue(img1: Img, img2: Img): Img = ??? 
 +</​code>​ 
 + 
 +**7.11.** Write a function which takes two images ''​X''​ and ''​Y''​ and //glues// them on the vertical axis: 
 +<code scala> 
 +def vglue(img1: Img, img2: Img): Img = ??? 
 +</​code>​ 
 + 
 +**7.12.** Define a function that takes a **square** image, and draws two diagonal lines of intensity 1 across it. Use ''​_.until(_)''​ and ''​_.toList''​. 
 +<code scala> 
 +def diag(img: Img): Img = ??? 
 +</​code>​ 
 + 
 +**7.13.** Define a function which blurs an image as follows: 
 +  * for each pixel p of intensity > 1: make all neighbouring pixels of intensity 0 equal to p-1 (including diagonals). 
 +  * if some pixel of intensity 0 is in the vicinity of two different >1 pixels of different intensities,​ the one which is larger should be taken into account 
 +  * to make the implementation easier, you do not need to implement the blur on the image edges. 
 + 
 +<code scala> 
 +def blur(img: Img): Img = ??? 
 +</​code>​  
 + 
 +**7.14. (!) ** Define a function which builds an effect of intensity x as shown below: 
 +<code scala> 
 +val img2 = List( 
 +  List(0,​0,​0,​0,​0,​0,​0,​0,​0),​ 
 +  List(0,​0,​0,​0,​0,​0,​0,​0,​0),​ 
 +  List(0,​0,​0,​0,​0,​0,​0,​0,​0),​ 
 +  List(0,​0,​0,​0,​1,​0,​0,​0,​0),​ 
 +  List(0,​0,​0,​1,​2,​1,​0,​0,​0),​ 
 +  List(0,​0,​0,​0,​1,​0,​0,​0,​0),​ 
 +  List(0,​0,​0,​0,​0,​0,​0,​0,​0),​ 
 +  List(0,​0,​0,​0,​0,​0,​0,​0,​0),​ 
 +  List(0,​0,​0,​0,​0,​0,​0,​0,​0) 
 +
 + 
 +/* 
 +Before: ​                  After (for x = 2) 
 + 
 +    0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 
 +    0 0 0 0 0 0 0 0 0      0 0 1 1 1 1 1 0 0 
 +    0 0 0 0 0 0 0 0 0      0 1 1 2 2 2 1 1 0 
 +    0 0 0 0 1 0 0 0 0      0 1 2 2 3 2 2 1 0 
 +    0 0 0 1 2 1 0 0 0      0 1 2 3 4 3 2 1 0 
 +    0 0 0 0 1 0 0 0 0      0 1 2 2 3 2 2 1 0 
 +    0 0 0 0 0 0 0 0 0      0 1 1 2 2 2 1 0 0 
 +    0 0 0 0 0 0 0 0 0      0 0 1 1 1 1 1 0 0 
 +    0 0 0 0 0 0 0 0 0      0 0 0 0 0 0 0 0 0 
 + 
 + 
 +Hint: a single foldRight is sufficient. Which is the list you should apply it on? 
 + */ 
 +def effect(intensity:​ Int, img: Img): Img = ??? 
 +</​code> ​