File size: 4,685 Bytes
9674445
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
{
 "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
}