Phaneendrabayi commited on
Commit
d8a25c8
·
verified ·
1 Parent(s): 683da96

Update pages/Life_cycle_of_ML.py

Browse files
Files changed (1) hide show
  1. 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 Cropping Demo")
798
 
799
- st.write("Affine transformations are linear mapping methods that preserve points, straight lines, and planes. This demo showcases the following transformations:")
800
- st.write("- **Translation**: Shifts the image in the x and y directions.")
801
- st.write("- **Rotation**: Rotates the image around a center point.")
802
- st.write("- **Scaling**: Enlarges or reduces the dimensions of the image.")
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
- st.write("### Translation")
817
- st.write("Translation shifts the image in the x and y directions. The transformation matrix is defined as:")
818
- st.code("""
819
- [[1, 0, tx],
820
- [0, 1, ty]]
821
- """, language="text")
822
- st.write("**Advantages:** Simple and efficient for repositioning objects in an image.")
823
- st.write("**Disadvantages:** May result in parts of the image moving out of frame.")
824
- translation_matrix = np.float32([[1, 0, 50], [0, 1, 50]]) # Shift by 50 pixels right and down
 
 
 
 
 
 
 
 
 
 
825
  translated_image = cv2.warpAffine(image, translation_matrix, (cols, rows))
826
- st.image(translated_image, caption="Translated Image", channels="BGR")
827
-
828
- st.write("### Rotation")
829
- st.write("Rotation rotates the image around a center point by a specified angle. The transformation matrix is:")
830
- st.code("""
831
- [[cos(theta), -sin(theta), tx],
832
- [sin(theta), cos(theta), ty]]
833
- """, language="text")
834
- st.write("**Advantages:** Useful for correcting orientation or aligning objects.")
835
- st.write("**Disadvantages:** May introduce empty regions or distortions at the edges.")
836
- center = (cols // 2, rows // 2)
837
- rotation_matrix = cv2.getRotationMatrix2D(center, 45, 1) # Rotate by 45 degrees
 
 
 
 
 
 
 
 
 
838
  rotated_image = cv2.warpAffine(image, rotation_matrix, (cols, rows))
839
- st.image(rotated_image, caption="Rotated Image", channels="BGR")
840
-
841
- st.write("### Scaling")
842
- st.write("Scaling enlarges or reduces the image dimensions. The transformation matrix is:")
843
- st.code("""
844
- [[sx, 0, 0],
845
- [ 0, sy, 0]]
846
- """, language="text")
847
- st.write("**Advantages:** Effective for resizing images for analysis or display.")
848
- st.write("**Disadvantages:** May lead to loss of detail or pixelation when scaling up or down significantly.")
849
- scaling_matrix = np.float32([[1.5, 0, 0], [0, 1.5, 0]]) # Scale by 1.5x
850
- scaled_image = cv2.warpAffine(image, scaling_matrix, (int(cols * 1.5), int(rows * 1.5)))
851
- st.image(scaled_image, caption="Scaled Image", channels="BGR")
852
-
853
- st.write("### Shearing")
854
- st.write("Shearing slants the image along the x or y direction. The transformation matrix is:")
855
- st.code("""
856
- [[1, shx, 0],
857
- [shy, 1, 0]]
858
- """, language="text")
859
- st.write("**Advantages:** Introduces perspective distortion, useful for simulating 3D effects.")
860
- st.write("**Disadvantages:** Can make the image appear unnatural if overused.")
861
- shearing_matrix = np.float32([[1, 0.2, 0], [0.2, 1, 0]]) # Shear in both x and y directions
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
862
  sheared_image = cv2.warpAffine(image, shearing_matrix, (cols, rows))
863
- st.image(sheared_image, caption="Sheared Image", channels="BGR")
864
-
865
- st.write("### Cropping")
866
- 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.")
867
- st.write("**Advantages:** Simple and effective for focusing on specific areas of an image.")
868
- st.write("**Disadvantages:** Removes data outside the selected region, which may be irreversible.")
869
- x_start, x_end = cols // 4, 3 * cols // 4
870
- y_start, y_end = rows // 4, 3 * rows // 4
871
- cropped_image = image[y_start:y_end, x_start:x_end]
872
- st.image(cropped_image, caption="Cropped Image", channels="BGR")
 
 
 
 
 
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