Graph Based Segmentation with Python + OpenCV

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) lena_seg.png

Recommended Posts

Graph Based Segmentation with Python + OpenCV
Binarization with OpenCV / Python
"Apple processing" with OpenCV3 + Python3
Image editing with python OpenCV
Camera capture with Python + OpenCV
[Python] Using OpenCV with Python (Basic)
Using OpenCV with Python @Mac
Shining life with Python and OpenCV
[Python] Using OpenCV with Python (Image Filtering)
Neural network with OpenCV 3 and Python 3
[Python] Using OpenCV with Python (Image transformation)
[Python] Using OpenCV with Python (Edge Detection)
Easy Python + OpenCV programming with Canopy
Try face recognition with python + OpenCV
Cut out face with Python + OpenCV
Face recognition with camera with opencv3 + python2.7
Load gif images with Python + OpenCV
Let's make a graph with python! !!
Find image similarity with Python + OpenCV
Use OpenCV with Python 3 in Window
Draw an illustration with Python + OpenCV
Track baseball balls with Python + OpenCV
Draw arrows (vectors) with opencv / python
Basic study of OpenCV with Python
[Python] Set the graph range with matplotlib
Save video frame by frame with Python OpenCV
Capturing images with Pupil, python and OpenCV
I tried non-photorealistic rendering with Python + opencv
[python, openCV] base64 Face recognition with images
Create miscellaneous Photoshop videos with Python + OpenCV ③ Create miscellaneous Photoshop videos
[Python] Read images with OpenCV (for beginners)
Until you can use opencv with python
Light image processing with Python x OpenCV
Smoothing edge-saved with python + OpenCV (BilateralFilter, NLMeansFilter)
FizzBuzz with Python3
Scraping with Python
Statistics with python
Python2.7 + CentOS7 + OpenCV3
Scraping with Python
Python with Go
Twilio with Python
Play with 2016-Python
Tested with Python
with syntax (Python)
OpenCV Samples (Python)
Bingo with python
[Note] openCV + python
Excel with Python
Microcomputer with Python
Cast with python
Hello World and face detection with OpenCV 4.3 + Python
I tried "smoothing" the image with Python + OpenCV
Performance comparison of face detector with Python + OpenCV
[Python] limit axis of 3D graph with Matplotlib
I tried "differentiating" the image with Python + OpenCV
Read Python csv data with Pandas ⇒ Graph with Matplotlib
Edge extraction with python + OpenCV (Sobel filter, Laplacian filter)
[Python] Draw a directed graph with Dash Cytoscape
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Make OpenCV3 available from python3 installed with pyenv
I tried "binarizing" the image with Python + OpenCV