cari
Rumahpembangunan bahagian belakangTutorial PythonPengenalan Kepada Penglihatan Komputer dengan Python (Bahagian 1)

Note: In this post, we will only be working with Grey-Scale images to make it easy to follow.

What is an Image?

An image can be thought of as a matrix of values, where each value represents the intensity of a pixel. There are three main types of image formats:

  • Binary: An image in this format is represented by a single 2-D matrix with values of 0 (black) and 1 (white). It's the simplest form of image representation.

Introduction To Computer Vision with Python (Part 1)

  • Grey-Scale: In this format, an image is represented by a single 2-D matrix with values ranging from 0 to 255; where 0 represents black and 255 represents white. The intermediate values represent varying shades of grey.

Introduction To Computer Vision with Python (Part 1)

  • RGB Scale: Here, an image is represented by three 2-D matrices (one for each color channel: Red, Green, and Blue), with values ranging from 0 to 255. Each matrix contains pixel values for one color component, and combining these three channels gives us the full color image.

Introduction To Computer Vision with Python (Part 1)

Filters

Filters are tools used to modify images by applying certain operations. A filter is a matrix (also called a kernel) that moves across the image, performing computations on the pixel values within its window. We’ll cover two common types of filters: Mean Filters and Median Filters.

Mean Filters

A Mean Filter is used to reduce noise by averaging the pixel values within a window. It replaces the center pixel in the window with the average of all the pixel values within that window. The cv2.blur() function applies a mean filter with a kernel size of 3x3, which means it considers a 3x3 window of pixels around each pixel to compute the average. This helps in smoothing the image.

import cv2
import matplotlib.pyplot as plt

image = cv2.imread('McLaren-720S-Price-1200x675.jpg', cv2.IMREAD_GRAYSCALE)

# Applies a Mean Filter of size 3 x 3
blurred_image = cv2.blur(image, (3, 3))

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis("off")

plt.subplot(1, 2, 2)
plt.imshow(blurred_image, cmap='gray')
plt.title('Mean Filtered Image')
plt.axis("off")
plt.show()

Introduction To Computer Vision with Python (Part 1)

Median Filters

A Median Filter is used to reduce noise by replacing each pixel's value with the median value of all pixels in a window. It’s particularly effective in removing salt-and-pepper noise. The cv2.medianBlur() function applies a median filter with a kernel size of 3. This method replaces each pixel with the median value of the pixel values in its neighborhood, which helps in preserving edges while removing noise. Here the larger the kernel size the more blurred the image gets.

import cv2
import matplotlib.pyplot as plt

image = cv2.imread('McLaren-720S-Price-1200x675.jpg', cv2.IMREAD_GRAYSCALE)

# Applies a Median Filter with a kernel size of 3
blurred_image = cv2.medianBlur(image, 3)

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis("off")

plt.subplot(1, 2, 2)
plt.imshow(blurred_image, cmap='gray')
plt.title('Median Filtered Image')
plt.axis("off")
plt.show()

Introduction To Computer Vision with Python (Part 1)

Custom Filters

You can create custom filters to apply specific operations on your images. The cv2.filter2D() function allows you to apply any custom kernel to an image. The cv2.filter2D() function applies a custom kernel (filter) to the image. The kernel is a matrix that defines the operation to be performed on the pixel values. In this example, the kernel enhances certain features of the image based on the specified values.

import cv2
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread('McLaren-720S-Price-1200x675.jpg', cv2.IMREAD_GRAYSCALE)

# Define a custom filter kernel
kernel = np.array([[2, -1, 5],
                   [-5, 5, -1],
                   [0, -1, 0]])

filtered_image = cv2.filter2D(image, -1, kernel)

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(filtered_image, cmap='gray')
plt.title('Filtered Image')
plt.axis('off')

plt.show()

Introduction To Computer Vision with Python (Part 1)

Thresholding

Note: In the code snippets, you will see _ , image when assigning the thresholded image. This is because the cv2.threshold function returns two values: the threshold value used and the thresholded image. Since we only need the thresholded image, we use _ to ignore the threshold value.

Thresholding converts an image into a binary image by setting pixel values based on a condition. There are several types of thresholding techniques:

Global Thresholding

Simple Thresholding

