fanfuhan OpenCV 教學117 ~ opencv-117-圖像均值漂移分割
fanfuhan OpenCV 教學117 ~ opencv-117-圖像均值漂移分割
	
資料來源: https://fanfuhan.github.io/
https://fanfuhan.github.io/2019/05/25/opencv-117/
GITHUB:https://github.com/jash-git/fanfuhan_ML_OpenCV
	
圖像均值漂移分割是一種無監督的圖像分割方法,前面我們在跟踪相關的內容介紹過均值遷移算法,知道均值遷移可以找到圖像中特徵直方圖空間的峰值分佈,這裡我們還是使用均值遷移,讓它去不斷分割找到空間顏色分佈的峰值,然後根據峰值進行相似度合併,解決過度分割問題,得到最終的分割圖像,對於圖像多維度數據顏色值(RGB)與空間位置(x,y),所以需要兩個窗口半徑,一個是空間半徑、另外一個是顏色半徑,經過均值漂移窗口的所有的像素點會具有相同的像素值
	
C++
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
	Mat src = imread("D:/images/yuan_test.png");
	imshow("input", src);
	Mat dst;
	TermCriteria tc = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 10, 0.1);
	pyrMeanShiftFiltering(src, dst, 20, 40, 2, tc);
	imshow("mean shift segementation demo", dst);
	waitKey(0);
	return 0;
}
Python
"""
图像均值漂移分割
"""
import cv2 as cv
src = cv.imread("images/yuan_test.png")
cv.imshow("input", src)
dst = cv.pyrMeanShiftFiltering(src, 25, 40, None, 2)
cv.imshow("result", dst)
cv.waitKey(0)
cv.destroyAllWindows()