[파이썬] opencv-python 이미지 분할

Image segmentation is often an essential task in computer vision and image processing. It involves dividing an image into different meaningful regions or objects for further analysis or manipulation. OpenCV-Python provides a convenient and powerful library for performing image segmentation tasks. In this blog post, we will explore some common methods and techniques for image segmentation using OpenCV-Python.

Installing OpenCV-Python

Before we begin, make sure you have OpenCV-Python installed on your system. You can install it via pip using the following command:

pip install opencv-python

Loading and Displaying an Image

Let’s start by loading and displaying an image. We will be using the cv2 module from OpenCV-Python for this task.

import cv2

# Load an image
image = cv2.imread('path/to/image.jpg')

# Display the image
cv2.imshow('Original Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Make sure to replace 'path/to/image.jpg' with the actual path to your image file. This code will load the image and display it in a new window.

Simple Thresholding

One of the simplest methods for image segmentation is thresholding. It separates an image into two regions based on a threshold value. Pixels below the threshold are assigned to one region, and pixels above the threshold are assigned to another region.

import cv2

# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply simple thresholding
_, thresholded = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

# Display the thresholded image
cv2.imshow('Thresholded Image', thresholded)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the code above, we convert the image to grayscale using cv2.cvtColor() and then apply simple thresholding using cv2.threshold(). The threshold value is set to 127 in this example. Pixels with intensity below 127 are set to 0 (black), and pixels with intensity above 127 are set to 255 (white).

Adaptive Thresholding

Adaptive thresholding is a variation of simple thresholding where the threshold value is determined for each pixel based on its local neighborhood. This allows for better handling of images with varying lighting conditions.

import cv2

# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply adaptive thresholding
thresholded = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)

# Display the thresholded image
cv2.imshow('Thresholded Image', thresholded)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the code above, we use cv2.adaptiveThreshold() to perform adaptive thresholding. We specify the maximum value (255), the adaptive method (cv2.ADAPTIVE_THRESH_MEAN_C), the threshold type (cv2.THRESH_BINARY), the block size (11), and the constant subtracted from the mean (2).

Edge Detection

Another common approach to image segmentation is edge detection. It aims to identify the boundaries between different regions or objects in an image.

import cv2

# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply Canny edge detection
edges = cv2.Canny(gray, 100, 200) 

# Display the edges
cv2.imshow('Edges', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the code above, we use cv2.Canny() to perform Canny edge detection. We specify the lower threshold (100) and the upper threshold (200) for edge detection. Adjust these values to fine-tune the edge detection results.

Conclusion

Image segmentation is a fundamental task in computer vision applications. OpenCV-Python provides a variety of methods and techniques to perform image segmentation effectively. In this blog post, we have covered simple thresholding, adaptive thresholding, and edge detection methods. Experiment with different parameters and techniques to achieve optimal segmentation results for your specific applications.

Remember to import the necessary modules (e.g., cv2) and replace 'path/to/image.jpg' with your own image path when running the code examples. Have fun exploring and experimenting with image segmentation using OpenCV-Python!