图像分割理论简介
图像分割是机器学习的重要组成部分,无论是前期feed给深度学习模型,还是作为后期在行人检测、医学领域的图像精确分割等都具有重要意义。图像分割的理论前提在于图像所构成矩阵在图像内不同物体间边缘的梯度骤变,如同物体的像素如同水流,再与不同物体像素汇合时颜色的差别就会形成边界。基于OpenCV的watershed、p-algrorithm可以很好的将边界提取,当然,在特定领域,需要基于CNN对不同图片做大量学习也可以识别物体或人物。
1.approximate segmentation
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('coins.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
2.前景、背景分离(为进一步分离不同硬币之间的细小边界)
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)
3.前景、背景标签标记,将背景标为0
# 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
*说明不同颜色表示硬币的中心
4.应用whtershed算法,将标记图像改良,并将边界区域标位-1
markers = cv2.watershed(img,markers)
img[markers == -1] = [255,0,0]