Skip to content

Commit 6c5c16d

Browse files
Pre-Processing Techniques in DL
1 parent 764957c commit 6c5c16d

File tree

1 file changed

+379
-0
lines changed

1 file changed

+379
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,379 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "1db3b3cd",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"import cv2\n",
11+
"\n",
12+
"# Load an image\n",
13+
"image = cv2.imread('C:/Users/hp/R.jpeg')\n",
14+
"\n",
15+
"# Display the image\n",
16+
"cv2.imshow('Image', image)\n",
17+
"cv2.waitKey(0)\n",
18+
"cv2.destroyAllWindows()\n"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 2,
24+
"id": "2c109797",
25+
"metadata": {},
26+
"outputs": [],
27+
"source": [
28+
"import cv2\n",
29+
"image = cv2.imread('C:/Users/hp/R.jpeg')\n",
30+
"cv2.imshow('Image', image)\n",
31+
"#cv2.waitKey(0)\n",
32+
"#cv2.destroyAllWindows()"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 3,
38+
"id": "5626577e",
39+
"metadata": {},
40+
"outputs": [
41+
{
42+
"ename": "error",
43+
"evalue": "OpenCV(4.9.0) D:\\a\\opencv-python\\opencv-python\\opencv\\modules\\imgproc\\src\\resize.cpp:4152: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'\n",
44+
"output_type": "error",
45+
"traceback": [
46+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
47+
"\u001b[1;31merror\u001b[0m Traceback (most recent call last)",
48+
"Cell \u001b[1;32mIn[3], line 9\u001b[0m\n\u001b[0;32m 7\u001b[0m width \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m300\u001b[39m\n\u001b[0;32m 8\u001b[0m height \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m200\u001b[39m\n\u001b[1;32m----> 9\u001b[0m resized_image \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mresize(image, (width, height))\n\u001b[0;32m 11\u001b[0m \u001b[38;5;66;03m# Display the resized image\u001b[39;00m\n\u001b[0;32m 12\u001b[0m cv2\u001b[38;5;241m.\u001b[39mimshow(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mResized Image\u001b[39m\u001b[38;5;124m'\u001b[39m, resized_image)\n",
49+
"\u001b[1;31merror\u001b[0m: OpenCV(4.9.0) D:\\a\\opencv-python\\opencv-python\\opencv\\modules\\imgproc\\src\\resize.cpp:4152: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'\n"
50+
]
51+
}
52+
],
53+
"source": [
54+
"import cv2\n",
55+
"\n",
56+
"# Load an image\n",
57+
"image = cv2.imread('R.jpg')\n",
58+
"\n",
59+
"# Resize the image to a specific width and height\n",
60+
"width = 300\n",
61+
"height = 200\n",
62+
"resized_image = cv2.resize(image, (width, height))\n",
63+
"\n",
64+
"# Display the resized image\n",
65+
"cv2.imshow('Resized Image', resized_image)\n",
66+
"cv2.waitKey(0)\n",
67+
"cv2.destroyAllWindows()\n"
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": null,
73+
"id": "0f3f0beb",
74+
"metadata": {},
75+
"outputs": [],
76+
"source": [
77+
"import cv2\n",
78+
"\n",
79+
"# Load the image\n",
80+
"image = cv2.imread('R.jpg')\n",
81+
"\n",
82+
"# Define the coordinates of the region to be cropped (x, y, width, height)\n",
83+
"x, y, width, height = 100, 100, 300, 200\n",
84+
"\n",
85+
"# Crop the image\n",
86+
"cropped_image = image[y:y+height, x:x+width]\n",
87+
"\n",
88+
"# Display the cropped image\n",
89+
"cv2.imshow('Cropped Image', cropped_image)\n",
90+
"cv2.waitKey(0)\n",
91+
"cv2.destroyAllWindows()\n"
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": null,
97+
"id": "e0dcfc5c",
98+
"metadata": {},
99+
"outputs": [],
100+
"source": [
101+
"import cv2 \n",
102+
"import glob \n",
103+
"import os\n",
104+
"\n",
105+
"inputFolder = r'C:\\Users\\hp\\Desktop\\dl practice'\n",
106+
"outputFolder = r'C:\\Users\\hp\\Desktop\\dl practice\\resized'\n",
107+
"\n",
108+
"# Create the output directory if it doesn't exist\n",
109+
"if not os.path.exists(outputFolder):\n",
110+
" os.mkdir(outputFolder)\n",
111+
"\n",
112+
"for img_path in glob.glob(os.path.join(inputFolder, '*.JPEG')):\n",
113+
" # Read the image\n",
114+
" image = cv2.imread(img_path)\n",
115+
" # Resize the image\n",
116+
" imResized = cv2.resize(image, (224, 224))\n",
117+
" # Extract the filename\n",
118+
" filename = os.path.basename(img_path)\n",
119+
" # Write the resized image to the output folder\n",
120+
" cv2.imwrite(os.path.join(outputFolder, filename), imResized)\n",
121+
"\n",
122+
"print(\"Resizing complete.\")\n"
123+
]
124+
},
125+
{
126+
"cell_type": "code",
127+
"execution_count": null,
128+
"id": "b87145af",
129+
"metadata": {},
130+
"outputs": [],
131+
"source": [
132+
"import cv2\n",
133+
"import glob\n",
134+
"import os\n",
135+
"\n",
136+
"inputFolder = 'C:/Users/hp/Desktop/dl practice' # Corrected the path separator\n",
137+
"os.makedirs(inputFolder, exist_ok=True) # Corrected function name and made sure the directory exists\n",
138+
"\n",
139+
"for img_path in glob.glob(os.path.join(inputFolder, '*.JPEG')): # Corrected the path and file extension\n",
140+
" image = cv2.imread(img_path) # Corrected function name\n",
141+
" if image is not None: # Check if image is loaded successfully\n",
142+
" imResized = cv2.resize(image, (224, 224))\n",
143+
" new_img_path = os.path.join(inputFolder, f\"resized_{os.path.basename(img_path)}\") # Construct new file path\n",
144+
" cv2.imwrite(new_img_path, imResized) # Write the resized image\n",
145+
" print(f\"Resized and saved: {new_img_path}\")\n",
146+
" else:\n",
147+
" print(f\"Unable to read image: {img_path}\")\n"
148+
]
149+
},
150+
{
151+
"cell_type": "code",
152+
"execution_count": null,
153+
"id": "7cf7c0d7",
154+
"metadata": {},
155+
"outputs": [],
156+
"source": [
157+
"import cv2\n",
158+
"import glob\n",
159+
"import os\n",
160+
"\n",
161+
"inputFolder = 'C:/Users/hp/Desktop/dl practice' # Corrected the path separator\n",
162+
"os.makedirs(inputFolder, exist_ok=True) # Corrected function name and made sure the directory exists\n",
163+
"\n",
164+
"# Define the crop dimensions\n",
165+
"x_start, y_start, width, height = 50, 50, 150, 150 # Example values, adjust as needed\n",
166+
"\n",
167+
"for img_path in glob.glob(os.path.join(inputFolder, '*.JPEG')): # Corrected the path and file extension\n",
168+
" image = cv2.imread(img_path) # Corrected function name\n",
169+
" if image is not None: # Check if image is loaded successfully\n",
170+
" cropped_img = image[y_start:y_start+height, x_start:x_start+width]\n",
171+
" new_img_path = os.path.join(inputFolder, f\"cropped_{os.path.basename(img_path)}\") # Construct new file path\n",
172+
" cv2.imwrite(new_img_path, cropped_img) # Write the cropped image\n",
173+
" print(f\"Cropped and saved: {new_img_path}\")\n",
174+
" else:\n",
175+
" print(f\"Unable to read image: {img_path}\")\n"
176+
]
177+
},
178+
{
179+
"cell_type": "code",
180+
"execution_count": null,
181+
"id": "f299c243",
182+
"metadata": {},
183+
"outputs": [],
184+
"source": [
185+
"import cv2 \n",
186+
"import glob \n",
187+
"import os\n",
188+
"\n",
189+
"inputFolder = r'C:\\Users\\hp\\Desktop\\dl practice'\n",
190+
"outputFolder = r'C:\\Users\\hp\\Desktop\\dl practice\\resized'\n",
191+
"\n",
192+
"# Create the output directory if it doesn't exist\n",
193+
"if not os.path.exists(outputFolder):\n",
194+
" os.mkdir(outputFolder)\n",
195+
"\n",
196+
"for img_path in glob.glob(os.path.join(inputFolder, '*.JPEG')):\n",
197+
" # Read the image\n",
198+
" image = cv2.imread(img_path)\n",
199+
" \n",
200+
" # Apply Gaussian Blur for noise removal\n",
201+
" blurred_image = cv2.GaussianBlur(image, (5, 5), 0) # You can adjust the kernel size (5, 5) if needed\n",
202+
" \n",
203+
" # Resize the image\n",
204+
" imResized = cv2.resize(blurred_image, (224, 224))\n",
205+
" \n",
206+
" # Extract the filename\n",
207+
" filename = os.path.basename(img_path)\n",
208+
" \n",
209+
" # Write the resized and denoised image to the output folder\n",
210+
" cv2.imwrite(os.path.join(outputFolder, filename), imResized)\n",
211+
"\n",
212+
"print(\"Resizing and noise removal complete.\")\n"
213+
]
214+
},
215+
{
216+
"cell_type": "code",
217+
"execution_count": null,
218+
"id": "e15aece0",
219+
"metadata": {},
220+
"outputs": [],
221+
"source": [
222+
"import cv2 \n",
223+
"import glob \n",
224+
"import os\n",
225+
"\n",
226+
"inputFolder = r'C:\\Users\\hp\\Desktop\\dl practice'\n",
227+
"outputFolder = r'C:\\Users\\hp\\Desktop\\dl practice\\resized'\n",
228+
"\n",
229+
"# Create the output directory if it doesn't exist\n",
230+
"if not os.path.exists(outputFolder):\n",
231+
" os.mkdir(outputFolder)\n",
232+
"\n",
233+
"for img_path in glob.glob(os.path.join(inputFolder, '*.JPEG')):\n",
234+
" # Read the image\n",
235+
" image = cv2.imread(img_path)\n",
236+
" \n",
237+
" # Apply Gaussian Blur for noise removal\n",
238+
" blurred_image = cv2.GaussianBlur(image, (5, 5), 0)\n",
239+
" \n",
240+
" # Convert the image to grayscale\n",
241+
" gray_image = cv2.cvtColor(blurred_image, cv2.COLOR_BGR2GRAY)\n",
242+
" \n",
243+
" # Apply Sobel operator for edge detection\n",
244+
" sobelx = cv2.Sobel(gray_image, cv2.CV_64F, 1, 0, ksize=5)\n",
245+
" sobely = cv2.Sobel(gray_image, cv2.CV_64F, 0, 1, ksize=5)\n",
246+
" edge_image = cv2.sqrt(cv2.addWeighted(cv2.pow(sobelx, 2.0), 1.0, cv2.pow(sobely, 2.0), 1.0, 0))\n",
247+
" \n",
248+
" # Resize the image\n",
249+
" resized_edge_image = cv2.resize(edge_image, (224, 224))\n",
250+
" \n",
251+
" # Extract the filename\n",
252+
" filename = os.path.basename(img_path)\n",
253+
" \n",
254+
" # Write the resized and edge-detected image to the output folder\n",
255+
" cv2.imwrite(os.path.join(outputFolder, filename), resized_edge_image)\n",
256+
"\n",
257+
"print(\"Resizing and edge detection complete.\")\n"
258+
]
259+
},
260+
{
261+
"cell_type": "code",
262+
"execution_count": null,
263+
"id": "39807909",
264+
"metadata": {},
265+
"outputs": [],
266+
"source": [
267+
"import cv2 \n",
268+
"import glob \n",
269+
"import os\n",
270+
"\n",
271+
"inputFolder = r'C:\\Users\\hp\\Desktop\\dl practice'\n",
272+
"outputFolder = r'C:\\Users\\hp\\Desktop\\dl practice\\converted'\n",
273+
"\n",
274+
"# Create the output directory if it doesn't exist\n",
275+
"if not os.path.exists(outputFolder):\n",
276+
" os.mkdir(outputFolder)\n",
277+
"\n",
278+
"for img_path in glob.glob(os.path.join(inputFolder, '*.JPEG')):\n",
279+
" # Read the image\n",
280+
" image = cv2.imread(img_path)\n",
281+
" \n",
282+
" # Apply Gaussian Blur for noise removal\n",
283+
" blurred_image = cv2.GaussianBlur(image, (5, 5), 0)\n",
284+
" \n",
285+
" # Convert the image to grayscale\n",
286+
" gray_image = cv2.cvtColor(blurred_image, cv2.COLOR_BGR2GRAY)\n",
287+
" \n",
288+
" # Apply Sobel operator for edge detection\n",
289+
" sobelx = cv2.Sobel(gray_image, cv2.CV_64F, 1, 0, ksize=5)\n",
290+
" sobely = cv2.Sobel(gray_image, cv2.CV_64F, 0, 1, ksize=5)\n",
291+
" edge_image = cv2.sqrt(cv2.addWeighted(cv2.pow(sobelx, 2.0), 1.0, cv2.pow(sobely, 2.0), 1.0, 0))\n",
292+
" \n",
293+
" # Resize the image\n",
294+
" resized_edge_image = cv2.resize(edge_image, (224, 224))\n",
295+
" \n",
296+
" # Convert the image to PNG format\n",
297+
" output_img_path = os.path.join(outputFolder, os.path.splitext(os.path.basename(img_path))[0] + '.png')\n",
298+
" cv2.imwrite(output_img_path, resized_edge_image)\n",
299+
"\n",
300+
"print(\"Conversion complete.\")\n"
301+
]
302+
},
303+
{
304+
"cell_type": "code",
305+
"execution_count": null,
306+
"id": "9413a9a1",
307+
"metadata": {},
308+
"outputs": [],
309+
"source": [
310+
"import cv2 \n",
311+
"import glob \n",
312+
"import os\n",
313+
"\n",
314+
"inputFolder = r'C:\\Users\\hp\\Desktop\\dl practice'\n",
315+
"outputFolder = r'C:\\Users\\hp\\Desktop\\dl practice\\equalized'\n",
316+
"\n",
317+
"# Create the output directory if it doesn't exist\n",
318+
"if not os.path.exists(outputFolder):\n",
319+
" os.mkdir(outputFolder)\n",
320+
"\n",
321+
"for img_path in glob.glob(os.path.join(inputFolder, '*.JPEG')):\n",
322+
" # Read the image\n",
323+
" image = cv2.imread(img_path)\n",
324+
" \n",
325+
" # Convert the image to grayscale\n",
326+
" gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
327+
" \n",
328+
" # Apply histogram equalization\n",
329+
" equalized_image = cv2.equalizeHist(gray_image)\n",
330+
" \n",
331+
" # Apply Gaussian Blur for noise removal\n",
332+
" blurred_image = cv2.GaussianBlur(equalized_image, (5, 5), 0)\n",
333+
" \n",
334+
" # Apply Sobel operator for edge detection\n",
335+
" sobelx = cv2.Sobel(blurred_image, cv2.CV_64F, 1, 0, ksize=5)\n",
336+
" sobely = cv2.Sobel(blurred_image, cv2.CV_64F, 0, 1, ksize=5)\n",
337+
" edge_image = cv2.sqrt(cv2.addWeighted(cv2.pow(sobelx, 2.0), 1.0, cv2.pow(sobely, 2.0), 1.0, 0))\n",
338+
" \n",
339+
" # Resize the image\n",
340+
" resized_edge_image = cv2.resize(edge_image, (224, 224))\n",
341+
" \n",
342+
" # Convert the image to PNG format\n",
343+
" output_img_path = os.path.join(outputFolder, os.path.splitext(os.path.basename(img_path))[0] + '.png')\n",
344+
" cv2.imwrite(output_img_path, resized_edge_image)\n",
345+
"\n",
346+
"print(\"Histogram equalization complete.\")\n"
347+
]
348+
},
349+
{
350+
"cell_type": "code",
351+
"execution_count": null,
352+
"id": "25bf561e",
353+
"metadata": {},
354+
"outputs": [],
355+
"source": []
356+
}
357+
],
358+
"metadata": {
359+
"kernelspec": {
360+
"display_name": "Python 3 (ipykernel)",
361+
"language": "python",
362+
"name": "python3"
363+
},
364+
"language_info": {
365+
"codemirror_mode": {
366+
"name": "ipython",
367+
"version": 3
368+
},
369+
"file_extension": ".py",
370+
"mimetype": "text/x-python",
371+
"name": "python",
372+
"nbconvert_exporter": "python",
373+
"pygments_lexer": "ipython3",
374+
"version": "3.11.3"
375+
}
376+
},
377+
"nbformat": 4,
378+
"nbformat_minor": 5
379+
}

0 commit comments

Comments
 (0)