{ "cells": [ { "cell_type": "code", "execution_count": 5, "id": "2a48ed33-0649-440a-a672-366eccecc595", "metadata": {}, "outputs": [], "source": [ "import cv2\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 9, "id": "79e94481-02c5-41df-b243-b5ab9d6215cf", "metadata": {}, "outputs": [], "source": [ "white_img=np.full((400,500),255,dtype=np.uint8) # Create a white image (400x500 pixels) with all values set to 255." ] }, { "cell_type": "code", "execution_count": 10, "id": "dafbe73b-1c9a-4ecd-a97b-273066fa722b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cv2.imwrite(r\"C:\\Users\\Singh\\Downloads\\flower_photos\\white.jpg\",white_img) # Save the white image as \"white.jpg\" in the specified directory." ] }, { "cell_type": "code", "execution_count": 13, "id": "84e50e23-dcac-4d6a-b1b9-a3cf10d4e4f0", "metadata": {}, "outputs": [], "source": [ "img=cv2.imread(r\"C:\\Users\\Singh\\Downloads\\flower_photos\\tulips\\54895006_55b49052dc.jpg\") # Load the image \"tulips\" from the specified directory." ] }, { "cell_type": "code", "execution_count": 14, "id": "2f465e89-1151-4e14-ad1d-14208ee8ba43", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(333, 500, 3)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "img.shape # Get the shape of the image (height, width, and color channels)." ] }, { "cell_type": "code", "execution_count": 17, "id": "0b34448d-47eb-45be-8595-b08f1a17716d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(333, 500)" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "img.shape[:-1] # Get only the height and width of the image by excluding the color channels." ] }, { "cell_type": "code", "execution_count": 16, "id": "39a4831e-4415-4699-a0ae-24222370e0c3", "metadata": {}, "outputs": [], "source": [ "b,g,r=cv2.split(img) # Split the image into its blue (b), green (g), and red (r) channels." ] }, { "cell_type": "code", "execution_count": 19, "id": "d56c4865-f80f-4cd7-af69-1b3437cb8315", "metadata": {}, "outputs": [], "source": [ "zeros=np.zeros(img.shape[:-1],dtype=np.uint8) # Create a zero matrix with the same height and width as the image for masking." ] }, { "cell_type": "code", "execution_count": 20, "id": "cd625553-7992-4706-aae7-4bbaaf9daee6", "metadata": {}, "outputs": [], "source": [ "blue_channel=cv2.merge([b,zeros,zeros]) # Create the blue channel by combining the blue channel and zero matrices for other channels.\n", "green_channel=cv2.merge([zeros,b,zeros]) # Create the green channel by combining the green channel and zero matrices for other channels.\n", "red_channel=cv2.merge([zeros,zeros,r]) # Create the red channel by combining the red channel and zero matrices for other channels." ] }, { "cell_type": "code", "execution_count": null, "id": "3c896f79-ec7d-4bcf-8db6-2e09d1551b6b", "metadata": {}, "outputs": [], "source": [ "cv2.imshow(\"blue_channel\",blue_channel) # Display the blue channel image in a window.\n", "cv2.imshow(\"green_channel\",green_channel) # Display the green channel image in a window.\n", "cv2.imshow(\"red_channel\",red_channel) # Display the red channel image in a window.\n", "cv2.imshow(\"original_channel\",cv2.merge([b,g,r])) # Display the original image by merging the blue, green, and red channels back together.\n", "\n", "cv2.waitKey() # Wait indefinitely until a key is pressed.\n", "cv2.destroyAllWindows() # Close all OpenCV windows." ] }, { "cell_type": "code", "execution_count": null, "id": "12381df8-94f1-4523-bbd3-d6d26052c5da", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.7" } }, "nbformat": 4, "nbformat_minor": 5 }