Zero_to_Hero_Machine_Learning / pages /Image augmentation.py
shwetashweta05's picture
Update pages/Image augmentation.py
829ce11 verified
import streamlit as st
import numpy as np
import pandas as pd
st.subheader(":red[**Image augmentation**]")
st.write("**What is Image augmentation?**")
st.write("image augmentation is a fun and effective way to improve computer vision models. Image augmentation involves applying various transformations to your training images to create new, slightly modified versions of the original data. This helps to increase the diversity of your training dataset and makes your model more robust to different variations in the input data.")
st.write("""
**we are going to implement the following image transformation:**
- Rotation
- Cropping
- Transformation
- Scaling
- Shearing
""")
st.write("**Image Translation -** Translation moves the image by a specified distance in the x- and y-directions.")
code="""
import numpy as np
import cv2
img=cv2.imread("shiv.jpg")
tx = 0
ty = 0
t_m = np.array([[1, 0, tx], [0, 1, ty]], dtype=np.float32)
t_img = cv2.warpAffine(img, t_m, (225, 225))
cv2.imshow("o_p", img)
cv2.imshow("t_i", t_img)
cv2.waitKey()
cv2.destroyAllWindows()
"""
st.code(code,language="python")
st.write("""
**Explanation:**
- tx and ty: Define the translation distances along the x- and y-axes. In this case, both are 0, so no translation occurs.
- t_m: Translation matrix. This defines the transformation for moving the image.
- cv2.warpAffine: Applies the translation to the image.
- Output: The original image and translated image (which looks the same due to 𝑑π‘₯=𝑑𝑦=0 tx=ty=0).
""")
st.image("translation.jpg")
st.write("**Image Rotation -** Rotation changes the orientation of the image around a specified point.")
code="""
r_m = cv2.getRotationMatrix2D((112, 112), 90, 1)
r_img1 = cv2.warpAffine(img, r_m, (225, 225))
cv2.imshow("o_p", img)
cv2.imshow("t_i", r_img1)
cv2.waitKey()
cv2.destroyAllWindows()
"""
st.code(code,language="python")
st.write("""
**Explanation:**
- cv2.getRotationMatrix2D:
- Center: The point around which the image is rotated. Here, it's (112,112)(112,112), which is roughly the center for a 225Γ—225 225Γ—225 image.
- Angle: 90 degrees, meaning the image is rotated clockwise.
- Scale: 1, so the image retains its original size.
- cv2.warpAffine: Rotates the image using the rotation matrix
- Output: The original image and the rotated image.
""")
st.image("rotation.jpg")
st.write("**Image Scaling- Scaling resizes the image either by a factor or to specified dimensions.**")
code="""
sx = 1
sy = 1
tx = 0
ty = 0
sc_m = np.array([[sx, 0, tx], [0, sy, ty]], dtype=np.float32)
scal_img = cv2.warpAffine(img, sc_m, (2*225, 2*225))
cv2.imshow("o_i", img)
cv2.imshow("s_im", scal_img)
cv2.waitKey()
cv2.destroyAllWindows()
"""
st.code(code,language="python")
st.write("""
**Explanation:**
- sx and sy: Scaling factors along x- and y-axes.sx=sy=1 means no scaling.
- sc_m: Scaling matrix.
- cv2.warpAffine: Applies the scaling transformation.
- Output: The original image and a scaled image (which appears unchanged because sx=sy=1).
""")
st.image("scaling.jpg")
st.write("**Image Shearing -** Shearing slants the shape of the image along the x- or y-axis.")
code="""
shx = 1
shy = 1
tx = 0
ty = 0
sh_m = np.array([[1, shx, tx], [shy, 1, ty]], dtype=np.float32)
scal_img = cv2.warpAffine(img, sh_m, (225, 225))
cv2.imshow("o_i", img)
cv2.imshow("s_im", scal_img)
cv2.waitKey()
cv2.destroyAllWindows()
"""
st.code(code,language="python")
st.write("""
**Explanation:**
- shx and shy: Shear factors along x- and y-axes. Here, both are set to 1, introducing significant distortion.
- sh_m: Shearing matrix.
- cv2.warpAffine: Applies the shearing transformation.
- Output: A sheared (slanted) version of the image.
""")
st.image("Shearing.png")