{"id":1133281,"date":"2025-01-08T21:04:59","date_gmt":"2025-01-08T13:04:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1133281.html"},"modified":"2025-01-08T21:05:02","modified_gmt":"2025-01-08T13:05:02","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%af%b9%e7%81%b0%e5%ba%a6%e5%9b%be%e5%83%8f%e8%bf%9b%e8%a1%8c%e7%89%b9%e5%be%81%e6%8f%90%e5%8f%96","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1133281.html","title":{"rendered":"\u5982\u4f55\u7528python\u5bf9\u7070\u5ea6\u56fe\u50cf\u8fdb\u884c\u7279\u5f81\u63d0\u53d6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102546\/403e7237-c550-402e-9ba2-cc78cd61388d.webp\" alt=\"\u5982\u4f55\u7528python\u5bf9\u7070\u5ea6\u56fe\u50cf\u8fdb\u884c\u7279\u5f81\u63d0\u53d6\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528Python\u5bf9\u7070\u5ea6\u56fe\u50cf\u8fdb\u884c\u7279\u5f81\u63d0\u53d6<\/strong><\/p>\n<\/p>\n<p><p>\u7528Python\u5bf9\u7070\u5ea6\u56fe\u50cf\u8fdb\u884c\u7279\u5f81\u63d0\u53d6\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>OpenCV\u3001scikit-image\u3001Pillow\u3001numpy<\/strong>\u7b49\u5de5\u5177\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u8fdb\u884c\u7279\u5f81\u63d0\u53d6\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\u3002<strong>OpenCV\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u53ef\u4ee5\u5904\u7406\u5404\u79cd\u56fe\u50cf\u5904\u7406\u4efb\u52a1<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u5229\u7528\u8fd9\u4e9b\u5de5\u5177\u8fdb\u884c\u7070\u5ea6\u56fe\u50cf\u7279\u5f81\u63d0\u53d6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001OpenCV\u8fdb\u884c\u7279\u5f81\u63d0\u53d6<\/h3>\n<\/p>\n<p><p>OpenCV\uff08Open Source Computer Vision Library\uff09\u662f\u4e00\u4e2a\u5f00\u6e90\u8ba1\u7b97\u673a\u89c6\u89c9\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u8f6f\u4ef6\u5e93\u3002\u5b83\u5305\u542b\u4e86\u6570\u767e\u79cd\u8ba1\u7b97\u673a\u89c6\u89c9\u7b97\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u3001\u7279\u5f81\u63d0\u53d6\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8bfb\u53d6\u7070\u5ea6\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165OpenCV\u5e93\u5e76\u8bfb\u53d6\u7070\u5ea6\u56fe\u50cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path_to_image.jpg&#39;, cv2.IMREAD_GRAYSCALE)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528<code>cv2.imread<\/code>\u51fd\u6570\u8bfb\u53d6\u56fe\u50cf\uff0c\u5e76\u6307\u5b9a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a<code>cv2.IMREAD_GRAYSCALE<\/code>\uff0c\u4ee5\u786e\u4fdd\u8bfb\u53d6\u7684\u56fe\u50cf\u662f\u7070\u5ea6\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8fb9\u7f18\u68c0\u6d4b<\/h4>\n<\/p>\n<p><p>\u8fb9\u7f18\u68c0\u6d4b\u662f\u7279\u5f81\u63d0\u53d6\u7684\u4e00\u4e2a\u91cd\u8981\u6b65\u9aa4\u3002Canny\u8fb9\u7f18\u68c0\u6d4b\u7b97\u6cd5\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u8fb9\u7f18\u68c0\u6d4b\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Canny\u8fb9\u7f18\u68c0\u6d4b\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">edges = cv2.Canny(image, 100, 200)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528<code>cv2.Canny<\/code>\u51fd\u6570\u8fdb\u884c\u8fb9\u7f18\u68c0\u6d4b\uff0c\u5e76\u6307\u5b9a\u4e24\u4e2a\u9608\u503c\u53c2\u6570\uff0c\u5206\u522b\u4e3a100\u548c200\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u8f6e\u5ed3\u68c0\u6d4b<\/h4>\n<\/p>\n<p><p>\u8f6e\u5ed3\u68c0\u6d4b\u662f\u53e6\u4e00\u79cd\u5e38\u7528\u7684\u7279\u5f81\u63d0\u53d6\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528OpenCV\u8fdb\u884c\u8f6e\u5ed3\u68c0\u6d4b\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CH<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>N_APPROX_SIMPLE)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528<code>cv2.findContours<\/code>\u51fd\u6570\u68c0\u6d4b\u56fe\u50cf\u4e2d\u7684\u8f6e\u5ed3\uff0c\u5e76\u6307\u5b9a\u4e24\u4e2a\u53c2\u6570\uff0c\u5206\u522b\u4e3a<code>cv2.RETR_TREE<\/code>\u548c<code>cv2.CHAIN_APPROX_SIMPLE<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001scikit-image\u8fdb\u884c\u7279\u5f81\u63d0\u53d6<\/h3>\n<\/p>\n<p><p>scikit-image\u662f\u4e00\u4e2a\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u7684Python\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c\u7279\u5f81\u63d0\u53d6\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8bfb\u53d6\u7070\u5ea6\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165scikit-image\u5e93\u5e76\u8bfb\u53d6\u7070\u5ea6\u56fe\u50cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from skimage import io, color<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = io.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>gray_image = color.rgb2gray(image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528<code>io.imread<\/code>\u51fd\u6570\u8bfb\u53d6\u56fe\u50cf\uff0c\u5e76\u4f7f\u7528<code>color.rgb2gray<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8fb9\u7f18\u68c0\u6d4b<\/h4>\n<\/p>\n<p><p>scikit-image\u4e5f\u63d0\u4f9b\u4e86Canny\u8fb9\u7f18\u68c0\u6d4b\u7b97\u6cd5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Canny\u8fb9\u7f18\u68c0\u6d4b\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from skimage import feature<\/p>\n<p>edges = feature.canny(gray_image, sigma=1.0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528<code>feature.canny<\/code>\u51fd\u6570\u8fdb\u884c\u8fb9\u7f18\u68c0\u6d4b\uff0c\u5e76\u6307\u5b9a\u4e00\u4e2a\u53c2\u6570sigma\uff0c\u5176\u503c\u4e3a1.0\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u7eb9\u7406\u7279\u5f81\u63d0\u53d6<\/h4>\n<\/p>\n<p><p>\u7eb9\u7406\u7279\u5f81\u63d0\u53d6\u662f\u56fe\u50cf\u7279\u5f81\u63d0\u53d6\u7684\u53e6\u4e00\u79cd\u91cd\u8981\u65b9\u6cd5\u3002scikit-image\u63d0\u4f9b\u4e86\u5c40\u90e8\u4e8c\u503c\u6a21\u5f0f\uff08LBP\uff09\u7b97\u6cd5\u6765\u63d0\u53d6\u7eb9\u7406\u7279\u5f81\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528LBP\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from skimage.feature import local_binary_pattern<\/p>\n<p>radius = 3<\/p>\n<p>n_points = 8 * radius<\/p>\n<p>lbp = local_binary_pattern(gray_image, n_points, radius, method=&#39;uniform&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528<code>local_binary_pattern<\/code>\u51fd\u6570\u63d0\u53d6\u7eb9\u7406\u7279\u5f81\uff0c\u5e76\u6307\u5b9a\u4e09\u4e2a\u53c2\u6570\uff0c\u5206\u522b\u4e3a<code>n_points<\/code>\u3001<code>radius<\/code>\u548c<code>method<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001Pillow\u8fdb\u884c\u7279\u5f81\u63d0\u53d6<\/h3>\n<\/p>\n<p><p>Pillow\u662fPython Imaging Library\uff08PIL\uff09\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u63d0\u4f9b\u4e86\u7b80\u5355\u800c\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8bfb\u53d6\u7070\u5ea6\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165Pillow\u5e93\u5e76\u8bfb\u53d6\u7070\u5ea6\u56fe\u50cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.open(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>gray_image = image.convert(&#39;L&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528<code>Image.open<\/code>\u51fd\u6570\u8bfb\u53d6\u56fe\u50cf\uff0c\u5e76\u4f7f\u7528<code>convert<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8fb9\u7f18\u68c0\u6d4b<\/h4>\n<\/p>\n<p><p>Pillow\u672c\u8eab\u4e0d\u63d0\u4f9b\u8fb9\u7f18\u68c0\u6d4b\u529f\u80fd\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u5c06Pillow\u4e0e\u5176\u4ed6\u5e93\uff08\u5982numpy\uff09\u7ed3\u5408\u4f7f\u7528\u6765\u5b9e\u73b0\u8fb9\u7f18\u68c0\u6d4b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from PIL import ImageFilter<\/p>\n<h2><strong>\u5e94\u7528\u8fb9\u7f18\u68c0\u6d4b\u6ee4\u955c<\/strong><\/h2>\n<p>edges = gray_image.filter(ImageFilter.FIND_EDGES)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528<code>ImageFilter.FIND_EDGES<\/code>\u6ee4\u955c\u8fdb\u884c\u8fb9\u7f18\u68c0\u6d4b\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001numpy\u8fdb\u884c\u7279\u5f81\u63d0\u53d6<\/h3>\n<\/p>\n<p><p>numpy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u3002\u5728\u56fe\u50cf\u5904\u7406\u548c\u7279\u5f81\u63d0\u53d6\u4e2d\uff0cnumpy\u4e5f\u626e\u6f14\u7740\u91cd\u8981\u89d2\u8272\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8bfb\u53d6\u7070\u5ea6\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165numpy\u5e93\u5e76\u8bfb\u53d6\u7070\u5ea6\u56fe\u50cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path_to_image.jpg&#39;, cv2.IMREAD_GRAYSCALE)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528OpenCV\u8bfb\u53d6\u7070\u5ea6\u56fe\u50cf\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3anumpy\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u56fe\u50cf\u77e9\u7279\u5f81\u63d0\u53d6<\/h4>\n<\/p>\n<p><p>\u56fe\u50cf\u77e9\u662f\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\uff0c\u5e38\u7528\u4e8e\u5f62\u72b6\u5206\u6790\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528numpy\u8fdb\u884c\u56fe\u50cf\u77e9\u7279\u5f81\u63d0\u53d6\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">moments = cv2.moments(image)<\/p>\n<p>hu_moments = cv2.HuMoments(moments).flatten()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528<code>cv2.moments<\/code>\u51fd\u6570\u8ba1\u7b97\u56fe\u50cf\u7684\u77e9\uff0c\u5e76\u4f7f\u7528<code>cv2.HuMoments<\/code>\u51fd\u6570\u8ba1\u7b97Hu\u77e9\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7efc\u5408\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u7ed3\u5408\u4f7f\u7528\u591a\u4e2a\u5e93\u8fdb\u884c\u56fe\u50cf\u7279\u5f81\u63d0\u53d6\uff0c\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u6548\u679c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u5e94\u7528\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<p>from skimage import feature, io, color<\/p>\n<p>from PIL import Image, ImageFilter<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf\u5e76\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path_to_image.jpg&#39;, cv2.IMREAD_GRAYSCALE)<\/p>\n<p>gray_image = color.rgb2gray(io.imread(&#39;path_to_image.jpg&#39;))<\/p>\n<p>pillow_image = Image.open(&#39;path_to_image.jpg&#39;).convert(&#39;L&#39;)<\/p>\n<h2><strong>OpenCV\u8fb9\u7f18\u68c0\u6d4b<\/strong><\/h2>\n<p>edges_opencv = cv2.Canny(image, 100, 200)<\/p>\n<h2><strong>scikit-image\u8fb9\u7f18\u68c0\u6d4b<\/strong><\/h2>\n<p>edges_skimage = feature.canny(gray_image, sigma=1.0)<\/p>\n<h2><strong>Pillow\u8fb9\u7f18\u68c0\u6d4b<\/strong><\/h2>\n<p>edges_pillow = pillow_image.filter(ImageFilter.FIND_EDGES)<\/p>\n<h2><strong>numpy\u56fe\u50cf\u77e9\u7279\u5f81\u63d0\u53d6<\/strong><\/h2>\n<p>moments = cv2.moments(image)<\/p>\n<p>hu_moments = cv2.HuMoments(moments).flatten()<\/p>\n<h2><strong>\u7eb9\u7406\u7279\u5f81\u63d0\u53d6<\/strong><\/h2>\n<p>radius = 3<\/p>\n<p>n_points = 8 * radius<\/p>\n<p>lbp = feature.local_binary_pattern(gray_image, n_points, radius, method=&#39;uniform&#39;)<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c\uff08\u53ef\u9009\uff09<\/strong><\/h2>\n<p>cv2.imshow(&#39;OpenCV Edges&#39;, edges_opencv)<\/p>\n<p>cv2.imshow(&#39;scikit-image Edges&#39;, edges_skimage.astype(np.uint8) * 255)<\/p>\n<p>edges_pillow.show()<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u7ed3\u5408\u4f7f\u7528OpenCV\u3001scikit-image\u3001Pillow\u548cnumpy\u8fdb\u884c\u56fe\u50cf\u7279\u5f81\u63d0\u53d6\u3002\u901a\u8fc7\u8fd9\u4e9b\u793a\u4f8b\u4ee3\u7801\uff0c\u60a8\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\uff0c\u4ee5\u4fbf\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u8fdb\u884c\u56fe\u50cf\u7279\u5f81\u63d0\u53d6\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7279\u5f81\u9009\u62e9\u4e0e\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u56fe\u50cf\u7279\u5f81\u63d0\u53d6\u540e\uff0c\u901a\u5e38\u9700\u8981\u8fdb\u884c\u7279\u5f81\u9009\u62e9\uff0c\u4ee5\u9009\u62e9\u6700\u5177\u4ee3\u8868\u6027\u7684\u7279\u5f81\u8fdb\u884c\u8fdb\u4e00\u6b65\u5206\u6790\u6216\u5efa\u6a21\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u7279\u5f81\u9009\u62e9\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5e38\u7528\u7684\u7279\u5f81\u9009\u62e9\u65b9\u6cd5\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u65b9\u5dee\u9009\u62e9\u6cd5<\/strong>\uff1a\u9009\u62e9\u65b9\u5dee\u5927\u7684\u7279\u5f81\u3002<\/li>\n<li><strong>\u76f8\u5173\u7cfb\u6570\u6cd5<\/strong>\uff1a\u9009\u62e9\u4e0e\u76ee\u6807\u53d8\u91cf\u76f8\u5173\u6027\u5f3a\u7684\u7279\u5f81\u3002<\/li>\n<li><strong>\u4e3b\u6210\u5206\u5206\u6790\uff08PCA\uff09<\/strong>\uff1a\u901a\u8fc7\u7ebf\u6027\u53d8\u6362\u5c06\u539f\u59cb\u7279\u5f81\u8f6c\u6362\u4e3a\u4e00\u7ec4\u65b0\u7684\u4e0d\u76f8\u5173\u7279\u5f81\u3002<\/li>\n<li><strong>L1\u6b63\u5219\u5316<\/strong>\uff1a\u901a\u8fc7\u589e\u52a0L1\u6b63\u5219\u5316\u9879\uff0c\u4f7f\u5f97\u90e8\u5206\u7279\u5f81\u7684\u7cfb\u6570\u4e3a\u96f6\uff0c\u4ece\u800c\u5b9e\u73b0\u7279\u5f81\u9009\u62e9\u3002<\/li>\n<\/ul>\n<p><h4>2\u3001\u7279\u5f81\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u7279\u5f81\u540e\uff0c\u53ef\u4ee5\u5c06\u5176\u5e94\u7528\u4e8e\u5404\u79cd\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u4e2d\uff0c\u5982\u652f\u6301\u5411\u91cf\u673a\uff08SVM\uff09\u3001\u968f\u673a\u68ee\u6797\u3001\u795e\u7ecf\u7f51\u7edc\u7b49\uff0c\u4ee5\u5b9e\u73b0\u56fe\u50cf\u5206\u7c7b\u3001\u76ee\u6807\u68c0\u6d4b\u7b49\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u968f\u673a\u68ee\u6797\u8fdb\u884c\u56fe\u50cf\u5206\u7c7b\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.ensemble import RandomForestClassifier<\/p>\n<p>from sklearn.model_selection import train_test_split<\/p>\n<p>from sklearn.metrics import accuracy_score<\/p>\n<h2><strong>\u5047\u8bbe\u6211\u4eec\u5df2\u7ecf\u63d0\u53d6\u4e86\u56fe\u50cf\u7684\u7279\u5f81\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728feature_matrix\u4e2d<\/strong><\/h2>\n<h2><strong>\u6807\u7b7e\u5b58\u50a8\u5728labels\u6570\u7ec4\u4e2d<\/strong><\/h2>\n<h2><strong>\u5212\u5206\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6<\/strong><\/h2>\n<p>X_train, X_test, y_train, y_test = train_test_split(feature_matrix, labels, test_size=0.2, random_state=42)<\/p>\n<h2><strong>\u8bad\u7ec3\u968f\u673a\u68ee\u6797\u5206\u7c7b\u5668<\/strong><\/h2>\n<p>clf = RandomForestClassifier(n_estimators=100, random_state=42)<\/p>\n<p>clf.fit(X_train, y_train)<\/p>\n<h2><strong>\u8fdb\u884c\u9884\u6d4b<\/strong><\/h2>\n<p>y_pred = clf.predict(X_test)<\/p>\n<h2><strong>\u8ba1\u7b97\u51c6\u786e\u7387<\/strong><\/h2>\n<p>accuracy = accuracy_score(y_test, y_pred)<\/p>\n<p>print(f&#39;Accuracy: {accuracy * 100:.2f}%&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528<code>RandomForestClassifier<\/code>\u8fdb\u884c\u56fe\u50cf\u5206\u7c7b\uff0c\u5e76\u4f7f\u7528<code>train_test_split<\/code>\u5212\u5206\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>accuracy_score<\/code>\u8ba1\u7b97\u5206\u7c7b\u51c6\u786e\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Python\u5bf9\u7070\u5ea6\u56fe\u50cf\u8fdb\u884c\u7279\u5f81\u63d0\u53d6\uff0c\u5305\u62ec\u4f7f\u7528OpenCV\u3001scikit-image\u3001Pillow\u548cnumpy\u7b49\u5de5\u5177\u8fdb\u884c\u8fb9\u7f18\u68c0\u6d4b\u3001\u8f6e\u5ed3\u68c0\u6d4b\u3001\u7eb9\u7406\u7279\u5f81\u63d0\u53d6\u7b49\u64cd\u4f5c\u3002\u901a\u8fc7\u8fd9\u4e9b\u793a\u4f8b\u4ee3\u7801\uff0c\u60a8\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\uff0c\u4ee5\u4fbf\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u8fdb\u884c\u56fe\u50cf\u7279\u5f81\u63d0\u53d6\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u8ba8\u8bba\u4e86\u7279\u5f81\u9009\u62e9\u7684\u65b9\u6cd5\u548c\u7279\u5f81\u5728\u673a\u5668\u5b66\u4e60\u4e2d\u7684\u5e94\u7528\u3002\u901a\u8fc7\u9009\u62e9\u6700\u5177\u4ee3\u8868\u6027\u7684\u7279\u5f81\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6a21\u578b\u7684\u6027\u80fd\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7279\u5f81\u63d0\u53d6\u548c\u9009\u62e9\u662f\u4e00\u4e2a\u590d\u6742\u800c\u5173\u952e\u7684\u8fc7\u7a0b\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u95ee\u9898\u548c\u6570\u636e\u8fdb\u884c\u8c03\u6574\u548c\u4f18\u5316\u3002\u5e0c\u671b\u672c\u6587\u80fd\u4e3a\u60a8\u63d0\u4f9b\u4e00\u4e9b\u6709\u4ef7\u503c\u7684\u53c2\u8003\u548c\u6307\u5bfc\uff0c\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u8fdb\u884c\u56fe\u50cf\u7279\u5f81\u63d0\u53d6\u548c\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u63d0\u53d6\u7070\u5ea6\u56fe\u50cf\u7684\u7279\u5f81\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u7279\u5f81\u63d0\u53d6\u901a\u5e38\u6d89\u53ca\u4f7f\u7528\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u5982OpenCV\u548cscikit-image\u3002\u53ef\u4ee5\u901a\u8fc7\u8fb9\u7f18\u68c0\u6d4b\u3001\u7eb9\u7406\u5206\u6790\u6216\u5f62\u72b6\u8bc6\u522b\u7b49\u65b9\u6cd5\u6765\u63d0\u53d6\u7279\u5f81\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u52a0\u8f7d\u7070\u5ea6\u56fe\u50cf\u3001\u5e94\u7528\u76f8\u5e94\u7684\u7279\u5f81\u63d0\u53d6\u7b97\u6cd5\uff08\u5982SIFT\u3001HOG\u7b49\uff09\uff0c\u5e76\u5c06\u63d0\u53d6\u7684\u7279\u5f81\u4fdd\u5b58\u4e3a\u7279\u5f81\u5411\u91cf\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5e38\u7528\u7684Python\u5e93\u53ef\u4ee5\u7528\u4e8e\u7070\u5ea6\u56fe\u50cf\u7279\u5f81\u63d0\u53d6\uff1f<\/strong><br \/>\u5e38\u7528\u7684Python\u5e93\u5305\u62ecOpenCV\u3001scikit-image\u548cPIL\uff08Pillow\uff09\u3002OpenCV\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u8fdb\u884c\u8fb9\u7f18\u68c0\u6d4b\u548c\u7279\u5f81\u70b9\u63d0\u53d6\uff1bscikit-image\u5219\u4e13\u6ce8\u4e8e\u56fe\u50cf\u5904\u7406\u7684\u79d1\u5b66\u8ba1\u7b97\uff1bPillow\u5219\u9002\u5408\u57fa\u672c\u56fe\u50cf\u64cd\u4f5c\u548c\u5904\u7406\u3002\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u8f7b\u677e\u5b9e\u73b0\u7279\u5f81\u63d0\u53d6\u7684\u9700\u6c42\u3002<\/p>\n<p><strong>\u5728\u7279\u5f81\u63d0\u53d6\u8fc7\u7a0b\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u56fe\u50cf\u566a\u58f0\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u7279\u5f81\u63d0\u53d6\u524d\uff0c\u9884\u5904\u7406\u56fe\u50cf\u4ee5\u53bb\u9664\u566a\u58f0\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u4f7f\u7528\u9ad8\u65af\u6a21\u7cca\u3001\u5747\u503c\u6a21\u7cca\u6216\u4e2d\u503c\u6ee4\u6ce2\u7b49\u6280\u672f\u6765\u5e73\u6ed1\u56fe\u50cf\uff0c\u51cf\u5c11\u566a\u58f0\u5bf9\u7279\u5f81\u63d0\u53d6\u7684\u5f71\u54cd\u3002\u9009\u62e9\u5408\u9002\u7684\u6ee4\u6ce2\u5668\u548c\u53c2\u6570\u80fd\u591f\u663e\u8457\u63d0\u5347\u540e\u7eed\u7279\u5f81\u63d0\u53d6\u7684\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528Python\u5bf9\u7070\u5ea6\u56fe\u50cf\u8fdb\u884c\u7279\u5f81\u63d0\u53d6 \u7528Python\u5bf9\u7070\u5ea6\u56fe\u50cf\u8fdb\u884c\u7279\u5f81\u63d0\u53d6\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u3001scik [&hellip;]","protected":false},"author":3,"featured_media":1133289,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133281"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1133281"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133281\/revisions"}],"predecessor-version":[{"id":1133290,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133281\/revisions\/1133290"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1133289"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1133281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1133281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1133281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}