This method sets a fixed threshold value for the entire image. Pixels with values above the threshold are set to the maximum value (255), and those below are set to 0. The cv2.threshold() function is used for simple thresholding. Pixels with intensity greater than 127 are set to white (255), and those with intensity less than or equal to 127 are set to black (0), producing a binary image.

import cv2
import matplotlib.pyplot as plt

image = cv2.imread('McLaren-720S-Price-1200x675.jpg', cv2.IMREAD_GRAYSCALE)

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

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis("off")

plt.subplot(1, 2, 2)
plt.imshow(thresholded_image, cmap='gray')
plt.title('Thresholded Image')
plt.axis("off")
plt.show()

Introduction To Computer Vision with Python (Part 1)

Otsu Thresholding

Otsu's method determines the optimal threshold value automatically based on the histogram of the image. This method minimizes intra-class variance and maximizes inter-class variance. By setting the threshold value to 0 and using cv2.THRESH_OTSU, the function automatically calculates the best threshold value to separate the foreground from the background.

import cv2
import matplotlib.pyplot as plt

image = cv2.imread('McLaren-720S-Price-1200x675.jpg', cv2.IMREAD_GRAYSCALE)

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

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis("off")

plt.subplot(1, 2, 2)
plt.imshow(otsu_thresholded_image, cmap='gray')
plt.title("Otsu's Thresholded Image")
plt.axis("off")
plt.show()

Introduction To Computer Vision with Python (Part 1)

Adaptive Thresholding

Mean Adaptive Thresholding

In Mean Adaptive Thresholding, the threshold value for each pixel is calculated based on the average of pixel values in a local neighborhood around that pixel. This method adjusts the threshold dynamically across different regions of the image. The cv2.adaptiveThreshold() function calculates the threshold for each pixel based on the mean value of the pixel values in a local 11x11 neighborhood. A constant value of 2 is subtracted from this mean to fine-tune the threshold. This method is effective for images with varying lighting conditions.

import cv2
import matplotlib.pyplot as plt

image = cv2.imread('McLaren-720S-Price-1200x675.jpg', cv2.IMREAD_GRAYSCALE)

mean_adaptive_thresholded_image = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis("off")

plt.subplot(1, 2, 2)
plt.imshow(mean_adaptive_thresholded_image, cmap='gray')
plt.title('Mean Adaptive Thresholded Image')
plt.axis("off")
plt.show()

Introduction To Computer Vision with Python (Part 1)

Gaussian Adaptive Thresholding

Gaussian Adaptive Thresholding computes the threshold value for each pixel based on a Gaussian-weighted sum of the pixel values in a local neighborhood. This method often provides better results in cases with non-uniform illumination. In Gaussian Adaptive Thresholding, the threshold is determined by a Gaussian-weighted sum of pixel values in an 11x11 neighborhood. The constant value 2 is subtracted from this weighted mean to adjust the threshold. This method is useful for handling images with varying lighting and shadows.

import cv2
import matplotlib.pyplot as plt

image = cv2.imread('McLaren-720S-Price-1200x675.jpg', cv2.IMREAD_GRAYSCALE)

gaussian_adaptive_thresholded_image = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis("off")

plt.subplot(1, 2, 2)
plt.imshow(gaussian_adaptive_thresholded_image, cmap='gray')
plt.title('Gaussian Adaptive Thresholded Image')
plt.axis("off")
plt.show()

Introduction To Computer Vision with Python (Part 1)

References

  • Encord.com
  • Pyimagesearch.com
  • OpenCV Thresholding
  • OpenCV Filtering

Atas ialah kandungan terperinci Pengenalan Kepada Penglihatan Komputer dengan Python (Bahagian 1). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Apakah beberapa operasi biasa yang boleh dilakukan pada tatasusunan python?Apakah beberapa operasi biasa yang boleh dilakukan pada tatasusunan python?Apr 26, 2025 am 12:22 AM

PythonArraysSupportVariousoperations: 1) SlicingExtractsSubsets, 2) Menambah/ExtendingAddSelements, 3) InsertingPlaceSelementSatSatSatSpecifics, 4) RemovingDeleteselements, 5) Sorting/ReversingChangesOrder,

