{"id":995,"date":"2020-11-05T10:24:00","date_gmt":"2020-11-05T02:24:00","guid":{"rendered":"http:\/\/blog.5danyuan.com\/?p=995"},"modified":"2020-11-05T10:24:00","modified_gmt":"2020-11-05T02:24:00","slug":"python-%e6%8f%90%e5%8f%96%e5%9b%be%e7%89%87%e7%9a%84%e4%b8%bb%e4%bd%93%e9%a2%9c%e8%89%b2","status":"publish","type":"post","link":"https:\/\/blog.5danyuan.com\/?p=995","title":{"rendered":"Python \u63d0\u53d6\u56fe\u7247\u7684\u4e3b\u4f53\u989c\u8272"},"content":{"rendered":"\n<p>\u5982\u679c\u8981\u8ba1\u7b97\u4e00\u4e2a\u56fe\u7247\u7684\u5e73\u5747\u8272\u6216\u8005\u662f\u4e3b\u9898\u8272\uff0c\u8fc7\u53bb\u7684\u505a\u6cd5\u662f\u904d\u5386\u56fe\u7247\u7684\u50cf\u7d20\u70b9\uff0c\u7136\u540e\u5206\u522b\u9488\u5bf9R\\G\\B\u505a\u52a0\u6743\u5e73\u5747\u3002\u4f46\u662f\u8fd9\u6837\u4f1a\u4ea7\u751f\u4e00\u4e2a\u610f\u6599\u4e4b\u5916\u7684\u7ed3\u679c\uff0c\u5c31\u662f\u8fd9\u4e2a\u5e73\u5747\u503c\u7684\u989c\u8272\uff0c\u5728\u8fd9\u4e2a\u56fe\u7247\u4e0a\u6839\u672c\u5c31\u6ca1\u6709\u51fa\u73b0\u8fc7\u3002\u5b83\u4ec5\u4ec5\u662f\u6570\u5b66\u610f\u4e49\u4e0a\u7684\u5e73\u5747\u503c\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u53e6\u5916\u4e00\u79cd\u601d\u8def\u5c31\u662f\u6839\u636e\u56fe\u7247\u91cc\u7684\u989c\u8272\u505a\u805a\u7c7b\u5206\u6790\uff0c\u53ea\u5728\u5df2\u7ecf\u5b58\u5728\u7684\u989c\u8272\u8303\u56f4\u91cc\u8ba1\u7b97\u3002\u8fd9\u91cc\u53ef\u4ee5\u4f7f\u7528 k-means \u4f20\u7edf\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from skimage import io\nfrom sklearn.cluster import KMeans\nimport numpy as np\n\n# k-means\u4e2d\u7684k\u503c\uff0c\u5373\u9009\u62e9\u51e0\u4e2a\u4e2d\u5fc3\u70b9\nk = 3\n\nimg = io.imread('city.jpg')\n# \u8f6c\u6362\u6570\u636e\u7ef4\u5ea6\nimg_ori_shape = img.shape\nimg1 = img.reshape((img_ori_shape&#91;0] * img_ori_shape&#91;1], img_ori_shape&#91;2]))\nimg_shape = img1.shape\n\n# \u83b7\u53d6\u56fe\u7247\u8272\u5f69\u5c42\u6570\nn_channels = img_shape&#91;1]\n# \u6784\u9020\u805a\u7c7b\u5668\nestimator = KMeans(n_clusters=k, max_iter= 4000, init='k-means++', n_init=50)\nestimator.fit(img1) # \u805a\u7c7b\ncentroids = estimator.cluster_centers_  # \u83b7\u53d6\u805a\u7c7b\u4e2d\u5fc3\n\n# \u751f\u6210\u4e00\u4e2a\u53ef\u89c6\u5316\u77e9\u9635\nresult = &#91;]\nresult_width = 200\nresult_height_per_center = 80\nfor center_index in range(k):\n    result.append(np.full((result_width * result_height_per_center, n_channels), \n    centroids&#91;center_index], dtype=int))\nresult = np.array(result)\nresult = result.reshape((result_height_per_center * k, result_width, n_channels))\n# \u4fdd\u5b58\u77e9\u9635\u5230\u56fe\u7247\nio.imsave('result.jpg', result)\n<\/code><\/pre>\n\n\n\n<p>\u6700\u540e\u653e\u4e0a\u6d4b\u8bd5\u4f7f\u7528\u7684\u56fe\u7247\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u8bd5\u4e00\u8bd5\uff0c\u987a\u5e8f\u6709\u53ef\u80fd\u4e0d\u4e00\u6837\uff0c\u4f46\u662f\u989c\u8272\u5e94\u8be5\u662f\u8fd9\u51e0\u4e2a\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"225\" src=\"https:\/\/blog.5danyuan.com\/wp-content\/uploads\/2020\/08\/city.jpg\" alt=\"\" class=\"wp-image-996\" srcset=\"https:\/\/blog.5danyuan.com\/wp-content\/uploads\/2020\/08\/city.jpg 400w, https:\/\/blog.5danyuan.com\/wp-content\/uploads\/2020\/08\/city-300x169.jpg 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"200\" height=\"400\" src=\"https:\/\/blog.5danyuan.com\/wp-content\/uploads\/2020\/08\/result.jpg\" alt=\"\" class=\"wp-image-997\" srcset=\"https:\/\/blog.5danyuan.com\/wp-content\/uploads\/2020\/08\/result.jpg 200w, https:\/\/blog.5danyuan.com\/wp-content\/uploads\/2020\/08\/result-150x300.jpg 150w\" sizes=\"auto, (max-width: 200px) 100vw, 200px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u8981\u8ba1\u7b97\u4e00\u4e2a\u56fe\u7247\u7684\u5e73\u5747\u8272\u6216\u8005\u662f\u4e3b\u9898\u8272\uff0c\u8fc7\u53bb\u7684\u505a\u6cd5\u662f\u904d\u5386\u56fe\u7247\u7684\u50cf\u7d20\u70b9\uff0c\u7136\u540e\u5206\u522b\u9488\u5bf9R\\G\\B\u505a\u52a0\u6743\u5e73\u5747\u3002\u4f46\u662f\u8fd9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[300,5],"tags":[323],"class_list":["post-995","post","type-post","status-publish","format-standard","hentry","category-python","category-daily","tag-323"],"_links":{"self":[{"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/posts\/995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=995"}],"version-history":[{"count":0,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=\/wp\/v2\/posts\/995\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.5danyuan.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}