Skip to content

Commit 1156176

Browse files
committed
Add comments
1 parent 871727d commit 1156176

20 files changed

+202
-108
lines changed

Examples.md

Lines changed: 0 additions & 91 deletions
This file was deleted.

src/main/scala/Algorithms/Algorithm.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ package Algorithms
22

33
import breeze.linalg.DenseMatrix
44

5+
/**
6+
* Trait that define an algorithm for the processing pipeline
7+
*/
58
trait Algorithm extends Serializable {
9+
10+
/**
11+
* Main run algorithm method, takes a matrix and return a matrix
12+
*
13+
* @param imageMatrix unprocessed image matrix
14+
* @return processed image matrix
15+
*/
616
def run (imageMatrix :DenseMatrix[Double]): DenseMatrix[Double]
717
}

src/main/scala/Algorithms/Convolution.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,20 @@ package Algorithms
22

33
import breeze.linalg.DenseMatrix
44

5+
/**
6+
* A convolution process takes a kernel matrix, which is slid across the image and multiplied with the input
7+
* such that the output is enhanced in a certain desirable manner.
8+
*
9+
* @param convolutionKernel kernel matrix
10+
*/
511
class Convolution (convolutionKernel :DenseMatrix[Double]) extends Algorithm {
12+
13+
/**
14+
* Main run algorithm method, takes a matrix and return a matrix
15+
*
16+
* @param imageMatrix unprocessed image matrix
17+
* @return processed image matrix
18+
*/
619
def run (imageMatrix :DenseMatrix[Double]) :DenseMatrix[Double] = {
720
assert(convolutionKernel.cols == 3)
821
assert(convolutionKernel.rows == 3)

src/main/scala/Algorithms/Denoiser.scala

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ import breeze.linalg.InjectNumericOps
88
import scala.util.Random
99
import java.io.File
1010

11-
class Denoiser (MAX_BURNS :Int = 100, MAX_SAMPLES :Int = 200) extends Algorithm {
11+
/**
12+
* Gibbs denoiser implementation
13+
*
14+
* @param maxBurns max number of iterations
15+
*/
16+
class Denoiser (maxBurns:Int = 100) extends Algorithm {
1217
val ITA = 0.9
1318
val BETA = 2
19+
val MAX_SAMPLES :Int = 200
1420
val initialization = "same"
1521

1622
private def energy (Y : DenseMatrix[Double], X: DenseMatrix[Double]) : Double = {
@@ -21,15 +27,29 @@ class Denoiser (MAX_BURNS :Int = 100, MAX_SAMPLES :Int = 200) extends Algorithm
2127
+ sum( Y( ::, 0 until M-1 ) *:* Y( ::, 1 to -1 ) )
2228
}
2329

30+
/**
31+
* Sample phase
32+
*
33+
* @param i pixel row
34+
* @param j pixel col
35+
* @param Y processed matrix
36+
* @param X original matrix
37+
* @return
38+
*/
2439
private def sample (i: Int, j: Int, Y: DenseMatrix[Double], X: DenseMatrix[Double]) : Int = {
2540
val blanket = new DenseVector[Double]( Array(Y(i-1, j), Y(i, j-1), Y(i, j+1), Y(i+1, j), X(i, j)) )
2641

2742
val w = ITA * blanket(-1) + BETA * sum(blanket(0 until 4))
2843
val prob = 1 / (1 + math.exp(-2*w))
29-
//val prob = exp( 2 * sum(blanket).toDouble ) / ( 1 + exp( 2 * sum(blanket).toDouble ))
3044
if (Random.nextDouble < prob) 1 else -1
3145
}
3246

47+
/**
48+
* Main run algorithm method, takes a matrix and return a matrix
49+
*
50+
* @param imageMatrix unprocessed image matrix
51+
* @return processed image matrix
52+
*/
3353
def run (imageMatrix :DenseMatrix[Double]): DenseMatrix[Double] = {
3454
println("Denoiser: working")
3555
println(s"Initialization : $initialization")
@@ -44,7 +64,7 @@ class Denoiser (MAX_BURNS :Int = 100, MAX_SAMPLES :Int = 200) extends Algorithm
4464

4565

4666
var ctr = 0
47-
for ( _ <- 0 until MAX_BURNS) {
67+
for ( _ <- 0 until maxBurns) {
4868
for {
4969
i <- 1 until N-1
5070
j <- 1 until M-1

src/main/scala/Algorithms/Invert.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@ package Algorithms
22

33
import breeze.linalg.DenseMatrix
44

5-
class Invert extends Algorithm {
5+
/**
6+
* Invert colors of a given matrix
7+
*/
8+
object Invert extends Algorithm {
9+
10+
/**
11+
* Main run algorithm method, takes a matrix and return a matrix
12+
*
13+
* @param imageMatrix unprocessed image matrix
14+
* @return processed image matrix
15+
*/
616
def run (imageMatrix :DenseMatrix[Double]) :DenseMatrix[Double] = {
717
imageMatrix.map( elem => 255 - elem)
818
}

src/main/scala/Algorithms/AverageFilter.scala renamed to src/main/scala/Algorithms/MeanFilter.scala

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@ package Algorithms
22

33
import breeze.linalg.DenseMatrix
44

5-
class MeanFilter extends Algorithm {
5+
/**
6+
* Mean filtering is a simple, intuitive and easy to implement method of smoothing images,
7+
* reducing the amount of intensity variation between one pixel and the next
8+
*/
9+
object MeanFilter extends Algorithm {
10+
11+
/**
12+
* Main run algorithm method, takes a matrix and return a matrix
13+
*
14+
* @param imageMatrix unprocessed image matrix
15+
* @return processed image matrix
16+
*/
617
def run (imageMatrix :DenseMatrix[Double]): DenseMatrix[Double] = {
718
val outMatrix = DenseMatrix.zeros[Double](imageMatrix.rows, imageMatrix.cols)
819
for {

src/main/scala/Algorithms/MedianFilter.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@ package Algorithms
22

33
import breeze.linalg.DenseMatrix
44

5+
/**
6+
* Median filter works by moving through the image pixel by pixel,
7+
* replacing each value with the median value of neighbouring pixels
8+
*/
59
object MedianFilter extends Algorithm {
10+
11+
/**
12+
* Main run algorithm method, takes a matrix and return a matrix
13+
*
14+
* @param imageMatrix unprocessed image matrix
15+
* @return processed image matrix
16+
*/
617
def run (imageMatrix :DenseMatrix[Double]): DenseMatrix[Double] = {
718
val outMatrix = DenseMatrix.zeros[Double](imageMatrix.rows, imageMatrix.cols)
819
for {

src/main/scala/Main.scala

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import Pipelines.{EdgeDetection, MedianDenoiser, GibbsEdgeDetection, Pipeline}
88
import Algorithms.Denoiser
99
import Pipelines.GibbsDenoiser
1010

11+
/**
12+
* Main entry point
13+
*
14+
* sbt assembly
15+
* spark-submit --driver-memory 8g --master local[*] ./jar/binary.jar ./data/nike_noisy.png
16+
*/
1117
object Main {
1218

1319
var inputPathImage = "./data/input.png"
@@ -48,6 +54,7 @@ object Main {
4854
println(s"Sub matrix size: ${subHeight}")
4955
println(s"Paddding: ${padding}")
5056

57+
// Get image as matrix
5158
val inputStream = FileUtils.getInputStream(inputPathImage)
5259
val inputImage = new Image()
5360
val pixelArray = inputImage.getPixelMatrix(inputStream, true)
@@ -56,6 +63,7 @@ object Main {
5663

5764
// Define the Spark Job
5865
val job = new SparkJob(padding, subHeight, subWidth, denoiserRuns, debug)
66+
// Match the selected pipeline
5967
val pipeline = inputPiepeline match {
6068
case "GibbsDenoise" => new GibbsDenoiser(denoiserRuns)
6169
case "GibbsEdgeDetection" => new GibbsEdgeDetection(denoiserRuns)
@@ -64,16 +72,19 @@ object Main {
6472
}
6573

6674
println("\nStart")
75+
// Runs job and gets execution time
6776
val result = Utils.time(job.run(pixelMatrix, pipeline))
6877
if(debug > 0)
6978
println(s"Time: ${result._2} ms")
7079

80+
// Saves output image as file
7181
val outputStream = FileUtils.getOutputStream(outputPathImage)
7282
val outputImage = new Image()
7383
outputImage.setPixelMatrix(result._1.data.map(_.toInt), result._1.rows, result._1.cols, true)
7484
outputImage.saveImage(outputStream)
7585
outputStream.close()
7686

87+
// Forges the output report
7788
val json = s"""{
7889
"time": ${result._2},
7990
"inputPiepeline": "${inputPiepeline}",
@@ -86,11 +97,9 @@ object Main {
8697
"subWidth": ${subWidth}
8798
}"""
8899

100+
// Saves report to file
89101
val jsonOutputStream = FileUtils.getOutputStream(outputPathJson)
90102
jsonOutputStream.write(json.getBytes(Charset.forName("UTF-8")))
91103
jsonOutputStream.close()
92104
}
93-
}
94-
95-
// sbt assembly
96-
// spark-submit --driver-memory 8g --master local[*] ./jar/binary.jar ./data/nike_noisy.png
105+
}

src/main/scala/Noiser/Noiser.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import java.io.File
22
import scala.util.Random
33
import _root_.Utils.FileUtils
44

5+
/**
6+
* Used to make sample noise images
7+
*/
58
object Noiser {
69
val PROB = 0.3
710
val inputImage = "./data/nike.png"

src/main/scala/Pipelines/EdgeDetection.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ import breeze.linalg.DenseMatrix
66
object EdgeDetection extends Pipeline (
77
List(
88
new Convolution( DenseMatrix((-1.0, -1.0, -1.0), (-1.0, 8.0, -1.0), (-1.0, -1.0, -1.0))),
9-
new Invert()
9+
Invert
1010
)) {}

0 commit comments

Comments
 (0)