碼清單8-25 myPyrMeanShiftFiltering.cpp利用均值漂移法分割圖像#include <opencv2/opencv.hpp>#include <iostream>
using namespace cv;using namespace std;
int main(){ Mat img = imread("coins.png"); if (!img.data) { cout << "讀取圖像錯誤,請確認圖像文件是否正確" << endl; return -1; }
Mat result1, result2; TermCriteria T10 = TermCriteria(TermCriteria::COUNT | TermCriteria::EPS, 10, 0.1); pyrMeanShiftFiltering(img, result1, 20, 40, 2, T10); pyrMeanShiftFiltering(result1, result2, 20, 40, 2, T10);
imshow("img", img); imshow("result1", result1); imshow("result2", result2);
Mat imgCanny, result1Canny, result2Canny; Canny(img, imgCanny, 150, 300); Canny(result1, result1Canny, 150, 300); Canny(result2, result2Canny, 150, 300);
imshow("imgCanny", imgCanny); imshow("result1Canny", result1Canny); imshow("result2Canny", result2Canny); waitKey(0); return 0;}