zoukankan      html  css  js  c++  java
  • FCM 和Kmeans图像分割对比

    Kmeans

    # coding: utf-8
    import cv2
    import numpy as np
    
    img = cv2.imread("/home/hichens/Datasets/pic/11.jpg")
    if len(img.shape) == 3:
       data = img.reshape(-1, 3)
    else:
       data = img.reshape(-1, 1)
    data = np.float32(data)
    
    #定义中心 (type,max_iter,epsilon)
    criteria = (cv2.TERM_CRITERIA_EPS +
                cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    
    #设置标签
    flags = cv2.KMEANS_RANDOM_CENTERS
    
    #K-Means聚类 聚集成4类
    Ncenter = 2
    compactness, labels, centers = cv2.kmeans(data, Ncenter, None, criteria, 10, flags)
    
    #生成最终图像
    res = centers[labels.flatten()]
    dst = res.reshape(img.shape)
    
    print(dst.shape)
    dst = np.array(dst, dtype=np.uint8)
    dst = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)
    while True:
        cv2.imshow("img", img)
        cv2.imshow("dst", dst)
        if cv2.waitKey(1) == 27:
            break
    
    cv2.destroyAllWindows()
    

    FCM

    
    import cv2
    import skfuzzy as fuzz
    import numpy as np
    
    img = cv2.imread('/home/hichens/Datasets/pic/11.jpg')
    if len(img.shape) == 3:
       data = img.reshape(-1, 3)
    else:
       data = img.reshape(-1, 1)
    data = np.float32(data)
    
    Ncenter = 4
    cntr, u, _, _, _, _, fpc = fuzz.cluster.cmeans(
             data.T, Ncenter, 2, error=0.05, maxiter=100, init=None)
    labels = np.argmax(u, axis=0)
    res = cntr[labels.flatten()]
    dst = res.reshape(img.shape)
    print(dst.shape)
    print(fpc)
    dst = np.array(dst, dtype=np.uint8)
    
    dst = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)
    while True:
        cv2.imshow("img", img)
        cv2.imshow("dst", dst)
        if cv2.waitKey(1) == 27:
            break
    cv2.destroyAllWindows()
    

  • 相关阅读:
    GridView鼠标悬浮
    GridView控件属性及应用(转载)
    GridView动态绑定按钮
    GridView隐藏列, 并能读取列值的解决方法(转载)
    Oracle语句需要注意的地方
    Oracle数据库创建一个主键ID自增的表
    微软宣布.NET开源:关键软件技术兼容各大平台
    全球排名前50网站都用什么语言开发的?
    钢琴
    SQL函数
  • 原文地址:https://www.cnblogs.com/hichens/p/12785982.html
Copyright © 2011-2022 走看看