Skip to content

Commit

Permalink
Merge pull request #2 from pfe-rs/mihailo
Browse files Browse the repository at this point in the history
Testovi i popunjene klase filtara
  • Loading branch information
ThatSava authored Mar 30, 2024
2 parents 5e61f2b + 61e9ed6 commit 221d427
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
venv
__pycache__
.pytest_cache
sacuvana_switz.png
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"python.testing.unittestArgs": [
"-v",
"-s",
".",
"-p",
"test_*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true
}
106 changes: 97 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from PIL import Image
import cv2
import numpy as np
import os

class Slika:
def __init__(self, putanja=None):
Expand All @@ -11,6 +13,10 @@ def ucitaj_sliku(self, putanja):
self.slika = np.array(Image.open(putanja))

def sacuvaj_sliku(self, putanja):

direktorijum = os.path.dirname(putanja)
if not os.path.exists(direktorijum):
os.makedirs(direktorijum, exist_ok=True)
Image.fromarray(self.slika).save(putanja)

def prikazi_sliku(self):
Expand All @@ -24,22 +30,104 @@ def postavi_pixel(self, x, y, vrednost):


class Filtar:
def primeni_filtar(self):
def primeni_filtar(self, slika):
pass


class GausFiltar:
def primeni_filtar(self,velicina_kernela,sigmaX,sigmaY):
pass
def __init__(self, velicina_kernela, sigmaX, sigmaY):
self.velicina_kernela = velicina_kernela
self.sigmaX = sigmaX
self.sigmaY = sigmaY
def primeni_filtar(self, slika):
if slika is None:
print("Slika nije učitana.")
return None

if self.velicina_kernela % 2 == 0:
print("Veličina kernela mora biti neparni broj.")
return None


isfiltrirana_slika = cv2.GaussianBlur(slika, (self.velicina_kernela, self.velicina_kernela), self.sigmaX, self.sigmaY)
return isfiltrirana_slika

class CannyFiltar:
def primeni_filtar(self, minimalna_vrednost, maksimalna_vrednost,velicina_otvora_blende):
pass
def __init__(self, minimalna_vrednost, maksimalna_vrednost, velicina_otvora_blende=3):

self.minimalna_vrednost = minimalna_vrednost
self.maksimalna_vrednost = maksimalna_vrednost
self.velicina_otvora_blende = velicina_otvora_blende

def primeni_filtar(self, slika):

if slika is None:
print("Slika nije učitana.")
return None

if len(slika.shape) == 3:
slika = cv2.cvtColor(slika, cv2.COLOR_BGR2GRAY)

ivice = cv2.Canny(slika, self.minimalna_vrednost, self.maksimalna_vrednost, apertureSize=self.velicina_otvora_blende)
return ivice


class LaplacianFiltar:
def primeni_filtar(self, velicina_kernela):
pass
def __init__(self, ddepth, ksize=1):
"""
:param ddepth:Na primer, cv2.CV_8U, cv2.CV_16S, cv2.CV_32F, itd.
:param ksize: Veličina kernela za Laplacian operator. Mora biti neparni broj.
"""
self.ddepth = ddepth
self.ksize = ksize

def primeni_filtar(self, slika):

if slika is None:
print("Slika nije učitana.")
return None
if len(slika.shape) == 3:
slika = cv2.cvtColor(slika, cv2.COLOR_BGR2GRAY)

# Primena Laplacian filtra
laplacian = cv2.Laplacian(slika, self.ddepth, ksize=self.ksize)
return laplacian

class MedianFiltar:
def primeni_filtar(self, velicina_kernela):
pass
def __init__(self, velicina_kernela):


# param velicina_kernela:mora biti neparni broj.

self.velicina_kernela = velicina_kernela

def primeni_filtar(self, slika):

if slika is None:
print("Slika nije učitana.")
return None

# Veličina kernela mora biti neparni broj.
if self.velicina_kernela % 2 == 0:
print("Veličina kernela za medijanski filter mora biti neparni broj.")
return None


isfiltrirana_slika = cv2.medianBlur(slika, self.velicina_kernela)
return isfiltrirana_slika




# slika_obj = Slika("C:\\Users\\mihai\\Desktop\\zr-s-2024-filter-slike\\switz.png")
# slika = slika_obj.slika


# laplacian_filtr = LaplacianFiltar(cv2.CV_8U, ksize=3)
# laplacian_ivice = laplacian_filtr.primeni_filtar(slika)


# if laplacian_ivice is not None:
# cv2.imshow("Laplacian Ivice", laplacian_ivice)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
9 changes: 9 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
colorama==0.4.6
iniconfig==2.0.0
numpy==1.26.4
opencv-python==4.9.0.80
packaging==24.0
pillow==10.2.0
pluggy==1.4.0
pytest==8.1.1
pytest-mock==3.14.0
Binary file added switz.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions test_filtriranje.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import PIL
import numpy as np
import pytest
from PIL import Image
from main import Slika
import os

PUTANJA_TEST_SLIKE = "switz.png"
PUTANJA_SACUVANE_SLIKE = os.path.abspath("sacuvana_switz.png")

def test_ucitaj_sliku():
slika = Slika(PUTANJA_TEST_SLIKE)
assert slika.slika is not None
assert isinstance(slika.slika, np.ndarray)

def test_sacuvaj_sliku():
slika = Slika(PUTANJA_TEST_SLIKE)
slika.sacuvaj_sliku(PUTANJA_SACUVANE_SLIKE)

sacuvana_slika = Image.open(PUTANJA_SACUVANE_SLIKE)
originalna_slika = Image.open(PUTANJA_TEST_SLIKE)
assert list(sacuvana_slika.getdata()) == list(originalna_slika.getdata())




def test_prikazi_sliku(mocker):
mocker.patch('PIL.Image.Image.show')
slika = Slika(PUTANJA_TEST_SLIKE)
slika.prikazi_sliku()
assert PIL.Image.Image.show.call_count == 1



0 comments on commit 221d427

Please sign in to comment.