Update pages/Life_cycle_of_ML.py
Browse files- pages/Life_cycle_of_ML.py +47 -61
pages/Life_cycle_of_ML.py
CHANGED
|
@@ -749,7 +749,6 @@ pd.DataFrame(flattened_image.reshape(1, -1))""",language="python")
|
|
| 749 |
if st.button("About Affine Transformation"):
|
| 750 |
st.session_state.page = "Affine"
|
| 751 |
|
| 752 |
-
|
| 753 |
def affine_transformation_demo():
|
| 754 |
st.title("Affine Transformations and Cropping Demo")
|
| 755 |
|
|
@@ -768,68 +767,55 @@ def affine_transformation_demo():
|
|
| 768 |
|
| 769 |
st.image(image, caption="Original Image", use_column_width=True, channels="BGR")
|
| 770 |
|
| 771 |
-
transformation_type = st.selectbox(
|
| 772 |
-
"Select Transformation Type",
|
| 773 |
-
("Translation", "Rotation", "Scaling", "Shearing", "Cropping")
|
| 774 |
-
)
|
| 775 |
-
|
| 776 |
rows, cols = image.shape[:2]
|
| 777 |
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
|
| 781 |
-
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
|
| 790 |
-
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
|
| 813 |
-
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
else:
|
| 827 |
-
transformed_image = image
|
| 828 |
-
|
| 829 |
-
if transformation_type == "Cropping":
|
| 830 |
-
st.image(transformed_image, caption="Cropped Image", use_column_width=True, channels="BGR")
|
| 831 |
-
else:
|
| 832 |
-
st.image(transformed_image, caption=f"Transformed Image ({transformation_type})", use_column_width=True, channels="BGR")
|
| 833 |
|
| 834 |
|
| 835 |
|
|
|
|
| 749 |
if st.button("About Affine Transformation"):
|
| 750 |
st.session_state.page = "Affine"
|
| 751 |
|
|
|
|
| 752 |
def affine_transformation_demo():
|
| 753 |
st.title("Affine Transformations and Cropping Demo")
|
| 754 |
|
|
|
|
| 767 |
|
| 768 |
st.image(image, caption="Original Image", use_column_width=True, channels="BGR")
|
| 769 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 770 |
rows, cols = image.shape[:2]
|
| 771 |
|
| 772 |
+
st.write("### Translation")
|
| 773 |
+
st.write("Translation shifts the image in the x and y directions. The transformation matrix is defined as:")
|
| 774 |
+
st.code("""
|
| 775 |
+
[[1, 0, tx],
|
| 776 |
+
[0, 1, ty]]
|
| 777 |
+
""", language="text")
|
| 778 |
+
translation_matrix = np.float32([[1, 0, 50], [0, 1, 50]]) # Shift by 50 pixels right and down
|
| 779 |
+
translated_image = cv2.warpAffine(image, translation_matrix, (cols, rows))
|
| 780 |
+
st.image(translated_image, caption="Translated Image", use_column_width=True, channels="BGR")
|
| 781 |
+
|
| 782 |
+
st.write("### Rotation")
|
| 783 |
+
st.write("Rotation rotates the image around a center point by a specified angle. The transformation matrix is:")
|
| 784 |
+
st.code("""
|
| 785 |
+
[[cos(theta), -sin(theta), tx],
|
| 786 |
+
[sin(theta), cos(theta), ty]]
|
| 787 |
+
""", language="text")
|
| 788 |
+
center = (cols // 2, rows // 2)
|
| 789 |
+
rotation_matrix = cv2.getRotationMatrix2D(center, 45, 1) # Rotate by 45 degrees
|
| 790 |
+
rotated_image = cv2.warpAffine(image, rotation_matrix, (cols, rows))
|
| 791 |
+
st.image(rotated_image, caption="Rotated Image", use_column_width=True, channels="BGR")
|
| 792 |
+
|
| 793 |
+
st.write("### Scaling")
|
| 794 |
+
st.write("Scaling enlarges or reduces the image dimensions. The transformation matrix is:")
|
| 795 |
+
st.code("""
|
| 796 |
+
[[sx, 0, 0],
|
| 797 |
+
[ 0, sy, 0]]
|
| 798 |
+
""", language="text")
|
| 799 |
+
scaling_matrix = np.float32([[1.5, 0, 0], [0, 1.5, 0]]) # Scale by 1.5x
|
| 800 |
+
scaled_image = cv2.warpAffine(image, scaling_matrix, (int(cols * 1.5), int(rows * 1.5)))
|
| 801 |
+
st.image(scaled_image, caption="Scaled Image", use_column_width=True, channels="BGR")
|
| 802 |
+
|
| 803 |
+
st.write("### Shearing")
|
| 804 |
+
st.write("Shearing slants the image along the x or y direction. The transformation matrix is:")
|
| 805 |
+
st.code("""
|
| 806 |
+
[[1, shx, 0],
|
| 807 |
+
[shy, 1, 0]]
|
| 808 |
+
""", language="text")
|
| 809 |
+
shearing_matrix = np.float32([[1, 0.2, 0], [0.2, 1, 0]]) # Shear in both x and y directions
|
| 810 |
+
sheared_image = cv2.warpAffine(image, shearing_matrix, (cols, rows))
|
| 811 |
+
st.image(sheared_image, caption="Sheared Image", use_column_width=True, channels="BGR")
|
| 812 |
+
|
| 813 |
+
st.write("### Cropping")
|
| 814 |
+
st.write("Cropping extracts a specific region of interest (ROI) from the image without requiring a transformation matrix. Instead, slicing is used to select a portion of the image.")
|
| 815 |
+
x_start, x_end = cols // 4, 3 * cols // 4
|
| 816 |
+
y_start, y_end = rows // 4, 3 * rows // 4
|
| 817 |
+
cropped_image = image[y_start:y_end, x_start:x_end]
|
| 818 |
+
st.image(cropped_image, caption="Cropped Image", use_column_width=True, channels="BGR")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 819 |
|
| 820 |
|
| 821 |
|