From a4aea8de7aa1c1c364ca53507b1dc29c3d7b50e8 Mon Sep 17 00:00:00 2001 From: Gasper Spagnolo Date: Fri, 21 Oct 2022 14:51:40 +0200 Subject: [PATCH] Learn to plot the correct image idiot --- assignment1/images/mask.png | Bin 0 -> 809 bytes assignment1/solution.py | 50 ++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 22 deletions(-) create mode 100644 assignment1/images/mask.png diff --git a/assignment1/images/mask.png b/assignment1/images/mask.png new file mode 100644 index 0000000000000000000000000000000000000000..c35c8b60527b2ada2c7780be401399fabee33fa4 GIT binary patch literal 809 zcmeAS@N?(olHy`uVBq!ia0vp^CqS5k2}mkgS)K$^Y)RhkE)4%caKYZ?lYt_f1s;*b z3=G`DAk4@xYmNj^kiEpy*OmP~7bmBZ;vYV{2B45+iEBiObAE1aYF-J0b5UwyNotBh zd1gt5g1e`0KzJjcI0FOIL{AsTkcwMx@18Ddau8q**!bgryqdLmr&M;^!MZt{{k0BS z-@V*Wu$z&AK{JQ%;F8A{ZhgPEo&ED;eZ0_xSR(-)phY0?&%F3Gi_4bKsP5(W=ak6hewZz{XJBYJ;I(&` z`4PraYar)BomP$2Cduhh9|I*y?`N>8RHBxwFXYn~{ zyJ%l&3e^$N(Ks@*925Wt4;|y1zp=};ee1^PJLciW;vLdD^4r2depduKz`;a*9$OI5 z_kuvRK-Eqg9DrW9b?UolwZBZ@#(<3{0_F*eW{Lu3v}2Cv#cp`9dlAd(_W}wL5ABwe zy!~;+-r_h%OS{bY}4pu^-zub{$Rl z#oC?!xUTu`yT$Oce|_eu0xw{CDOG&1H6~4-Z@c@jYGr KT-G@yGywo!%RtEh literal 0 HcmV?d00001 diff --git a/assignment1/solution.py b/assignment1/solution.py index dc2c818..e893ed8 100644 --- a/assignment1/solution.py +++ b/assignment1/solution.py @@ -388,7 +388,7 @@ def excercise_three() -> None: three_a() -def three_a(): +def three_a() -> None: """ We will perform two basic morphological operations on the image mask.png, erosion and dilation. We will also experiment with combinations of both operations, named @@ -398,51 +398,57 @@ def three_a(): Answer: Opening = Erosion then dialation Closing = Dialation then erosion """ - img_orig = uz.imread_gray('./images/bird.jpg', uz.ImageType.float64) + img_orig = uz.imread_gray('./images/mask.png', uz.ImageType.float64) img_er_dil = img_orig.copy() img_dil_er = img_orig.copy() img_comb = img_orig.copy() - img_er_dil_N = 5 - SE = np.ones((img_er_dil_N, img_er_dil_N), np.float64) + SE_size = 2 + SE = np.ones((SE_size, SE_size), np.float64) - fig, axs = plt.subplots(3, 4) fig.suptitle("Image after N iterations of Opening, Closing and combination of opening and closing") - imgs = [] for im_ix in range(4): + SE = np.ones((SE_size, SE_size), np.float64) img_er_dil = cv2.erode(img_er_dil, SE) img_er_dil = cv2.dilate(img_er_dil, SE) - imgs.append(img_er_dil.copy()) + axs[0, im_ix].imshow(img_er_dil.copy(), cmap='gray') + axs[0, im_ix].set(title=f"after {im_ix + 1} iterations of Opening, SE: {SE_size} x {SE_size}") + SE_size +=1 - for im_ix in range(4): - axs[0, im_ix].imshow(imgs[im_ix], cmap='gray') - axs[0, im_ix].set(title=f"image after {im_ix + 1} iterations of opening") - imgs=[] + SE_size = 2 for im_ix in range(4): - img_dil_er = cv2.erode(img_dil_er, SE) + SE = np.ones((SE_size, SE_size), np.float64) img_dil_er = cv2.dilate(img_dil_er, SE) - imgs.append(img_dil_er.copy()) + img_dil_er = cv2.erode(img_dil_er, SE) + axs[1, im_ix].imshow(img_dil_er.copy(), cmap='gray') + axs[1, im_ix].set(title=f"after {im_ix + 1} iterations of Closing, SE: {SE_size} x {SE_size}") + SE_size +=1 - for im_ix in range(4): - axs[1, im_ix].imshow(imgs[im_ix], cmap='gray') - axs[1, im_ix].set(title=f"Image after {im_ix + 1} iterations of Closing") - - imgs = [] + SE_size = 2 for im_ix in range(4): img_comb = cv2.erode(img_comb, SE) img_comb = cv2.dilate(img_comb, SE) img_comb = cv2.dilate(img_comb, SE) img_comb = cv2.erode(img_comb, SE) - imgs.append(img_comb) + axs[2, im_ix].imshow(img_comb.copy(), cmap='gray') + axs[2, im_ix].set(title=f"after {im_ix + 1} iterations of O+C, SE: {SE_size} x {SE_size}") + SE_size +=1 - for im_ix in range(4): - axs[2, im_ix].imshow(imgs[im_ix], cmap='gray') - axs[2, im_ix].set(title=f"Image after {im_ix + 1} iterations of O + C") plt.show() + + + def three_b() -> None: + """ + Try to clean up the mask of the image bird.jpg using morphological operations + as shown in the image. Experiment with different sizes of the structuring element. + You can also try different shapes, like cv2.getStructuringElement(cv2.MORPH_- + ELLIPSE,(n,n)). + """ + def main() -> None: