Spaces:
Sleeping
Sleeping
Update pages/Data Collection.py
Browse files- pages/Data Collection.py +190 -53
pages/Data Collection.py
CHANGED
|
@@ -460,7 +460,7 @@ def image_details_page():
|
|
| 460 |
|
| 461 |
# Sidebar for navigation
|
| 462 |
st.sidebar.title("Navigation")
|
| 463 |
-
page = st.sidebar.radio("Go to", ["Introduction", "Basic Operations","
|
| 464 |
|
| 465 |
# App Title and Description
|
| 466 |
st.title("Image Processing Fundamentals")
|
|
@@ -503,7 +503,28 @@ def image_details_page():
|
|
| 503 |
- **In images**: Each row represents a set of data points (pixels), and the columns represent their features.
|
| 504 |
- **In tables**:Each row represents an individual data point, and each column corresponds to a feature of that data point.
|
| 505 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 506 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 507 |
st.header("Color Spaces")
|
| 508 |
|
| 509 |
# Explanation for Color Spaces
|
|
@@ -580,7 +601,10 @@ def image_details_page():
|
|
| 580 |
""")
|
| 581 |
|
| 582 |
# Theory Section
|
| 583 |
-
st.
|
|
|
|
|
|
|
|
|
|
| 584 |
st.markdown("""
|
| 585 |
- It converts an 2D image into Machine representation value array.
|
| 586 |
- **cv2.imread("path)** this method going to convert image to 3D aray as it used default colour space **RGB**.
|
|
@@ -599,7 +623,10 @@ def image_details_page():
|
|
| 599 |
|
| 600 |
|
| 601 |
# Theory Section
|
| 602 |
-
st.
|
|
|
|
|
|
|
|
|
|
| 603 |
st.markdown("""
|
| 604 |
- After creating or reading an image, we can display it using OpenCV. Here’s how the key functions work together:
|
| 605 |
- The `imshow()` function creates a pop-up window to display the image.
|
|
@@ -610,21 +637,31 @@ def image_details_page():
|
|
| 610 |
""")
|
| 611 |
|
| 612 |
# Theory Section
|
| 613 |
-
st.
|
|
|
|
|
|
|
| 614 |
st.markdown("""
|
| 615 |
- The main purpose Waits for a key press and adds a delay before closing the pop-up window.
|
| 616 |
- `waitKey(0)` or `waitKey()` Keeps the window open indefinitely until a key is pressed.
|
| 617 |
-
- `waitkey(10) After 10 milli seconds the pop up window will be closed when we use
|
| 618 |
""")
|
| 619 |
|
| 620 |
# Theory Section
|
| 621 |
-
st.
|
|
|
|
|
|
|
| 622 |
st.markdown("""
|
| 623 |
- **Purpose**: Closes all OpenCV-created windows.
|
| 624 |
- **Usage**:This makes sure that memory is cleared and helps avoid crashes by getting rid of resources when the image is no longer needed.
|
| 625 |
""")
|
| 626 |
|
| 627 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 628 |
|
| 629 |
|
| 630 |
st.code("""
|
|
@@ -649,83 +686,183 @@ def image_details_page():
|
|
| 649 |
- Use `cv2.destroyAllWindows()` to close all pop-up windows and release system resources properly.
|
| 650 |
""")
|
| 651 |
|
|
|
|
|
|
|
|
|
|
| 652 |
|
| 653 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 654 |
|
| 655 |
from PIL import Image
|
| 656 |
|
| 657 |
# Title of the app
|
| 658 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 659 |
|
| 660 |
# Display the code
|
| 661 |
-
st.subheader("Code to Create and Display Black & White Images")
|
| 662 |
st.code("""
|
| 663 |
import numpy as np
|
| 664 |
import streamlit as st
|
| 665 |
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
cv2.
|
| 672 |
-
cv2.
|
| 673 |
-
cv2.destroyAllWindows()
|
| 674 |
-
|
| 675 |
-
cv2.imshow("Black image", black_img)
|
| 676 |
-
cv2.waitKey(0)
|
| 677 |
-
cv2.destroyAllWindows()
|
| 678 |
""", language="python")
|
| 679 |
|
| 680 |
|
| 681 |
# Section 1: Grayscale Image
|
| 682 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 683 |
st.code("""
|
| 684 |
# Grayscale image creation
|
| 685 |
-
gray_img = np.full((500, 500), 155, dtype=np.uint8)
|
| 686 |
|
| 687 |
# Display in OpenCV
|
| 688 |
-
cv2.imshow("Gray Image", gray_img)
|
| 689 |
cv2.waitKey(0)
|
| 690 |
cv2.destroyAllWindows()
|
| 691 |
""", language="python")
|
| 692 |
|
| 693 |
-
# Section 2:
|
| 694 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 695 |
st.code("""
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
|
|
|
| 712 |
""", language="python")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 713 |
|
|
|
|
| 714 |
st.code("""
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
|
|
|
| 719 |
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
|
|
|
| 723 |
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 727 |
""", language="python")
|
| 728 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 729 |
if st.button("Go Back"):
|
| 730 |
st.experimental_set_query_params(page="Introduction")
|
| 731 |
|
|
|
|
| 460 |
|
| 461 |
# Sidebar for navigation
|
| 462 |
st.sidebar.title("Navigation")
|
| 463 |
+
page = st.sidebar.radio("Go to", ["Introduction", "Basic Operations","Image Conversions"])
|
| 464 |
|
| 465 |
# App Title and Description
|
| 466 |
st.title("Image Processing Fundamentals")
|
|
|
|
| 503 |
- **In images**: Each row represents a set of data points (pixels), and the columns represent their features.
|
| 504 |
- **In tables**:Each row represents an individual data point, and each column corresponds to a feature of that data point.
|
| 505 |
""")
|
| 506 |
+
|
| 507 |
+
st.subheader("Interactive Pixel Grid")
|
| 508 |
+
|
| 509 |
+
# User Input for Height and Width
|
| 510 |
+
height = st.number_input("Enter Image Height (pixels):", min_value=1, max_value=50, value=10, step=1)
|
| 511 |
+
width = st.number_input("Enter Image Width (pixels):", min_value=1, max_value=50, value=10, step=1)
|
| 512 |
+
|
| 513 |
+
# Display Resolution
|
| 514 |
+
resolution = height * width
|
| 515 |
+
st.write(f"**Image Resolution**: {resolution} pixels")
|
| 516 |
|
| 517 |
+
# Generate and Display Pixel Grid
|
| 518 |
+
st.write("**Pixel Grid Visualization:**")
|
| 519 |
+
grid = np.random.rand(int(height), int(width)) # Generate random grid values
|
| 520 |
+
fig, ax = plt.subplots()
|
| 521 |
+
cax = ax.imshow(grid, cmap="magma")
|
| 522 |
+
plt.colorbar(cax, ax=ax) # Add color bar for context
|
| 523 |
+
ax.set_title("Pixel Grid")
|
| 524 |
+
ax.set_xlabel("Width(pixels)", fontsize=8) # Set smaller font size
|
| 525 |
+
ax.set_ylabel("Height(pixels)", fontsize=8) # Set smaller font size
|
| 526 |
+
|
| 527 |
+
|
| 528 |
st.header("Color Spaces")
|
| 529 |
|
| 530 |
# Explanation for Color Spaces
|
|
|
|
| 601 |
""")
|
| 602 |
|
| 603 |
# Theory Section
|
| 604 |
+
st.markdown("""
|
| 605 |
+
<h3 style="color: #9400d3;"> Reading an image</h3>
|
| 606 |
+
""", unsafe_allow_html=True)
|
| 607 |
+
|
| 608 |
st.markdown("""
|
| 609 |
- It converts an 2D image into Machine representation value array.
|
| 610 |
- **cv2.imread("path)** this method going to convert image to 3D aray as it used default colour space **RGB**.
|
|
|
|
| 623 |
|
| 624 |
|
| 625 |
# Theory Section
|
| 626 |
+
st.markdown("""
|
| 627 |
+
<h3 style="color: #FF7F00;"> imshow()</h3>
|
| 628 |
+
""", unsafe_allow_html=True)
|
| 629 |
+
|
| 630 |
st.markdown("""
|
| 631 |
- After creating or reading an image, we can display it using OpenCV. Here’s how the key functions work together:
|
| 632 |
- The `imshow()` function creates a pop-up window to display the image.
|
|
|
|
| 637 |
""")
|
| 638 |
|
| 639 |
# Theory Section
|
| 640 |
+
st.markdown("""
|
| 641 |
+
<h3 style="color: #FF7F00;"> waitkey()</h3>
|
| 642 |
+
""", unsafe_allow_html=True)
|
| 643 |
st.markdown("""
|
| 644 |
- The main purpose Waits for a key press and adds a delay before closing the pop-up window.
|
| 645 |
- `waitKey(0)` or `waitKey()` Keeps the window open indefinitely until a key is pressed.
|
| 646 |
+
- `waitkey(10)` After 10 milli seconds the pop up window will be closed when we use waitkey(n) after n milliseconds window closes.
|
| 647 |
""")
|
| 648 |
|
| 649 |
# Theory Section
|
| 650 |
+
st.markdown("""
|
| 651 |
+
<h3 style="color: #FF7F00;"> destroyAllWindows()</h3>
|
| 652 |
+
""", unsafe_allow_html=True)
|
| 653 |
st.markdown("""
|
| 654 |
- **Purpose**: Closes all OpenCV-created windows.
|
| 655 |
- **Usage**:This makes sure that memory is cleared and helps avoid crashes by getting rid of resources when the image is no longer needed.
|
| 656 |
""")
|
| 657 |
|
| 658 |
+
st.code("""
|
| 659 |
+
cv2.destroyAllWindows() # When we give this all temporary windows will be closed
|
| 660 |
+
""")
|
| 661 |
+
|
| 662 |
+
st.markdown("""
|
| 663 |
+
<h5 style='color: green;'>These three functions must work together to display and manage images effectively. /h5>
|
| 664 |
+
""", unsafe_allow_html=True)
|
| 665 |
|
| 666 |
|
| 667 |
st.code("""
|
|
|
|
| 686 |
- Use `cv2.destroyAllWindows()` to close all pop-up windows and release system resources properly.
|
| 687 |
""")
|
| 688 |
|
| 689 |
+
st.markdown("""
|
| 690 |
+
<h3 style="color: #9400d3;">Saving an Image</h3>
|
| 691 |
+
""", unsafe_allow_html=True)
|
| 692 |
|
| 693 |
+
# About imwrite() function
|
| 694 |
+
st.write("""
|
| 695 |
+
To save an image file in OpenCV, we use the **imwrite()** function.
|
| 696 |
+
It converts the numerical array (image data) back into an image file format, such as `.jpg`, `.png`, or `.bmp`.
|
| 697 |
+
""")
|
| 698 |
+
|
| 699 |
+
# Code example
|
| 700 |
+
st.code("""
|
| 701 |
+
|
| 702 |
+
cv2.imwrite('image.jpg', image_array) # 'image.jpg' it is the name of the output file
|
| 703 |
+
print("Wow your image is saved!")
|
| 704 |
+
""", language="python")
|
| 705 |
+
|
| 706 |
+
elif page =="Image Conversions":
|
| 707 |
|
| 708 |
from PIL import Image
|
| 709 |
|
| 710 |
# Title of the app
|
| 711 |
+
st.markdown("""
|
| 712 |
+
<h3 style="color: #9400d3;">Creating a Black and White Image</h3>
|
| 713 |
+
""", unsafe_allow_html=True)
|
| 714 |
+
|
| 715 |
+
# Explanation
|
| 716 |
+
st.write("""
|
| 717 |
+
In OpenCV, black and white images are created by filling a matrix with pixel values:
|
| 718 |
+
- **Black image**: When the pixel values are set to 0.
|
| 719 |
+
- **White image**: When the pixel values are set to 255.
|
| 720 |
+
""")
|
| 721 |
|
| 722 |
# Display the code
|
|
|
|
| 723 |
st.code("""
|
| 724 |
import numpy as np
|
| 725 |
import streamlit as st
|
| 726 |
|
| 727 |
+
white_img= np.full((500,500),255,dtype=np.uint8)
|
| 728 |
+
black_img = np.zeros((500,500),dtype=np.uint8)
|
| 729 |
+
|
| 730 |
+
cv2.imshow("white",white_img) #white image is displayed
|
| 731 |
+
cv2.imshow("black",black_img) #black image is displayed
|
| 732 |
+
cv2.waitKey() # until we close the window it displays the image
|
| 733 |
+
cv2.destroyAllWindows() # Close all temporary windows
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 734 |
""", language="python")
|
| 735 |
|
| 736 |
|
| 737 |
# Section 1: Grayscale Image
|
| 738 |
+
st.markdown("""
|
| 739 |
+
<h3 style="color: #9400d3;">Creating a Grayscale Image</h3>
|
| 740 |
+
""", unsafe_allow_html=True)
|
| 741 |
+
|
| 742 |
+
st.write("""
|
| 743 |
+
In a grayscale image, 0 is black, 255 is white, and pixel values between 1 and 254 represent varying shades of gray
|
| 744 |
+
""")
|
| 745 |
+
|
| 746 |
+
|
| 747 |
st.code("""
|
| 748 |
# Grayscale image creation
|
| 749 |
+
gray_img = np.full((500, 500), 155, dtype=np.uint8) #155 is a medium-light gray, closer to white than black.
|
| 750 |
|
| 751 |
# Display in OpenCV
|
| 752 |
+
cv2.imshow("Gray Image", gray_img) #Gray scale image is created
|
| 753 |
cv2.waitKey(0)
|
| 754 |
cv2.destroyAllWindows()
|
| 755 |
""", language="python")
|
| 756 |
|
| 757 |
+
# Section 2: Merge colors
|
| 758 |
+
|
| 759 |
+
st.markdown("""
|
| 760 |
+
<h3 style="color: #e25822;">Combining Color Channels</h3>
|
| 761 |
+
""", unsafe_allow_html=True)
|
| 762 |
+
|
| 763 |
+
# About cv2.merge() function
|
| 764 |
+
st.write("""
|
| 765 |
+
To create a full color image from separate single-channel images (such as Red, Green, and Blue), the **cv2.merge()** function is used.
|
| 766 |
+
It combines individual color channels into a single, complete color image.
|
| 767 |
+
""")
|
| 768 |
+
|
| 769 |
+
# Syntax example for cv2.merge()
|
| 770 |
+
st.code("""
|
| 771 |
+
# Combining individual color channels (Blue, Green, Red)
|
| 772 |
+
merged_image = cv2.merge([blue_channel, green_channel, red_channel])
|
| 773 |
+
# blue_channel, green_channel, red_channel are individual images representing the Blue, Green, and Red color channels
|
| 774 |
+
""", language="python")
|
| 775 |
+
|
| 776 |
+
|
| 777 |
+
st.markdown("""
|
| 778 |
+
<h3 style="color: #9400d3;">Creating a BGR image</h3>
|
| 779 |
+
""", unsafe_allow_html=True)
|
| 780 |
+
|
| 781 |
+
st.write("""
|
| 782 |
+
- The **Red channel** has pixel values with red set to 255, and green and blue to 0.
|
| 783 |
+
- The **Green channel** has pixel values with green set to 255, and red and blue to 0.
|
| 784 |
+
- The **Blue channel** has pixel values with blue set to 255, and red and green to 0.
|
| 785 |
+
- When merged, these channels form a complete color image.
|
| 786 |
+
""")
|
| 787 |
+
|
| 788 |
+
st.markdown("""
|
| 789 |
+
<p style="color: #FF6347;">To represent a coloured image, we have to convert the image into a 3D array. The mixture of three 2D arrays is <strong style="color: #1E90FF;">RGB</strong>.</p>
|
| 790 |
+
""", unsafe_allow_html=True)
|
| 791 |
+
|
| 792 |
+
|
| 793 |
st.code("""
|
| 794 |
+
|
| 795 |
+
# Create individual color channels
|
| 796 |
+
b = np.full((200, 200), 255, dtype=np.uint8) # Blue channel
|
| 797 |
+
g = np.zeros((200, 200), dtype=np.uint8) # Green channel
|
| 798 |
+
r = np.zeros((200, 200), dtype=np.uint8) # Red channel
|
| 799 |
+
|
| 800 |
+
# Merge the color channels to create RGB images
|
| 801 |
+
b_img = cv2.merge([b, g, r]) # Blue image
|
| 802 |
+
g_img = cv2.merge([g, b, r]) # Green image
|
| 803 |
+
r_img = cv2.merge([r, g, b]) # Red image
|
| 804 |
+
|
| 805 |
+
# Display the images
|
| 806 |
+
cv2.imshow("Blue", b_img)
|
| 807 |
+
cv2.imshow("Green", g_img)
|
| 808 |
+
cv2.imshow("Red", r_img)
|
| 809 |
+
cv2.waitKey(0) # Wait until a key is pressed
|
| 810 |
+
cv2.destroyAllWindows() # Close all OpenCV windows
|
| 811 |
""", language="python")
|
| 812 |
+
|
| 813 |
+
st.markdown("""
|
| 814 |
+
<h3 style="color: #e25822;">Channel Splitting</h3>
|
| 815 |
+
""", unsafe_allow_html=True)
|
| 816 |
+
|
| 817 |
+
# About cv2.split() function
|
| 818 |
+
st.write("""
|
| 819 |
+
The `cv2.split()` function in OpenCV is used to separate an image into its individual color channels.
|
| 820 |
+
It generates separate single-channel arrays for each color, which can then be manipulated independently.
|
| 821 |
+
For example, it can divide an RGB image into its Red, Green, and Blue components.
|
| 822 |
+
""")
|
| 823 |
|
| 824 |
+
# Syntax for cv2.split() function
|
| 825 |
st.code("""
|
| 826 |
+
# Syntax for cv2.split()
|
| 827 |
+
channels = cv2.split(image)
|
| 828 |
+
# image: The input image (e.g., an RGB image).
|
| 829 |
+
# channels: A list of single-channel images (e.g., Blue, Green, Red).
|
| 830 |
+
""", language="python")
|
| 831 |
|
| 832 |
+
# Heading for the section
|
| 833 |
+
st.markdown("""
|
| 834 |
+
<h3 style="color: #9400d3;">Splitting and Recombining Color Channels</h3>
|
| 835 |
+
""", unsafe_allow_html=True)
|
| 836 |
|
| 837 |
+
# Code Example for Splitting and Merging Color Channels
|
| 838 |
+
st.code("""
|
| 839 |
+
img = cv2.imread("path of the image") # Load the image
|
| 840 |
+
|
| 841 |
+
b, g, r = cv2.split(img) # Separate the image into Blue, Green, and Red channels
|
| 842 |
+
|
| 843 |
+
zeros = np.zeros(img.shape[:-1], dtype=np.uint8) # Create a blank array for the empty channels
|
| 844 |
+
|
| 845 |
+
blue_channel = cv2.merge([b, zeros, zeros]) # The Blue channel has blue set to 255, and red and green to 0
|
| 846 |
+
green_channel = cv2.merge([zeros, g, zeros]) # The Green channel has green set to 255, and red and blue to 0
|
| 847 |
+
red_channel = cv2.merge([zeros, zeros, r]) # The Red channel has red set to 255, and green and blue to 0
|
| 848 |
+
|
| 849 |
+
# Show the separate color channels and the original image
|
| 850 |
+
cv2.imshow("Blue_channel", blue_channel)
|
| 851 |
+
cv2.imshow("Green_channel", green_channel)
|
| 852 |
+
cv2.imshow("Red_channel", red_channel)
|
| 853 |
+
cv2.imshow("Original_img", cv2.merge([b, g, r]))
|
| 854 |
+
cv2.waitKey(0)
|
| 855 |
+
cv2.destroyAllWindows()
|
| 856 |
""", language="python")
|
| 857 |
|
| 858 |
+
|
| 859 |
+
|
| 860 |
+
|
| 861 |
+
|
| 862 |
+
|
| 863 |
+
|
| 864 |
+
|
| 865 |
+
|
| 866 |
if st.button("Go Back"):
|
| 867 |
st.experimental_set_query_params(page="Introduction")
|
| 868 |
|