Update pages/Life_cycle_of_ML.py
Browse files- pages/Life_cycle_of_ML.py +105 -64
pages/Life_cycle_of_ML.py
CHANGED
|
@@ -794,82 +794,123 @@ pd.DataFrame(flattened_image.reshape(1, -1))""",language="python")
|
|
| 794 |
|
| 795 |
|
| 796 |
def affine_transformation_demo():
|
| 797 |
-
st.title("Affine Transformations and
|
| 798 |
|
| 799 |
-
st.write("
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
st.write("- **Shearing**: Slants the image along the x or y direction.")
|
| 804 |
-
st.write("- **Cropping**: Extracts a specific region of interest (ROI).")
|
| 805 |
|
| 806 |
-
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 807 |
|
| 808 |
if uploaded_file:
|
|
|
|
| 809 |
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 810 |
image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
| 811 |
-
|
| 812 |
-
st.image(image, caption="Original Image", channels="BGR")
|
| 813 |
|
| 814 |
rows, cols = image.shape[:2]
|
| 815 |
|
| 816 |
-
|
| 817 |
-
st.write("
|
| 818 |
-
st.
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 825 |
translated_image = cv2.warpAffine(image, translation_matrix, (cols, rows))
|
| 826 |
-
st.image(translated_image, caption="Translated Image", channels="BGR")
|
| 827 |
-
|
| 828 |
-
|
| 829 |
-
st.write("
|
| 830 |
-
st.
|
| 831 |
-
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
|
| 837 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 838 |
rotated_image = cv2.warpAffine(image, rotation_matrix, (cols, rows))
|
| 839 |
-
st.image(rotated_image, caption="Rotated Image", channels="BGR")
|
| 840 |
-
|
| 841 |
-
|
| 842 |
-
st.write("
|
| 843 |
-
st.
|
| 844 |
-
|
| 845 |
-
|
| 846 |
-
|
| 847 |
-
|
| 848 |
-
|
| 849 |
-
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
|
| 860 |
-
st.
|
| 861 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 862 |
sheared_image = cv2.warpAffine(image, shearing_matrix, (cols, rows))
|
| 863 |
-
st.image(sheared_image, caption="Sheared Image", channels="BGR")
|
| 864 |
-
|
| 865 |
-
|
| 866 |
-
st.write("
|
| 867 |
-
st.write("
|
| 868 |
-
|
| 869 |
-
|
| 870 |
-
|
| 871 |
-
|
| 872 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 873 |
|
| 874 |
ipynb_file_path = "Affine_Transformation.ipynb"
|
| 875 |
|
|
|
|
| 794 |
|
| 795 |
|
| 796 |
def affine_transformation_demo():
|
| 797 |
+
st.title("Affine Transformations and Their Mathematics")
|
| 798 |
|
| 799 |
+
st.write("""
|
| 800 |
+
Affine transformations are geometric transformations that preserve points, straight lines, and planes.
|
| 801 |
+
These include Translation, Rotation, Scaling, Shearing, and Cropping. Let's explore them step by step.
|
| 802 |
+
""")
|
|
|
|
|
|
|
| 803 |
|
| 804 |
+
uploaded_file = st.file_uploader("Upload an image (JPG/PNG)", type=["jpg", "jpeg", "png"])
|
| 805 |
|
| 806 |
if uploaded_file:
|
| 807 |
+
# Read uploaded image
|
| 808 |
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 809 |
image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
| 810 |
+
st.image(image, caption="Original Image", channels="BGR", use_column_width=True)
|
|
|
|
| 811 |
|
| 812 |
rows, cols = image.shape[:2]
|
| 813 |
|
| 814 |
+
# **Translation**
|
| 815 |
+
st.write("## Translation")
|
| 816 |
+
st.write("""
|
| 817 |
+
Translation shifts the image in the \(x\) and \(y\) directions. The transformation matrix is:
|
| 818 |
+
|
| 819 |
+
\\[
|
| 820 |
+
\\mathbf{T} =
|
| 821 |
+
\\begin{bmatrix}
|
| 822 |
+
1 & 0 & t_x \\\\
|
| 823 |
+
0 & 1 & t_y
|
| 824 |
+
\\end{bmatrix}
|
| 825 |
+
\\]
|
| 826 |
+
|
| 827 |
+
Here, \(t_x\) is the translation in the \(x\)-direction, and \(t_y\) is the translation in the \(y\)-direction.
|
| 828 |
+
""")
|
| 829 |
+
|
| 830 |
+
tx = st.slider("Translation in X (pixels)", -100, 100, 50)
|
| 831 |
+
ty = st.slider("Translation in Y (pixels)", -100, 100, 50)
|
| 832 |
+
translation_matrix = np.float32([[1, 0, tx], [0, 1, ty]])
|
| 833 |
translated_image = cv2.warpAffine(image, translation_matrix, (cols, rows))
|
| 834 |
+
st.image(translated_image, caption="Translated Image", channels="BGR", use_column_width=True)
|
| 835 |
+
|
| 836 |
+
# **Rotation**
|
| 837 |
+
st.write("## Rotation")
|
| 838 |
+
st.write("""
|
| 839 |
+
Rotation rotates the image by an angle θ around a center point. The transformation matrix is:
|
| 840 |
+
|
| 841 |
+
\\[
|
| 842 |
+
\\mathbf{R} =
|
| 843 |
+
\\begin{bmatrix}
|
| 844 |
+
\\cos\\theta & -\\sin\\theta & t_x \\\\
|
| 845 |
+
\\sin\\theta & \\cos\\theta & t_y
|
| 846 |
+
\\end{bmatrix}
|
| 847 |
+
\\]
|
| 848 |
+
|
| 849 |
+
Here, θ is the rotation angle (in degrees).
|
| 850 |
+
""")
|
| 851 |
+
|
| 852 |
+
angle = st.slider("Rotation Angle (degrees)", -180, 180, 45)
|
| 853 |
+
rotation_center = (cols // 2, rows // 2)
|
| 854 |
+
rotation_matrix = cv2.getRotationMatrix2D(rotation_center, angle, 1)
|
| 855 |
rotated_image = cv2.warpAffine(image, rotation_matrix, (cols, rows))
|
| 856 |
+
st.image(rotated_image, caption="Rotated Image", channels="BGR", use_column_width=True)
|
| 857 |
+
|
| 858 |
+
# **Scaling**
|
| 859 |
+
st.write("## Scaling")
|
| 860 |
+
st.write("""
|
| 861 |
+
Scaling adjusts the image size. The transformation matrix is:
|
| 862 |
+
|
| 863 |
+
\\[
|
| 864 |
+
\\mathbf{S} =
|
| 865 |
+
\\begin{bmatrix}
|
| 866 |
+
s_x & 0 & 0 \\\\
|
| 867 |
+
0 & s_y & 0
|
| 868 |
+
\\end{bmatrix}
|
| 869 |
+
\\]
|
| 870 |
+
|
| 871 |
+
Here, \(s_x\) and \(s_y\) are the scaling factors in the \(x\) and \(y\)-directions.
|
| 872 |
+
""")
|
| 873 |
+
|
| 874 |
+
sx = st.slider("Scale Factor in X", 0.5, 2.0, 1.0)
|
| 875 |
+
sy = st.slider("Scale Factor in Y", 0.5, 2.0, 1.0)
|
| 876 |
+
scaled_image = cv2.resize(image, (int(cols * sx), int(rows * sy)))
|
| 877 |
+
st.image(scaled_image, caption="Scaled Image", channels="BGR", use_column_width=True)
|
| 878 |
+
|
| 879 |
+
# **Shearing**
|
| 880 |
+
st.write("## Shearing")
|
| 881 |
+
st.write("""
|
| 882 |
+
Shearing skews the image along the \(x\)- or \(y\)-axis. The transformation matrix is:
|
| 883 |
+
|
| 884 |
+
\\[
|
| 885 |
+
\\mathbf{H} =
|
| 886 |
+
\\begin{bmatrix}
|
| 887 |
+
1 & s_x & 0 \\\\
|
| 888 |
+
s_y & 1 & 0
|
| 889 |
+
\\end{bmatrix}
|
| 890 |
+
\\]
|
| 891 |
+
|
| 892 |
+
Here, \(s_x\) and \(s_y\) control the shear amount in the \(x\)- and \(y\)-directions.
|
| 893 |
+
""")
|
| 894 |
+
|
| 895 |
+
shx = st.slider("Shear Factor in X", -0.5, 0.5, 0.2)
|
| 896 |
+
shy = st.slider("Shear Factor in Y", -0.5, 0.5, 0.2)
|
| 897 |
+
shearing_matrix = np.float32([[1, shx, 0], [shy, 1, 0]])
|
| 898 |
sheared_image = cv2.warpAffine(image, shearing_matrix, (cols, rows))
|
| 899 |
+
st.image(sheared_image, caption="Sheared Image", channels="BGR", use_column_width=True)
|
| 900 |
+
|
| 901 |
+
# **Cropping**
|
| 902 |
+
st.write("## Cropping")
|
| 903 |
+
st.write("""
|
| 904 |
+
Cropping extracts a region of interest (ROI) from the image by selecting specific coordinates.
|
| 905 |
+
""")
|
| 906 |
+
|
| 907 |
+
x1 = st.slider("Crop Start (X-axis)", 0, cols - 1, cols // 4)
|
| 908 |
+
x2 = st.slider("Crop End (X-axis)", x1 + 1, cols, 3 * cols // 4)
|
| 909 |
+
y1 = st.slider("Crop Start (Y-axis)", 0, rows - 1, rows // 4)
|
| 910 |
+
y2 = st.slider("Crop End (Y-axis)", y1 + 1, rows, 3 * rows // 4)
|
| 911 |
+
|
| 912 |
+
cropped_image = image[y1:y2, x1:x2]
|
| 913 |
+
st.image(cropped_image, caption="Cropped Image", channels="BGR", use_column_width=True)
|
| 914 |
|
| 915 |
ipynb_file_path = "Affine_Transformation.ipynb"
|
| 916 |
|