convolution done, sobel todo
This commit is contained in:
parent
bcc891bcb6
commit
0838004590
BIN
03nov/convo1.jpg
Normal file
BIN
03nov/convo1.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 256 KiB |
BIN
03nov/convo2.jpg
Normal file
BIN
03nov/convo2.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 321 KiB |
BIN
03nov/convo3.jpg
Normal file
BIN
03nov/convo3.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 261 KiB |
BIN
03nov/gray.jpg
Normal file
BIN
03nov/gray.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 161 KiB |
BIN
03nov/image_test.jpg
Normal file
BIN
03nov/image_test.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 186 KiB |
73
03nov/main.py
Normal file
73
03nov/main.py
Normal file
@ -0,0 +1,73 @@
|
||||
from umage import load, save, show_image
|
||||
|
||||
def grayscale(img_mat):
|
||||
"""Transform an image into gray
|
||||
|
||||
:img_mat: image en entree
|
||||
:returns: grayscale de limage em entree
|
||||
|
||||
"""
|
||||
new_matrix = list()
|
||||
for row in range(len(img_mat)):
|
||||
new_matrix.append(list())
|
||||
for column in img_mat[row]:
|
||||
_r, _g, _b = column
|
||||
gray = round(0.2125 * _r + 0.7154 * _g + 0.0721 * _b)
|
||||
new_matrix[row].append((gray, gray, gray))
|
||||
|
||||
return new_matrix
|
||||
|
||||
def convolution(img_mat, mat):
|
||||
"""effectue le passage d'une matrice de convolution sur une image grise
|
||||
TODO: image de couleurs
|
||||
|
||||
:img_mat: image en entree
|
||||
:mat: matrice de convolution
|
||||
:returns: image retouchee
|
||||
|
||||
"""
|
||||
|
||||
new_matrix = list()
|
||||
for row in range(len(img_mat)):
|
||||
new_matrix.append(list())
|
||||
for column in range(len(img_mat[row])):
|
||||
# _gray = img_mat[row][column][0]
|
||||
_sum = 0
|
||||
for mat_row in range(len(mat)):
|
||||
for mat_column in range(len(mat[mat_row])):
|
||||
diff_row = mat_row - len(mat)//2
|
||||
diff_col = mat_column - len(mat[mat_column])//2
|
||||
if dans_image(img_mat, row+diff_row, column+ diff_col):
|
||||
_sum += mat[mat_row][mat_column] * img_mat[row + diff_row][column + diff_col][0]
|
||||
new_matrix[row].append((_sum, _sum, _sum))
|
||||
return new_matrix
|
||||
|
||||
def dans_image(img_mat, row, col):
|
||||
if row < len(img_mat)-1 and row > 0 and col < len(img_mat[0])-1 and col > 0:
|
||||
return True
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
unit = [
|
||||
[0, 1, 0],
|
||||
[0, 0, 0],
|
||||
[0, 0, 0]]
|
||||
convolution1 = [
|
||||
[-1, -1, -1],
|
||||
[-1, 8, -1],
|
||||
[-1, -1, -1]]
|
||||
convolution2 = [
|
||||
[-1, -1, -1],
|
||||
[-1, 9, -1],
|
||||
[-1, -1, -1]]
|
||||
convolution3 = [
|
||||
[-2, 0, 0],
|
||||
[ 0, 1, 0],
|
||||
[ 0, 0, 2]]
|
||||
img = load('./myimg.jpg')
|
||||
new_image = grayscale(img)
|
||||
convolution(new_image, convolution2)
|
||||
save(new_image, 'gray')
|
||||
save(convolution(new_image, convolution1), 'convo1')
|
||||
save(convolution(new_image, convolution2), 'convo2')
|
||||
save(convolution(new_image, convolution3), 'convo3')
|
BIN
03nov/myimg.jpg
Normal file
BIN
03nov/myimg.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 172 KiB |
BIN
03nov/serie7.pdf
Normal file
BIN
03nov/serie7.pdf
Normal file
Binary file not shown.
13
03nov/traceback.txt
Normal file
13
03nov/traceback.txt
Normal file
@ -0,0 +1,13 @@
|
||||
Traceback (most recent call last):
|
||||
File "/home/tonitch/.local/lib/python3.10/site-packages/pudb/__init__.py", line 148, in runscript
|
||||
dbg._runscript(mainpyfile)
|
||||
File "/home/tonitch/.local/lib/python3.10/site-packages/pudb/debugger.py", line 519, in _runscript
|
||||
self.run(statement)
|
||||
File "/usr/lib/python3.10/bdb.py", line 597, in run
|
||||
exec(cmd, globals, locals)
|
||||
File "<string>", line 1, in <module>
|
||||
File "main.py", line 65, in <module>
|
||||
final_img = convolution(new_image, unit)
|
||||
File "main.py", line 41, in convolution
|
||||
_sum += mat[mat_row][mat_column] * img_mat[row + diff_row][column + diff_col][0]
|
||||
IndexError: list index out of range
|
35
03nov/umage.py
Normal file
35
03nov/umage.py
Normal file
@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from PIL import Image # Requires python-image-library (pillow)
|
||||
import itertools
|
||||
|
||||
|
||||
def load(filename):
|
||||
""" Given a filename that matches an image file,
|
||||
return a list of lists of tuples corresponding to the list of
|
||||
lines of pixels (R, G, B) of the image. """
|
||||
|
||||
with Image.open(filename, 'r') as image:
|
||||
image = image.convert('RGB')
|
||||
content = list(image.getdata())
|
||||
size_x, size_y = image.size
|
||||
return [content[i:i + size_x] for i in range(0, len(content), size_x)]
|
||||
|
||||
|
||||
def save(image, filename='new', extension='jpg'):
|
||||
""" Stores the given image into a file. The name
|
||||
of the file is set to <filename>.<extension> which is
|
||||
'new.jpg' by default. """
|
||||
|
||||
size_x, size_y = len(image), len(image[0])
|
||||
new_image = Image.new('RGB', (size_y, size_x))
|
||||
new_image.putdata(list(itertools.chain.from_iterable(image)))
|
||||
new_image.save('%s.%s' % (filename, extension))
|
||||
|
||||
def show_image(image):
|
||||
""" Show the image to the user. """
|
||||
|
||||
size_x, size_y = len(image), len(image[0])
|
||||
new_image = Image.new('RGB', (size_y, size_x))
|
||||
new_image.putdata(list(itertools.chain.from_iterable(image)))
|
||||
new_image.show()
|
Loading…
Reference in New Issue
Block a user