Python | Algorithms | Data Structures | Cyber ​​Security | Networks
38.6K subscribers
779 photos
23 videos
21 files
714 links
This channel is for Programmers, Coders, Software Engineers.

1) Python
2) django
3) python frameworks
4) Data Structures
5) Algorithms
6) DSA

Admin: @Hussein_Sheikho

Ad & Earn money form your channel:
https://telega.io/?r=nikapsOH
Download Telegram
Topic: Python OpenCV – Part 2: Image Thresholding, Blurring, and Edge Detection

---

1. Image Thresholding

• Converts grayscale images to binary (black & white) by setting a pixel value threshold.

import cv2

image = cv2.imread('image.jpg', 0) # load as grayscale

_, binary = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)

cv2.imshow("Binary Image", binary)
cv2.waitKey(0)
cv2.destroyAllWindows()


• Common thresholding types:

* THRESH_BINARY
* THRESH_BINARY_INV
* THRESH_TRUNC
* THRESH_TOZERO
* THRESH_OTSU (automatic threshold)

_, otsu = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)


---

2. Image Blurring (Smoothing)

• Helps reduce image noise and detail.

# Gaussian Blur
blurred = cv2.GaussianBlur(image, (5, 5), 0)

# Median Blur
median = cv2.medianBlur(image, 5)

# Bilateral Filter (preserves edges)
bilateral = cv2.bilateralFilter(image, 9, 75, 75)


---

3. Edge Detection with Canny

• Canny edge detection is one of the most popular techniques for detecting edges in an image.

edges = cv2.Canny(image, 100, 200)

cv2.imshow("Edges", edges)
cv2.waitKey(0)
cv2.destroyAllWindows()


* The two thresholds (100 and 200) are used for hysteresis (strong vs. weak edges).

---

4. Combining Blur + Edge Detection

blurred = cv2.GaussianBlur(image, (5, 5), 0)
edges = cv2.Canny(blurred, 50, 150)


• Blurring before edge detection reduces noise and improves accuracy.

---

Summary

Thresholding simplifies images to black & white based on intensity.

Blurring smooths out images and reduces noise.

Canny Edge Detection is a reliable method for identifying edges.

• These tools are fundamental for object detection, image segmentation, and OCR tasks.

---

Exercise

• Write a program that:

1. Loads an image in grayscale.
2. Applies Gaussian blur.
3. Uses Canny to detect edges.
4. Saves the final edge-detected image to disk.

---

#Python #OpenCV #ImageProcessing #EdgeDetection #ComputerVision

https://t.iss.one/DataScience4
2