๐ ๋ณธ ์์ ๋ Window10์ VSCode, Python3.11.0๋ก ์์ฑ๋์์ต๋๋ค.
Segmentation์ ์์์์ ํน์ ์์ญ๋ง ์ถ์ถํ๋ ๊ธฐ๋ฅ์ด๋ค.
์๋ฅผ ๋ค์ด ์๋ฃ ์์์์ ์ข ์์ ์๋ณํ๊ฑฐ๋, ์์จ์ฃผํ์ฐจ๊ฐ ๋๋ก์ ๋ณดํ์๋ฅผ ๊ตฌ๋ถํ๋ ๋ฐ ์ฌ์ฉ๋๋ค.
OpenCV์์๋ K-Means ๋๋ Watershed ์๊ณ ๋ฆฌ์ฆ์ ํตํด Segmentation์ ํ ์ ์๋ค.
K-Means
K-Means ํด๋ฌ์คํฐ๋ง์ ๋จธ์ ๋ฌ๋ ์ค ํ๋๋ก ๋น์ง๋ ํ์ต ์๊ณ ๋ฆฌ์ฆ์ด๋ค.
๋ฐ์ดํฐ๋ฅผ K๊ฐ์ ํด๋ฌ์คํฐ๋ก ๋๋๋ ๊ธฐ๋ฒ์ผ๋ก ์ด๋ฅผ ํตํด ์ด๋ฏธ์ง๋ฅผ ์์ ๊ธฐ๋ฐ์ผ๋ก ๋ถํ ํ ์ ์๋ค.
import cv2
import numpy as np
import random
if __name__ == "__main__":
img = cv2.imread("cat.png")
# Resize
h,w,c = img.shape
img = cv2.resize(img,(int(w//2),int(h//2)))
cv2.imshow("original",img)
# BGR to RGB
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
# ์ด๋ฏธ์ง๋ฅผ 2D ๋ฐฐ์ด๋ก ๋ณํ
pixel_values = img.reshape((-1,3))
pixel_values = np.float32(pixel_values)
# k-means
k = 2
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100,0.2)
# Radom์ผ๋ก ์ค์ ์ค์
_,labels,centers = cv2.kmeans(pixel_values,k,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
# ๊ฐ ํด๋ฌ์คํฐ์ ๋ํด ๋๋ค ์์ ์์ฑ
colors = []
for _ in range(k):
colors.append([random.randint(0, 255) for _ in range(3)])
# ์ธ๊ทธ๋ฉํ
์ด์
๋ ์ด๋ฏธ์ง๋ฅผ ์์์ผ๋ก ์ฑ์ฐ๊ธฐ
segmented_img = np.zeros_like(img)
for i in range(len(labels)):
segmented_img[i // img.shape[1], i % img.shape[1]] = colors[labels[i][0]]
cv2.imshow("segmentation",segmented_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Watershed
Watershed๋ ์ด๋ฏธ์ง ๋ด ๊ฒฝ๊ณ์ ์ ์ฐพ๊ณ ์ด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๊ฐ์ฒด๋ฅผ ๋ถํ ํ๋ ๋ฐฉ๋ฒ์ด๋ค.
์ด ๋ฐฉ๋ฒ์ ๋ณต์กํ ๋ฐฐ๊ฒฝ ์์์ ๊ฐ์ฒด๋ฅผ ์๋ณํ๋ ๋ฐ ์ ์ฉํ๋ค.
import cv2
import numpy as np
import random
if __name__ == "__main__":
image = cv2.imread("coin.jpg")
# Resize
cv2.imshow("original",image)
# ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
# noise removal
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)
# sure background area
sure_bg = cv2.dilate(opening,kernel,iterations=3)
# Finding sure foreground area
dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0)
# Finding unknown region
sure_fg = np.uint8(sure_fg)
unknown = cv2.subtract(sure_bg,sure_fg)
cv2.imshow("Dist",dist_transform)
cv2.imshow("watershed",sure_fg)
# Marker labelling
ret, markers = cv2.connectedComponents(sure_fg)
# Add one to all labels so that sure background is not 0, but 1
markers = markers+1
# Now, mark the region of unknown with zero
markers[unknown==255] = 0
markers = cv2.watershed(image,markers)
image[markers == -1] = [255,0,0]
cv2.imshow("segmentation",image)
cv2.waitKey(0)
cv2.destroyAllWindows()
์ด๋ฌํ ๋ฐฉ๋ฒ ๋ง๊ณ ๋ ๋ฅ๋ฌ๋์ ์ฌ์ฉํ Segmentation(UNet)๋ ์๋ค.
์ด ๋ฐฉ๋ฒ์ด ํ์ต๋ง ์ถฉ๋ถํ ๋๋ค๋ฉด ๋ ์ฌ์ธํ๋ค.
'AI > Computer Vision' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Computer Vision] ์ด๋ฏธ์ง ์ ์ฌ๋ (0) | 2024.09.21 |
---|---|
[Computer Vision] Skeletonization (0) | 2024.09.20 |
[Computer Vision] Depth (0) | 2024.09.15 |
[Computer Vision] Trapped-ball Segmentation (0) | 2024.09.14 |
[Computer Vision] IoU(Intersection over Union) (0) | 2024.09.12 |