Python을 사용한 컴퓨터 비전 소개(1부)

PHPz
풀어 주다: 2024-09-10 06:44:02
원래의
709명이 탐색했습니다.

참고: 이 게시물에서는 쉽게 따라할 수 있도록 회색조 이미지만 사용합니다.

이미지란 무엇입니까?

이미지는 값의 행렬로 생각할 수 있으며, 각 값은 픽셀의 강도를 나타냅니다. 이미지 형식에는 세 가지 주요 유형이 있습니다.

  • 바이너리: 이 형식의 이미지는 0(검은색)과 1(흰색) 값을 갖는 단일 2D 행렬로 표현됩니다. 가장 간단한 형태의 이미지 표현입니다.

Introduction To Computer Vision with Python (Part 1)

  • 회색 스케일: 이 형식에서 이미지는 0에서 255 사이의 값을 갖는 단일 2D 행렬로 표현됩니다. 여기서 0은 검정색을 나타내고 255는 흰색을 나타냅니다. 중간 값은 다양한 회색 음영을 나타냅니다.

Introduction To Computer Vision with Python (Part 1)

  • RGB 배율: 여기서 이미지는 3개의 2D 행렬(빨간색, 녹색, 파란색 각 색상 채널에 하나씩)로 표현되며 값 범위는 0~255입니다. 각 행렬에는 다음의 픽셀 값이 포함됩니다. 하나의 색상 구성 요소를 사용하고 이 세 가지 채널을 결합하면 풀 컬러 이미지가 생성됩니다.

Introduction To Computer Vision with Python (Part 1)

필터

필터는 특정 작업을 적용하여 이미지를 수정하는 데 사용되는 도구입니다. 필터는 이미지를 가로질러 이동하면서 해당 창 내의 픽셀 값에 대해 계산을 수행하는 행렬(커널이라고도 함)입니다. 우리는 두 가지 일반적인 필터 유형, 즉 평균 필터와 중앙값 필터를 다룰 것입니다.

평균 필터

평균 필터는 창 내의 픽셀 값을 평균화하여 노이즈를 줄이는 데 사용됩니다. 창의 중앙 픽셀을 해당 창 내의 모든 픽셀 값의 평균으로 바꿉니다. cv2.blur() 함수는 커널 크기가 3x3인 평균 필터를 적용합니다. 이는 평균을 계산하기 위해 각 픽셀 주위의 픽셀의 3x3 창을 고려한다는 의미입니다. 이미지를 부드럽게 만드는 데 도움이 됩니다.

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)

중앙값 필터

중앙값 필터는 각 픽셀의 값을 창에 있는 모든 픽셀의 중앙값으로 대체하여 노이즈를 줄이는 데 사용됩니다. 이는 소금과 후추 소음을 제거하는 데 특히 효과적입니다. cv2.medianBlur() 함수는 커널 크기가 3인 중앙값 필터를 적용합니다. 이 메서드는 각 픽셀을 인접 픽셀 값의 중앙값으로 대체하므로 노이즈를 제거하는 동시에 가장자리를 유지하는 데 도움이 됩니다. 여기서 커널 크기가 클수록 이미지가 더 흐려집니다.

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)

맞춤 필터

맞춤 필터를 만들어 이미지에 특정 작업을 적용할 수 있습니다. cv2.filter2D() 함수를 사용하면 사용자 정의 커널을 이미지에 적용할 수 있습니다. cv2.filter2D() 함수는 이미지에 사용자 정의 커널(필터)을 적용합니다. 커널은 픽셀 값에 대해 수행할 작업을 정의하는 행렬입니다. 이 예에서 커널은 지정된 값을 기반으로 이미지의 특정 기능을 향상시킵니다.

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)

임계값

참고: 코드 조각에서 임계값 이미지를 할당할 때 _ , 이미지가 표시됩니다. 이는 cv2.threshold 함수가 사용된 임계값과 임계값 이미지라는 두 가지 값을 반환하기 때문입니다. 임계값 이미지만 필요하므로 _를 사용하여 임계값을 무시합니다.

임계값은 조건에 따라 픽셀 값을 설정하여 이미지를 이진 이미지로 변환합니다. 임계값 기술에는 여러 유형이 있습니다.

전역 임계값

단순 임계값

이 방법은 전체 이미지에 대해 고정된 임계값을 설정합니다. 임계값보다 높은 값을 갖는 픽셀은 최대값(255)으로 설정되고, 그 이하는 0으로 설정됩니다. 간단한 임계값 지정에는 cv2.threshold() 함수가 사용됩니다. 명암도가 127보다 큰 픽셀은 흰색(255)으로 설정되고, 명도가 127 이하인 픽셀은 검정색(0)으로 설정되어 이진 이미지를 생성합니다.

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

위 내용은 Python을 사용한 컴퓨터 비전 소개(1부)의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:dev.to
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!