Commits

Miroslav Shubernetskiy  committed ed21b03

added mandrill64

  • Participants
  • Parent commits 0ebc3c0

Comments (0)

Files changed (2)

File hw2/deblur.py

+"""
+
+"""
+
+import sys, os, numpy
+
+sys.path.append(os.path.abspath('..'))
+from base.ip import *
+
+class Deblur(IP_Image_Circ_Buffer):
+    mult_matrix = None
+    mult_matrix_inv = None
+    blur_size = None
+
+
+    def __init__(self, size):
+        self.matrix_size = (size, size)
+        self.blur_size = (size - 1) / 2
+
+    def computeDiagonalMatrix(self, n, k):
+        array = numpy.array([0.] * (n ** 2))
+        array.shape = n, n
+
+        k = (k - 1) / 2
+
+        for i in range(n):
+            for j in range(i - k, i + k + 1):
+                if j < 0:
+                    j = 0
+                elif j >= n:
+                    j = n - 1
+                array[i][j] += 1
+
+        self.mult_matrix = array
+
+    def computeInverseMatrix(self, k):
+        self.mult_matrix_inv = k * numpy.linalg.inv(self.mult_matrix)
+
+
+    def unblur(self):
+        self.unblur_x()
+
+    def unblur_x(self):
+        self.computeDiagonalMatrix(self.img.size[0], self.matrix_size[0])
+        self.computeInverseMatrix(self.matrix_size[0])
+
+        print self.mult_matrix_inv
+
+        self.pix = self.img.load()
+
+        for y in range(self.img.size[1]):
+            row = []
+            for i in range(self.img.size[0]):
+                row.append([self.pix[i, y], ])
+
+            result = self.mult_matrix_inv * numpy.matrix(row)
+            result = result.ravel().tolist()[0]
+
+            for i in range(len(result)):
+                result[i] = int(round(result[i]))
+                self.pix[i, y] = result[i]
+
+if __name__ == "__main__":
+    args = sys.argv
+    if len(args) != 4:
+        raise ValueError, "Usage: python %s input-image-path size output-image-path" % __file__
+
+    input_path, size, output_path = args[1:]
+    size = int(size)
+
+    deblur = Deblur(size)
+    deblur.openImage(input_path)
+    deblur.unblur()
+    #    deblur.saveImage(output_path)
+    if DEBUG:
+        deblur.img.show()
+
+

File test-images/mandrill64.png

Added
New image