Zero_to_Hero_Machine_Learning / pages /How to work on Image.py
shwetashweta05's picture
Update pages/How to work on Image.py
8c8a9ff verified
raw
history blame
4.58 kB
import streamlit as st
import pandas as pd
st.subheader(":red[**how to convert into grey scale**]")
st.write("To convert an image to grayscale using OpenCV, you can use the cv2.cvtColor() function with the color conversion code cv2.COLOR_BGR2GRAY.")
code="""
import cv2
# Read the original image
image = cv2.imread("example.jpg")
# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Display the original and grayscale images
cv2.imshow("Original Image", image)
cv2.imshow("Grayscale Image", gray_image)
# Wait for a key press and close all windows
cv2.waitKey(0)
cv2.destroyAllWindows()
# Save the grayscale image
cv2.imwrite("gray_example.jpg", gray_image)
"""
st.code(code,language="python")
st.write("""
**1.cv2.imread("example.jpg"):**
- Reads the image in BGR (Blue-Green-Red) format by default.
**2.cv2.cvtColor(image, cv2.COLOR_BGR2GRAY):**
- Converts the image from BGR color space to a single-channel grayscale image.
**3.cv2.imshow():**
- Displays the original and grayscale images in separate windows.
**4.cv2.imwrite():**
- Saves the grayscale image to a file.
""")
st.subheader(":red[**how to convert image into color space**]")
st.write("To convert an image from one color space to another using OpenCV, you can use the cv2.cvtColor() function. OpenCV provides various color conversion codes, such as converting an image to HSV, LAB, RGB, and more.")
code="""
import cv2
# Read the original image
image = cv2.imread("example.jpg")
# Convert to different color spaces
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # Convert to HSV
lab_image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB) # Convert to LAB
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Convert to RGB
# Display the original and converted images
cv2.imshow("Original Image", image)
cv2.imshow("HSV Image", hsv_image)
cv2.imshow("LAB Image", lab_image)
cv2.imshow("RGB Image", rgb_image)
# Wait for a key press and close all windows
cv2.waitKey(0)
cv2.destroyAllWindows()
# Save the converted images
cv2.imwrite("hsv_example.jpg", hsv_image)
cv2.imwrite("lab_example.jpg", lab_image)
cv2.imwrite("rgb_example.jpg", rgb_image)
"""
st.code(code,language="python")
st.write("**How to split**")
st.write("In OpenCV, splitting an image refers to separating the channels of a multi-channel image (such as RGB or HSV). Each channel is represented as a single grayscale image. This can be done using the cv2.split() function.")
code="""
import cv2
# Read the image
image = cv2.imread("example.jpg")
# Split the image into its channels
b, g, r = cv2.split(image) # For an RGB/BGR image
# Display each channel
cv2.imshow("Blue Channel", b)
cv2.imshow("Green Channel", g)
cv2.imshow("Red Channel", r)
# Wait for a key press and close all windows
cv2.waitKey(0)
cv2.destroyAllWindows()
# Save the individual channels
cv2.imwrite("blue_channel.jpg", b)
cv2.imwrite("green_channel.jpg", g)
cv2.imwrite("red_channel.jpg", r)
"""
st.code(code,language="python")
st.write("""
**1.cv2.split(image):**
- Splits the image into its individual channels.
- For a BGR image, the output will be three separate arrays: Blue, Green, and Red.
""")
st.write("**How to merge**")
st.write("In OpenCV, merging refers to combining individual image channels (e.g., Blue, Green, and Red) back into a single multi-channel image. You can use the cv2.merge() function for this purpose.")
code="""
#sytax
merged_image = cv2.merge((channel1, channel2, channel3))
"""
st.code(code,language="python")
st.write("Here, channel1, channel2, and channel3 are the individual channels to merge. Their order depends on the color space (e.g., for BGR, it should be Blue, Green, Red).")
st.write("**Code: Merging Channels**")
code="""
import cv2
# Read the image
image = cv2.imread("example.jpg")
# Split the image into channels
b, g, r = cv2.split(image)
# Merge the channels back into a single image
merged_image = cv2.merge((b, g, r))
# Display the merged image
cv2.imshow("Merged Image", merged_image)
# Save the merged image
cv2.imwrite("merged_image.jpg", merged_image)
# Wait for a key press and close all windows
cv2.waitKey(0)
cv2.destroyAllWindows()
"""
st.write("""
**1.cv2.split(image):**
- Splits the image into its respective channels.
- For an RGB/BGR image, you get three separate grayscale images: Blue, Green, and Red channels.
**2.cv2.merge((b, g, r)):**
- Combines the individual channels back into a single BGR image.
**3.Displaying the Image:**
- Use cv2.imshow() to display the merged image.
""")