I heard that Facebook has open sourced the segmentation framework, and I just needed segmentation, so I looked it up.
Segmenting and refining images with SharpMask
I wanted to get a candidate area without a teacher this time because it was unavoidable to be Lua or Torch, but this framework seemed to be supervised so I gave up using it and searched for something else .. Then, OpenCV had such a function, so I tried it.
graph_segmentation.py
import cv2
import numpy as np
segmentator = cv2.ximgproc.segmentation.createGraphSegmentation(sigma=0.5, k=300, min_size=1000)
src = cv2.imread('image.jpg')
segment = segmentator.processImage(src)
mask = segment.reshape(list(segment.shape) + [1]).repeat(3, axis=2)
masked = np.ma.masked_array(src, fill_value=0)
for i in range(np.max(segment)):
masked.mask = mask != i
y, x = np.where(segment == i)
top, bottom, left, right = min(y), max(y), min(x), max(x)
dst = masked.filled()[top : bottom + 1, left : right + 1]
cv2.imwrite('segment_{num}.jpg'.format(num=i), dst)
Parameters | |
---|---|
sigma | Boundary smoothness (small value for complex boundaries, large value for smooth boundaries) |
k | Maybe how much to integrate the candidate areas (smaller values divide into many smaller areas, larger values divide into fewer large areas?) |
min_size | Minimum area size (probably the number of pixels in the area) |
It was difficult to simply cut out only the candidate area. There seems to be a way to use OpenCV's boundingRect other than doing it with Numpy.
Postscript: The value can be changed with segmentator.setSigma (value), segmentator.setK (value), segmentator.setMinSize (value).
Results of application to Lena's whole body image that cannot be placed as it is (sigma = 0.7, k = 1200, min_size = 5000)
Recommended Posts