Dalam jenis aplikasi yang biasa digunakan oleh numpy?Dalam jenis aplikasi yang biasa digunakan oleh numpy?Apr 26, 2025 am 12:13 AM

NumpyarraysareessentialforapplicationRequiringeficientnumericalcomputationsanddatamanipulation.theyarecrucialindaSascience, machinelearning, fizik, kejuruteraan, danfinanceduetotheirabilitytOHandlelarge-Scaledataefisien.Forexample, infinancialanal

Bilakah anda memilih untuk menggunakan array di atas senarai di Python?Bilakah anda memilih untuk menggunakan array di atas senarai di Python?Apr 26, 2025 am 12:12 AM

UseanArray.arrayoveralistinpythonwhendealingwithhomogeneousdata, criticalcode prestasi, orinterfacingwithccode.1) homogeneousdata: arrayssavemememorywithtypedelements.2)

Adakah semua operasi senarai disokong oleh tatasusunan, dan sebaliknya? Mengapa atau mengapa tidak?Adakah semua operasi senarai disokong oleh tatasusunan, dan sebaliknya? Mengapa atau mengapa tidak?Apr 26, 2025 am 12:05 AM

Tidak, notalllistoperationsaresuportedByArrays, andviceversa.1) arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing, whyimpactsperformance.2) listsdonotguaranteeconstantTimeComplexityFordirectacesscesscesscesscesscesscesscesscesscesessd.

Bagaimana anda mengakses elemen dalam senarai python?Bagaimana anda mengakses elemen dalam senarai python?Apr 26, 2025 am 12:03 AM

ToaccesselementsinaPythonlist,useindexing,negativeindexing,slicing,oriteration.1)Indexingstartsat0.2)Negativeindexingaccessesfromtheend.3)Slicingextractsportions.4)Iterationusesforloopsorenumerate.AlwayschecklistlengthtoavoidIndexError.

Bagaimana tatasusunan digunakan dalam pengkomputeran saintifik dengan python?Bagaimana tatasusunan digunakan dalam pengkomputeran saintifik dengan python?Apr 25, 2025 am 12:28 AM

Arraysinpython, terutamanya yang, arecrucialinscientificificputingputingfortheirefficiencyandversatility.1) mereka yang digunakan untuk

Bagaimana anda mengendalikan versi python yang berbeza pada sistem yang sama?Bagaimana anda mengendalikan versi python yang berbeza pada sistem yang sama?Apr 25, 2025 am 12:24 AM

Anda boleh menguruskan versi python yang berbeza dengan menggunakan Pyenv, Venv dan Anaconda. 1) Gunakan pyenv untuk menguruskan pelbagai versi python: Pasang pyenv, tetapkan versi global dan tempatan. 2) Gunakan VENV untuk mewujudkan persekitaran maya untuk mengasingkan kebergantungan projek. 3) Gunakan Anaconda untuk menguruskan versi python dalam projek sains data anda. 4) Simpan sistem python untuk tugas peringkat sistem. Melalui alat dan strategi ini, anda dapat menguruskan versi Python yang berbeza untuk memastikan projek yang lancar.

Apakah beberapa kelebihan menggunakan array numpy melalui array python standard?Apakah beberapa kelebihan menggunakan array numpy melalui array python standard?Apr 25, 2025 am 12:21 AM

Numpyarrayshaveseveraladvantagesoverstanderardpythonarrays: 1) thearemuchfasterduetoc-assedimplementation, 2) thearemorememory-efficient, antyedlargedataSets, and3) theyofferoptimized, vectorizedfuncionsformathhematicalicalicalicialisation

See all articles

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

Video Face Swap

Video Face Swap

Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Alat panas

SublimeText3 versi Inggeris

SublimeText3 versi Inggeris

Disyorkan: Versi Win, menyokong gesaan kod!

VSCode Windows 64-bit Muat Turun

VSCode Windows 64-bit Muat Turun

Editor IDE percuma dan berkuasa yang dilancarkan oleh Microsoft

PhpStorm versi Mac

PhpStorm versi Mac

Alat pembangunan bersepadu PHP profesional terkini (2018.2.1).

Versi Mac WebStorm

Versi Mac WebStorm

Alat pembangunan JavaScript yang berguna

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual