{"id":1169251,"date":"2025-01-15T16:08:18","date_gmt":"2025-01-15T08:08:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1169251.html"},"modified":"2025-01-15T16:08:20","modified_gmt":"2025-01-15T08:08:20","slug":"python%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86%e5%9b%be%e5%83%8f%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1169251.html","title":{"rendered":"python\u5982\u4f55\u5904\u7406\u56fe\u50cf\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26070247\/ce35b397-e8be-4f6a-8e73-b4c74f337c2a.webp\" alt=\"python\u5982\u4f55\u5904\u7406\u56fe\u50cf\u6570\u636e\" \/><\/p>\n<p><p> Python\u5904\u7406\u56fe\u50cf\u6570\u636e\u4e3b\u8981\u5305\u62ec<strong>\u8bfb\u53d6\u56fe\u50cf\u3001\u5904\u7406\u56fe\u50cf\u3001\u663e\u793a\u56fe\u50cf\u3001\u4fdd\u5b58\u56fe\u50cf<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528Python\u4e2d\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u5982OpenCV\u3001Pillow\u3001scikit-image\u3001Matplotlib\u7b49\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5b8c\u6210\u8fd9\u4e9b\u4efb\u52a1\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u5904\u7406\u56fe\u50cf\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u8bfb\u53d6\u56fe\u50cf<\/h2>\n<\/p>\n<p><p>\u8bfb\u53d6\u56fe\u50cf\u662f\u5904\u7406\u56fe\u50cf\u7684\u7b2c\u4e00\u6b65\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u8bfb\u53d6\u56fe\u50cf\u7684\u65b9\u6cd5\u3002\u5176\u4e2d\uff0cOpenCV\u548cPillow\u662f\u6700\u5e38\u7528\u7684\u4e24\u4e2a\u5e93\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528OpenCV\u8bfb\u53d6\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>OpenCV\uff08Open Source Computer Vision Library\uff09\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\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\u4e3a\u56fe\u50cf\u5904\u7406\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u68c0\u67e5\u56fe\u50cf\u662f\u5426\u6210\u529f\u8bfb\u53d6<\/strong><\/h2>\n<p>if image is None:<\/p>\n<p>    print(&quot;F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led to load image&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Image loaded successfully&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528Pillow\u8bfb\u53d6\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>Pillow\u662fPython Imaging Library\uff08PIL\uff09\u7684\u4e00\u4e2a\u53cb\u597d\u7684\u5206\u652f\uff0c\u652f\u6301\u6253\u5f00\u3001\u64cd\u4f5c\u548c\u4fdd\u5b58\u8bb8\u591a\u4e0d\u540c\u683c\u5f0f\u7684\u56fe\u50cf\u6587\u4ef6\u3002<\/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>\u663e\u793a\u56fe\u50cf\u4fe1\u606f<\/strong><\/h2>\n<p>print(image.format, image.size, image.mode)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u5904\u7406\u56fe\u50cf<\/h2>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u56fe\u50cf\u4e4b\u540e\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u5bf9\u56fe\u50cf\u8fdb\u884c\u4e00\u4e9b\u5904\u7406\u64cd\u4f5c\uff0c\u5982\u8c03\u6574\u5927\u5c0f\u3001\u88c1\u526a\u3001\u65cb\u8f6c\u3001\u6ee4\u6ce2\u7b49\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/h3>\n<\/p>\n<p><p>\u8c03\u6574\u56fe\u50cf\u5927\u5c0f\u662f\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u5e38\u89c1\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u6216Pillow\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528OpenCV\u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/strong><\/h2>\n<p>resized_image = cv2.resize(image, (width, height))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528Pillow\u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/h4>\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>\u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/strong><\/h2>\n<p>resized_image = image.resize((width, height))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u88c1\u526a\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u88c1\u526a\u56fe\u50cf\u4e5f\u662f\u5e38\u89c1\u7684\u64cd\u4f5c\u4e4b\u4e00\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u6216Pillow\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528OpenCV\u88c1\u526a\u56fe\u50cf<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u88c1\u526a\u56fe\u50cf<\/strong><\/h2>\n<p>cropped_image = image[y:y+h, x:x+w]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528Pillow\u88c1\u526a\u56fe\u50cf<\/h4>\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>\u88c1\u526a\u56fe\u50cf<\/strong><\/h2>\n<p>cropped_image = image.crop((x, y, x+w, y+h))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u65cb\u8f6c\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u65cb\u8f6c\u56fe\u50cf\u4e5f\u662f\u5e38\u89c1\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u6216Pillow\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528OpenCV\u65cb\u8f6c\u56fe\u50cf<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u56fe\u50cf\u4e2d\u5fc3<\/strong><\/h2>\n<p>(h, w) = image.shape[:2]<\/p>\n<p>center = (w \/ 2, h \/ 2)<\/p>\n<h2><strong>\u65cb\u8f6c\u56fe\u50cf<\/strong><\/h2>\n<p>M = cv2.getRotationMatrix2D(center, angle, scale)<\/p>\n<p>rotated_image = cv2.warpAffine(image, M, (w, h))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528Pillow\u65cb\u8f6c\u56fe\u50cf<\/h4>\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>\u65cb\u8f6c\u56fe\u50cf<\/strong><\/h2>\n<p>rotated_image = image.rotate(angle)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u663e\u793a\u56fe\u50cf<\/h2>\n<\/p>\n<p><p>\u663e\u793a\u56fe\u50cf\u662f\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u91cd\u8981\u4e00\u6b65\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u3001OpenCV\u3001Pillow\u7b49\u5e93\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528Matplotlib\u663e\u793a\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2aPython 2D\u7ed8\u56fe\u5e93\uff0c\u80fd\u591f\u751f\u6210\u56fe\u50cf\u3001\u56fe\u8868\u548c\u5176\u4ed6\u7ed8\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u8f6c\u6362\u989c\u8272\u7a7a\u95f4\uff08BGR to RGB\uff09<\/strong><\/h2>\n<p>image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imshow(image_rgb)<\/p>\n<p>plt.axis(&#39;off&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528OpenCV\u663e\u793a\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>OpenCV\u4e5f\u53ef\u4ee5\u7528\u6765\u663e\u793a\u56fe\u50cf\uff0c\u4f46\u5176\u663e\u793a\u7a97\u53e3\u4e0d\u5982Matplotlib\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;Image&#39;, image)<\/p>\n<h2><strong>\u7b49\u5f85\u952e\u76d8\u4e8b\u4ef6<\/strong><\/h2>\n<p>cv2.waitKey(0)<\/p>\n<h2><strong>\u5173\u95ed\u6240\u6709\u7a97\u53e3<\/strong><\/h2>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u4f7f\u7528Pillow\u663e\u793a\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>Pillow\u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u663e\u793a\u56fe\u50cf\u529f\u80fd\u3002<\/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>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u4fdd\u5b58\u56fe\u50cf<\/h2>\n<\/p>\n<p><p>\u5904\u7406\u5b8c\u56fe\u50cf\u540e\uff0c\u901a\u5e38\u9700\u8981\u5c06\u56fe\u50cf\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528OpenCV\u6216Pillow\u6765\u4fdd\u5b58\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528OpenCV\u4fdd\u5b58\u56fe\u50cf<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imwrite(&#39;output_image.jpg&#39;, image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528Pillow\u4fdd\u5b58\u56fe\u50cf<\/h3>\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>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>image.save(&#39;output_image.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u56fe\u50cf\u5904\u7406\u7684\u9ad8\u7ea7\u5e94\u7528<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0cPython\u8fd8\u53ef\u4ee5\u7528\u4e8e\u66f4\u9ad8\u7ea7\u7684\u56fe\u50cf\u5904\u7406\u5e94\u7528\uff0c\u5982\u56fe\u50cf\u8fc7\u6ee4\u3001\u8fb9\u7f18\u68c0\u6d4b\u3001\u56fe\u50cf\u5206\u5272\u7b49\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u56fe\u50cf\u8fc7\u6ee4<\/h3>\n<\/p>\n<p><p>\u56fe\u50cf\u8fc7\u6ee4\u662f\u56fe\u50cf\u5904\u7406\u4e2d\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u7528\u4e8e\u5e73\u6ed1\u3001\u53bb\u566a\u3001\u589e\u5f3a\u56fe\u50cf\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528OpenCV\u5b9e\u73b0\u56fe\u50cf\u8fc7\u6ee4\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528OpenCV\u8fdb\u884c\u56fe\u50cf\u8fc7\u6ee4<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u5e94\u7528\u9ad8\u65af\u6a21\u7cca<\/strong><\/h2>\n<p>blurred_image = cv2.GaussianBlur(image, (5, 5), 0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u8fb9\u7f18\u68c0\u6d4b<\/h3>\n<\/p>\n<p><p>\u8fb9\u7f18\u68c0\u6d4b\u7528\u4e8e\u68c0\u6d4b\u56fe\u50cf\u4e2d\u7684\u8fb9\u7f18\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528OpenCV\u8fdb\u884c\u8fb9\u7f18\u68c0\u6d4b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path_to_image.jpg&#39;, cv2.IMREAD_GRAYSCALE)<\/p>\n<h2><strong>\u5e94\u7528Canny\u8fb9\u7f18\u68c0\u6d4b<\/strong><\/h2>\n<p>edges = cv2.Canny(image, 100, 200)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u56fe\u50cf\u5206\u5272<\/h3>\n<\/p>\n<p><p>\u56fe\u50cf\u5206\u5272\u7528\u4e8e\u5c06\u56fe\u50cf\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528OpenCV\u8fdb\u884c\u56fe\u50cf\u5206\u5272<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<h2><strong>\u5e94\u7528\u9608\u503c\u5206\u5272<\/strong><\/h2>\n<p>_, segmented_image = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u4f7f\u7528scikit-image\u8fdb\u884c\u56fe\u50cf\u5904\u7406<\/h2>\n<\/p>\n<p><p>scikit-image\u662f\u4e00\u4e2a\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u7684Python\u5e93\uff0c\u57fa\u4e8escipy\u6784\u5efa\uff0c\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u56fe\u50cf\u5904\u7406\u7684\u5de5\u5177\u548c\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u50cf<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from skimage import io<\/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>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>io.imshow(image)<\/p>\n<p>io.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u56fe\u50cf\u5904\u7406\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>scikit-image\u63d0\u4f9b\u4e86\u8bb8\u591a\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0c\u5982\u8c03\u6574\u5927\u5c0f\u3001\u65cb\u8f6c\u3001\u6ee4\u6ce2\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from skimage import transform<\/p>\n<h2><strong>\u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/strong><\/h2>\n<p>resized_image = transform.resize(image, (new_height, new_width))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u65cb\u8f6c\u56fe\u50cf<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from skimage import transform<\/p>\n<h2><strong>\u65cb\u8f6c\u56fe\u50cf<\/strong><\/h2>\n<p>rotated_image = transform.rotate(image, angle)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5e94\u7528\u6ee4\u6ce2<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from skimage import filters<\/p>\n<h2><strong>\u5e94\u7528\u9ad8\u65af\u6ee4\u6ce2<\/strong><\/h2>\n<p>blurred_image = filters.gaussian(image, sigma=1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u8fb9\u7f18\u68c0\u6d4b<\/h3>\n<\/p>\n<p><p>scikit-image\u63d0\u4f9b\u4e86\u591a\u79cd\u8fb9\u7f18\u68c0\u6d4b\u7b97\u6cd5\uff0c\u5982Canny\u3001Sobel\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from skimage import feature<\/p>\n<h2><strong>\u5e94\u7528Canny\u8fb9\u7f18\u68c0\u6d4b<\/strong><\/h2>\n<p>edges = feature.canny(image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u56fe\u50cf\u5206\u5272<\/h3>\n<\/p>\n<p><p>scikit-image\u63d0\u4f9b\u4e86\u591a\u79cd\u56fe\u50cf\u5206\u5272\u7b97\u6cd5\uff0c\u5982\u9608\u503c\u5206\u5272\u3001\u533a\u57df\u751f\u957f\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u9608\u503c\u5206\u5272<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from skimage import filters<\/p>\n<h2><strong>\u5e94\u7528\u5168\u5c40\u9608\u503c\u5206\u5272<\/strong><\/h2>\n<p>threshold = filters.threshold_otsu(image)<\/p>\n<p>segmented_image = image &gt; threshold<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e03\u3001\u4f7f\u7528TensorFlow\u548cKeras\u8fdb\u884c\u56fe\u50cf\u5904\u7406<\/h2>\n<\/p>\n<p><p>TensorFlow\u548cKeras\u662f\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u8bfb\u53d6\u548c\u9884\u5904\u7406\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>TensorFlow\u548cKeras\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u56fe\u50cf\u8bfb\u53d6\u548c\u9884\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = tf.io.read_file(&#39;path_to_image.jpg&#39;)<\/p>\n<p>image = tf.image.decode_jpeg(image, channels=3)<\/p>\n<h2><strong>\u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/strong><\/h2>\n<p>image = tf.image.resize(image, [height, width])<\/p>\n<h2><strong>\u6807\u51c6\u5316\u56fe\u50cf<\/strong><\/h2>\n<p>image = image \/ 255.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528\u9884\u8bad\u7ec3\u6a21\u578b\u8fdb\u884c\u56fe\u50cf\u5206\u7c7b<\/h3>\n<\/p>\n<p><p>TensorFlow\u548cKeras\u63d0\u4f9b\u4e86\u591a\u79cd\u9884\u8bad\u7ec3\u6a21\u578b\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u56fe\u50cf\u5206\u7c7b\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>from tensorflow.keras.applications import VGG16<\/p>\n<p>from tensorflow.keras.applications.vgg16 import preprocess_input, decode_predictions<\/p>\n<h2><strong>\u52a0\u8f7d\u9884\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model = VGG16(weights=&#39;imagenet&#39;)<\/p>\n<h2><strong>\u9884\u5904\u7406\u56fe\u50cf<\/strong><\/h2>\n<p>image = tf.image.resize(image, [224, 224])<\/p>\n<p>image = preprocess_input(image)<\/p>\n<h2><strong>\u6dfb\u52a0\u6279\u6b21\u7ef4\u5ea6<\/strong><\/h2>\n<p>image = tf.expand_dims(image, axis=0)<\/p>\n<h2><strong>\u8fdb\u884c\u9884\u6d4b<\/strong><\/h2>\n<p>predictions = model.predict(image)<\/p>\n<h2><strong>\u89e3\u7801\u9884\u6d4b\u7ed3\u679c<\/strong><\/h2>\n<p>decoded_predictions = decode_predictions(predictions, top=3)<\/p>\n<p>print(decoded_predictions)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u6a21\u578b\u8fdb\u884c\u56fe\u50cf\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u9884\u8bad\u7ec3\u6a21\u578b\uff0cTensorFlow\u548cKeras\u8fd8\u53ef\u4ee5\u7528\u4e8e\u6784\u5efa\u548c\u8bad\u7ec3\u81ea\u5b9a\u4e49\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>from tensorflow.keras import layers, models<\/p>\n<h2><strong>\u6784\u5efa\u81ea\u5b9a\u4e49\u6a21\u578b<\/strong><\/h2>\n<p>model = models.Sequential([<\/p>\n<p>    layers.Conv2D(32, (3, 3), activation=&#39;relu&#39;, input_shape=(height, width, 3)),<\/p>\n<p>    layers.MaxPooling2D((2, 2)),<\/p>\n<p>    layers.Conv2D(64, (3, 3), activation=&#39;relu&#39;),<\/p>\n<p>    layers.MaxPooling2D((2, 2)),<\/p>\n<p>    layers.Conv2D(64, (3, 3), activation=&#39;relu&#39;),<\/p>\n<p>    layers.Flatten(),<\/p>\n<p>    layers.Dense(64, activation=&#39;relu&#39;),<\/p>\n<p>    layers.Dense(num_classes, activation=&#39;softmax&#39;)<\/p>\n<p>])<\/p>\n<h2><strong>\u7f16\u8bd1\u6a21\u578b<\/strong><\/h2>\n<p>model.compile(optimizer=&#39;adam&#39;, loss=&#39;sparse_categorical_crossentropy&#39;, metrics=[&#39;accuracy&#39;])<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model.fit(train_images, train_labels, epochs=10, validation_data=(val_images, val_labels))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>Python\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5e93\u548c\u5de5\u5177\uff0c\u65e0\u8bba\u662f\u57fa\u672c\u7684\u56fe\u50cf\u8bfb\u53d6\u3001\u5904\u7406\u3001\u663e\u793a\u548c\u4fdd\u5b58\uff0c\u8fd8\u662f\u9ad8\u7ea7\u7684\u56fe\u50cf\u8fc7\u6ee4\u3001\u8fb9\u7f18\u68c0\u6d4b\u3001\u56fe\u50cf\u5206\u5272\u548c\u6df1\u5ea6\u5b66\u4e60\u5e94\u7528\uff0c\u90fd\u53ef\u4ee5\u4f7f\u7528Python\u8f7b\u677e\u5b9e\u73b0\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u7ec4\u5408\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5b8c\u6210\u5404\u79cd\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u5904\u7406\u56fe\u50cf\u6570\u636e\u9700\u8981\u54ea\u4e9b\u5e38\u7528\u5e93\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5904\u7406\u56fe\u50cf\u6570\u636e\u7684\u5e38\u7528\u5e93\u5305\u62ecPIL\uff08Pillow\uff09\u3001OpenCV\u3001NumPy\u548cscikit-image\u3002PIL\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u9002\u5408\u8fdb\u884c\u57fa\u672c\u7684\u56fe\u50cf\u64cd\u4f5c\uff0c\u5982\u6253\u5f00\u3001\u4fdd\u5b58\u548c\u4fee\u6539\u56fe\u50cf\u3002OpenCV\u5219\u4e13\u6ce8\u4e8e\u8ba1\u7b97\u673a\u89c6\u89c9\u5e94\u7528\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u9002\u5408\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002NumPy\u53ef\u4ee5\u7528\u4e8e\u9ad8\u6548\u7684\u6570\u7ec4\u8fd0\u7b97\uff0c\u5e38\u5e38\u4e0e\u56fe\u50cf\u6570\u636e\u7684\u6570\u503c\u5904\u7406\u7ed3\u5408\u4f7f\u7528\uff0c\u800cscikit-image\u5219\u4e13\u6ce8\u4e8e\u56fe\u50cf\u5904\u7406\u7684\u79d1\u5b66\u8ba1\u7b97\u548c\u5206\u6790\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u50cf\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528PIL\u5e93\u4e2d\u7684<code>Image<\/code>\u6a21\u5757\u6765\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u50cf\u3002\u9996\u5148\uff0c\u4f7f\u7528<code>Image.open()<\/code>\u51fd\u6570\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\uff0c\u7136\u540e\u53ef\u4ee5\u5229\u7528<code>show()<\/code>\u65b9\u6cd5\u76f4\u63a5\u663e\u793a\u56fe\u50cf\u3002\u4f8b\u5982\uff0c<code>python from PIL import Image img = Image.open(&#39;image.jpg&#39;) img.show() <\/code> \u53e6\u5916\uff0cOpenCV\u4e5f\u53ef\u4ee5\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u50cf\uff0c\u4f7f\u7528<code>cv2.imread()<\/code>\u548c<code>cv2.imshow()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5bf9\u56fe\u50cf\u8fdb\u884c\u57fa\u672c\u7684\u5904\u7406\u64cd\u4f5c\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u8fdb\u884c\u57fa\u672c\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0c\u5982\u88c1\u526a\u3001\u65cb\u8f6c\u3001\u8c03\u6574\u5927\u5c0f\u548c\u6ee4\u955c\u5e94\u7528\u3002\u4f7f\u7528PIL\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7<code>crop()<\/code>, <code>rotate()<\/code>, \u548c<code>resize()<\/code>\u7b49\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u8fd9\u4e9b\u64cd\u4f5c\u53ef\u4ee5\u8f7b\u677e\u5730\u901a\u8fc7\u4f20\u5165\u76f8\u5e94\u7684\u53c2\u6570\u8fdb\u884c\u5b9a\u5236\u3002\u6b64\u5916\uff0cOpenCV\u4e5f\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u5982<code>cv2.resize()<\/code>\u548c<code>cv2.GaussianBlur()<\/code>\u7b49\uff0c\u9002\u5408\u9700\u8981\u66f4\u590d\u6742\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5904\u7406\u56fe\u50cf\u6570\u636e\u4e3b\u8981\u5305\u62ec\u8bfb\u53d6\u56fe\u50cf\u3001\u5904\u7406\u56fe\u50cf\u3001\u663e\u793a\u56fe\u50cf\u3001\u4fdd\u5b58\u56fe\u50cf\u3002\u5176\u4e2d\uff0c\u4f7f\u7528Python\u4e2d\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406 [&hellip;]","protected":false},"author":3,"featured_media":1169256,"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\/1169251"}],"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=1169251"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169251\/revisions"}],"predecessor-version":[{"id":1169259,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169251\/revisions\/1169259"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1169256"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1169251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1169251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1169251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}