{"id":1017430,"date":"2024-12-27T12:27:57","date_gmt":"2024-12-27T04:27:57","guid":{"rendered":""},"modified":"2024-12-27T12:28:10","modified_gmt":"2024-12-27T04:28:10","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%bb%98%e5%88%b6%e7%85%a7%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1017430.html","title":{"rendered":"\u5982\u4f55\u7528python\u7ed8\u5236\u7167\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25160326\/b337ca9e-6038-43a0-b774-7368f43fb1c1.webp\" alt=\"\u5982\u4f55\u7528python\u7ed8\u5236\u7167\u7247\" \/><\/p>\n<p><p> \u4f7f\u7528Python\u7ed8\u5236\u7167\u7247\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u6700\u5e38\u7528\u7684\u5e93\u6709<strong>PIL\uff08Python Imaging Library\uff09\u3001OpenCV\u3001Matplotlib<\/strong>\u7b49\u3002\u8fd9\u4e9b\u5e93\u5404\u81ea\u6709\u4e0d\u540c\u7684\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u4f8b\u5982\uff0cPIL\u975e\u5e38\u9002\u5408\u8fdb\u884c\u56fe\u50cf\u7684\u57fa\u672c\u5904\u7406\u548c\u64cd\u4f5c\uff0cOpenCV\u5219\u5728\u8ba1\u7b97\u673a\u89c6\u89c9\u65b9\u9762\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u800cMatplotlib\u5219\u5e38\u7528\u4e8e\u6570\u636e\u7684\u53ef\u89c6\u5316\u548c\u7b80\u5355\u56fe\u50cf\u7684\u7ed8\u5236\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u8fdb\u884c\u7167\u7247\u7684\u7ed8\u5236\u548c\u5904\u7406\uff0c\u5c24\u5176\u662f\u5982\u4f55\u5229\u7528\u5b83\u4eec\u7684\u7279\u6027\u6765\u5b9e\u73b0\u7279\u5b9a\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528PIL\u8fdb\u884c\u56fe\u50cf\u5904\u7406<\/h3>\n<\/p>\n<p><p>PIL\uff08Python Imaging Library\uff09\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u521b\u5efa\u3001\u4fee\u6539\u548c\u4fdd\u5b58\u56fe\u50cf\u7684\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5PIL<\/h4>\n<\/p>\n<p><p>PIL\u5df2\u7ecf\u88abPillow\u5e93\u6240\u66ff\u4ee3\uff0c\u56e0\u6b64\u5b89\u88c5Pillow\u5373\u53ef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u52a0\u8f7d\u548c\u663e\u793a\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u50cf\u662f\u56fe\u50cf\u5904\u7406\u7684\u7b2c\u4e00\u6b65\u3002\u4f7f\u7528PIL\u975e\u5e38\u7b80\u5355\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>img = Image.open(&quot;example.jpg&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>img.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u56fe\u50cf\u7684\u57fa\u672c\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>PIL\u63d0\u4f9b\u4e86\u591a\u79cd\u5bf9\u56fe\u50cf\u8fdb\u884c\u64cd\u4f5c\u7684\u65b9\u6cd5\uff0c\u5982\u88c1\u526a\u3001\u8c03\u6574\u5927\u5c0f\u3001\u65cb\u8f6c\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u8c03\u6574\u5927\u5c0f<\/strong><\/li>\n<\/ul>\n<p><p>\u8c03\u6574\u56fe\u50cf\u5927\u5c0f\u53ef\u4ee5\u4f7f\u7528<code>resize<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/p>\n<p>img_resized = img.resize((300, 300))<\/p>\n<p>img_resized.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u65cb\u8f6c\u56fe\u50cf<\/strong><\/li>\n<\/ul>\n<p><p>\u65cb\u8f6c\u56fe\u50cf\u53ef\u4ee5\u4f7f\u7528<code>rotate<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u65cb\u8f6c\u56fe\u50cf<\/p>\n<p>img_rotated = img.rotate(45)<\/p>\n<p>img_rotated.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u88c1\u526a\u56fe\u50cf<\/strong><\/li>\n<\/ul>\n<p><p>\u88c1\u526a\u56fe\u50cf\u53ef\u4ee5\u4f7f\u7528<code>crop<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u88c1\u526a\u56fe\u50cf<\/p>\n<p>box = (100, 100, 400, 400)<\/p>\n<p>img_cropped = img.crop(box)<\/p>\n<p>img_cropped.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528OpenCV\u8fdb\u884c\u56fe\u50cf\u5904\u7406<\/h3>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u9002\u7528\u4e8e\u66f4\u4e3a\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5OpenCV<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5OpenCV\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u52a0\u8f7d\u548c\u663e\u793a\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>OpenCV\u4f7f\u7528<code>cv2.imread<\/code>\u6765\u52a0\u8f7d\u56fe\u50cf\uff0c\u4f7f\u7528<code>cv2.imshow<\/code>\u6765\u663e\u793a\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u52a0\u8f7d\u56fe\u50cf<\/strong><\/h2>\n<p>img = cv2.imread(&quot;example.jpg&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&quot;Image&quot;, img)<\/p>\n<p>cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u56fe\u50cf\u7684\u57fa\u672c\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>OpenCV\u63d0\u4f9b\u4e86\u5bf9\u56fe\u50cf\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\u7684\u529f\u80fd\uff0c\u5982\u8c03\u6574\u5927\u5c0f\u3001\u65cb\u8f6c\u3001\u88c1\u526a\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u8c03\u6574\u5927\u5c0f<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/p>\n<p>img_resized = cv2.resize(img, (300, 300))<\/p>\n<p>cv2.imshow(&quot;Resized Image&quot;, img_resized)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u65cb\u8f6c\u56fe\u50cf<\/strong><\/li>\n<\/ul>\n<p><p>\u65cb\u8f6c\u56fe\u50cf\u53ef\u4ee5\u901a\u8fc7\u4eff\u5c04\u53d8\u6362\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u56fe\u50cf\u4e2d\u5fc3<\/p>\n<p>(h, w) = img.shape[:2]<\/p>\n<p>center = (w \/\/ 2, h \/\/ 2)<\/p>\n<h2><strong>\u8ba1\u7b97\u65cb\u8f6c\u77e9\u9635<\/strong><\/h2>\n<p>M = cv2.getRotationMatrix2D(center, 45, 1.0)<\/p>\n<h2><strong>\u8fdb\u884c\u65cb\u8f6c<\/strong><\/h2>\n<p>img_rotated = cv2.warpAffine(img, M, (w, h))<\/p>\n<p>cv2.imshow(&quot;Rotated Image&quot;, img_rotated)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u88c1\u526a\u56fe\u50cf<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u88c1\u526a\u56fe\u50cf<\/p>\n<p>img_cropped = img[100:400, 100:400]<\/p>\n<p>cv2.imshow(&quot;Cropped Image&quot;, img_cropped)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Matplotlib\u8fdb\u884c\u56fe\u50cf\u7ed8\u5236<\/h3>\n<\/p>\n<p><p>Matplotlib\u901a\u5e38\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\uff0c\u4f46\u5b83\u4e5f\u652f\u6301\u57fa\u672c\u7684\u56fe\u50cf\u5904\u7406\u548c\u663e\u793a\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Matplotlib<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5Matplotlib\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u52a0\u8f7d\u548c\u663e\u793a\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>Matplotlib\u4f7f\u7528<code>imshow<\/code>\u51fd\u6570\u6765\u663e\u793a\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.image as mpimg<\/p>\n<h2><strong>\u52a0\u8f7d\u56fe\u50cf<\/strong><\/h2>\n<p>img = mpimg.imread(&quot;example.jpg&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imshow(img)<\/p>\n<p>plt.axis(&#39;off&#39;)  # \u4e0d\u663e\u793a\u5750\u6807\u8f74<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u56fe\u50cf\u7684\u57fa\u672c\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>Matplotlib\u867d\u7136\u4e0d\u5982PIL\u548cOpenCV\u5728\u56fe\u50cf\u64cd\u4f5c\u65b9\u9762\u5f3a\u5927\uff0c\u4f46\u5b83\u7684\u4f18\u52bf\u5728\u4e8e\u4e0e\u6570\u636e\u53ef\u89c6\u5316\u7684\u7ed3\u5408\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u8c03\u6574\u5927\u5c0f<\/strong><\/li>\n<\/ul>\n<p><p>Matplotlib\u672c\u8eab\u4e0d\u652f\u6301\u8c03\u6574\u56fe\u50cf\u5927\u5c0f\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7PIL\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u4f7f\u7528PIL\u8c03\u6574\u5927\u5c0f<\/strong><\/h2>\n<p>img_pil = Image.fromarray(img)<\/p>\n<p>img_resized = img_pil.resize((300, 300))<\/p>\n<h2><strong>\u663e\u793a\u8c03\u6574\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imshow(img_resized)<\/p>\n<p>plt.axis(&#39;off&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u65cb\u8f6c\u56fe\u50cf<\/strong><\/li>\n<\/ul>\n<p><p>\u540c\u6837\u5730\uff0c\u65cb\u8f6c\u53ef\u4ee5\u4f7f\u7528PIL\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528PIL\u65cb\u8f6c\u56fe\u50cf<\/p>\n<p>img_rotated = img_pil.rotate(45)<\/p>\n<h2><strong>\u663e\u793a\u65cb\u8f6c\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imshow(img_rotated)<\/p>\n<p>plt.axis(&#39;off&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u88c1\u526a\u56fe\u50cf<\/strong><\/li>\n<\/ul>\n<p><p>\u88c1\u526a\u56fe\u50cf\u4e5f\u53ef\u4ee5\u901a\u8fc7PIL\u5b8c\u6210\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528PIL\u88c1\u526a\u56fe\u50cf<\/p>\n<p>box = (100, 100, 400, 400)<\/p>\n<p>img_cropped = img_pil.crop(box)<\/p>\n<h2><strong>\u663e\u793a\u88c1\u526a\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imshow(img_cropped)<\/p>\n<p>plt.axis(&#39;off&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u591a\u4e2a\u5de5\u5177\u8fdb\u884c\u9ad8\u7ea7\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u7ed3\u5408\u591a\u4e2a\u5de5\u5177\u6765\u5b8c\u6210\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u63a2\u8ba8\u4e00\u4e9b\u9ad8\u7ea7\u7684\u56fe\u50cf\u5904\u7406\u6280\u672f\uff0c\u5e76\u5c55\u793a\u5982\u4f55\u7ed3\u5408\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>1. \u56fe\u50cf\u6ee4\u6ce2<\/h4>\n<\/p>\n<p><p>\u56fe\u50cf\u6ee4\u6ce2\u662f\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u91cd\u8981\u6280\u672f\uff0c\u7528\u4e8e\u53bb\u566a\u3001\u8fb9\u7f18\u68c0\u6d4b\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528OpenCV\u8fdb\u884c\u9ad8\u65af\u6ee4\u6ce2<\/strong><\/li>\n<\/ul>\n<p><p>\u9ad8\u65af\u6ee4\u6ce2\u662f\u4e00\u79cd\u5e38\u7528\u7684\u53bb\u566a\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9ad8\u65af\u6ee4\u6ce2<\/p>\n<p>img_blurred = cv2.GaussianBlur(img, (5, 5), 0)<\/p>\n<p>cv2.imshow(&quot;Blurred Image&quot;, img_blurred)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528OpenCV\u8fdb\u884c\u8fb9\u7f18\u68c0\u6d4b<\/strong><\/li>\n<\/ul>\n<p><p>Canny\u8fb9\u7f18\u68c0\u6d4b\u662fOpenCV\u4e2d\u5e38\u7528\u7684\u8fb9\u7f18\u68c0\u6d4b\u7b97\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Canny\u8fb9\u7f18\u68c0\u6d4b<\/p>\n<p>edges = cv2.Canny(img, 100, 200)<\/p>\n<p>cv2.imshow(&quot;Edges&quot;, edges)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u56fe\u50cf\u53d8\u6362<\/h4>\n<\/p>\n<p><p>\u56fe\u50cf\u53d8\u6362\u7528\u4e8e\u56fe\u50cf\u7684\u51e0\u4f55\u53d8\u6362\uff0c\u5982\u5e73\u79fb\u3001\u65cb\u8f6c\u3001\u7f29\u653e\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u4eff\u5c04\u53d8\u6362<\/strong><\/li>\n<\/ul>\n<p><p>\u4eff\u5c04\u53d8\u6362\u53ef\u4ee5\u5b9e\u73b0\u5e73\u79fb\u3001\u65cb\u8f6c\u3001\u7f29\u653e\u7b49\u7ec4\u5408\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4eff\u5c04\u53d8\u6362\u77e9\u9635<\/p>\n<p>pts1 = np.float32([[50, 50], [200, 50], [50, 200]])<\/p>\n<p>pts2 = np.float32([[10, 100], [200, 50], [100, 250]])<\/p>\n<p>M = cv2.getAffineTransform(pts1, pts2)<\/p>\n<h2><strong>\u5e94\u7528\u4eff\u5c04\u53d8\u6362<\/strong><\/h2>\n<p>img_affine = cv2.warpAffine(img, M, (cols, rows))<\/p>\n<p>cv2.imshow(&quot;Affine Transform&quot;, img_affine)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u900f\u89c6\u53d8\u6362<\/strong><\/li>\n<\/ul>\n<p><p>\u900f\u89c6\u53d8\u6362\u7528\u4e8e\u6821\u6b63\u56fe\u50cf\u7684\u89c6\u89d2\u5931\u771f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u900f\u89c6\u53d8\u6362\u77e9\u9635<\/p>\n<p>pts1 = np.float32([[56, 65], [368, 52], [28, 387], [389, 390]])<\/p>\n<p>pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])<\/p>\n<p>M = cv2.getPerspectiveTransform(pts1, pts2)<\/p>\n<h2><strong>\u5e94\u7528\u900f\u89c6\u53d8\u6362<\/strong><\/h2>\n<p>img_perspective = cv2.warpPerspective(img, M, (300, 300))<\/p>\n<p>cv2.imshow(&quot;Perspective Transform&quot;, img_perspective)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u9ad8\u7ea7\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u56fe\u50cf\u5904\u7406\u6280\u672f\u4e0d\u4ec5\u7528\u4e8e\u7b80\u5355\u7684\u56fe\u50cf\u64cd\u4f5c\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u9ad8\u7ea7\u5e94\u7528\uff0c\u5982\u56fe\u50cf\u8bc6\u522b\u3001\u56fe\u50cf\u5206\u5272\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u56fe\u50cf\u8bc6\u522b<\/h4>\n<\/p>\n<p><p>\u56fe\u50cf\u8bc6\u522b\u662f\u8ba1\u7b97\u673a\u89c6\u89c9\u4e2d\u7684\u6838\u5fc3\u4efb\u52a1\u4e4b\u4e00\u3002\u53ef\u4ee5\u4f7f\u7528\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff08\u5982TensorFlow\u3001PyTorch\uff09\u7ed3\u5408OpenCV\u5b9e\u73b0\u56fe\u50cf\u8bc6\u522b\u3002<\/p>\n<\/p>\n<p><h4>2. \u56fe\u50cf\u5206\u5272<\/h4>\n<\/p>\n<p><p>\u56fe\u50cf\u5206\u5272\u7528\u4e8e\u5c06\u56fe\u50cf\u5212\u5206\u4e3a\u4e0d\u540c\u7684\u533a\u57df\u3002\u5728OpenCV\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528GrabCut\u7b97\u6cd5\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316mask<\/p>\n<p>mask = np.zeros(img.shape[:2], np.uint8)<\/p>\n<h2><strong>\u5b9a\u4e49\u80cc\u666f\u548c\u524d\u666f\u6a21\u578b<\/strong><\/h2>\n<p>bgdModel = np.zeros((1, 65), np.float64)<\/p>\n<p>fgdModel = np.zeros((1, 65), np.float64)<\/p>\n<h2><strong>\u5b9a\u4e49\u77e9\u5f62\u533a\u57df<\/strong><\/h2>\n<p>rect = (50, 50, 450, 290)<\/p>\n<h2><strong>\u5e94\u7528GrabCut\u7b97\u6cd5<\/strong><\/h2>\n<p>cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT)<\/p>\n<h2><strong>\u4fee\u6539mask\u5e76\u83b7\u53d6\u7ed3\u679c<\/strong><\/h2>\n<p>mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype(&#39;uint8&#39;)<\/p>\n<p>img_result = img * mask2[:, :, np.newaxis]<\/p>\n<p>cv2.imshow(&quot;Segmented Image&quot;, img_result)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u4e0d\u540c\u5e93\u6765\u5b9e\u73b0\u7167\u7247\u7684\u7ed8\u5236\u548c\u5904\u7406\u3002\u4ece\u57fa\u672c\u7684\u56fe\u50cf\u64cd\u4f5c\u5230\u9ad8\u7ea7\u7684\u56fe\u50cf\u5904\u7406\u5e94\u7528\uff0c\u6bcf\u4e2a\u6b65\u9aa4\u90fd\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u8fdb\u884c\u8c03\u6574\u548c\u4f18\u5316\u3002\u65e0\u8bba\u662f\u521d\u5b66\u8005\u8fd8\u662f\u6709\u7ecf\u9a8c\u7684\u5f00\u53d1\u8005\uff0c\u8fd9\u4e9b\u5de5\u5177\u90fd\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u548c\u5904\u7406\u56fe\u50cf\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u5982\u4f55\u4f7f\u7528Python\u5e93\u6765\u5904\u7406\u548c\u7ed8\u5236\u7167\u7247\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u5904\u7406\u548c\u7ed8\u5236\u7167\u7247\uff0c\u5176\u4e2d\u6700\u5e38\u89c1\u7684\u662fPillow\u548cMatplotlib\u3002Pillow\u662f\u4e00\u4e2a\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u80fd\u591f\u6253\u5f00\u3001\u4fee\u6539\u548c\u4fdd\u5b58\u56fe\u50cf\uff1b\u800cMatplotlib\u5219\u4e3b\u8981\u7528\u4e8e\u7ed8\u5236\u56fe\u5f62\u548c\u53ef\u89c6\u5316\u6570\u636e\u3002\u901a\u8fc7\u7ed3\u5408\u8fd9\u4e24\u4e2a\u5e93\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8bfb\u53d6\u7167\u7247\u5e76\u5728\u4e0d\u540c\u7684\u5750\u6807\u7cfb\u4e2d\u8fdb\u884c\u5c55\u793a\u6216\u7f16\u8f91\u3002<\/p>\n<p><strong>2. \u6709\u6ca1\u6709\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\u53ef\u4ee5\u7ed8\u5236\u4e00\u5f20\u7167\u7247\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u4f7f\u7528Matplotlib\u548cPillow\u5e93\u6765\u8bfb\u53d6\u5e76\u663e\u793a\u4e00\u5f20\u7167\u7247\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image\nimport matplotlib.pyplot as plt\n\n# \u6253\u5f00\u56fe\u7247\nimage = Image.open(&#39;your_image.jpg&#39;)\n\n# \u663e\u793a\u56fe\u7247\nplt.imshow(image)\nplt.axis(&#39;off&#39;)  # \u5173\u95ed\u5750\u6807\u8f74\nplt.show()\n<\/code><\/pre>\n<p>\u53ea\u9700\u5c06 <code>&#39;your_image.jpg&#39;<\/code> \u66ff\u6362\u4e3a\u4f60\u60f3\u8981\u7ed8\u5236\u7684\u56fe\u7247\u8def\u5f84\uff0c\u8fd0\u884c\u4ee3\u7801\u540e\u5373\u53ef\u770b\u5230\u7167\u7247\u3002<\/p>\n<p><strong>3. \u4f7f\u7528Python\u7ed8\u5236\u7167\u7247\u65f6\uff0c\u5982\u4f55\u8c03\u6574\u56fe\u7247\u7684\u5c3a\u5bf8\u548c\u683c\u5f0f\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Pillow\u5e93\u65f6\uff0c\u53ef\u4ee5\u8f7b\u677e\u8c03\u6574\u56fe\u7247\u7684\u5c3a\u5bf8\u548c\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 <code>resize()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u6539\u53d8\u56fe\u7247\u7684\u5927\u5c0f\uff0c\u4f7f\u7528 <code>save()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5c06\u56fe\u7247\u4fdd\u5b58\u4e3a\u4e0d\u540c\u7684\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\"># \u8c03\u6574\u56fe\u7247\u5c3a\u5bf8\nresized_image = image.resize((width, height))  # \u66ff\u6362\u4e3a\u6240\u9700\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\n\n# \u4fdd\u5b58\u4e3a\u4e0d\u540c\u683c\u5f0f\nresized_image.save(&#39;resized_image.png&#39;, format=&#39;PNG&#39;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u7b80\u5355\u7684\u64cd\u4f5c\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u8c03\u6574\u56fe\u7247\u7684\u89c6\u89c9\u6548\u679c\u548c\u5b58\u50a8\u683c\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u7ed8\u5236\u7167\u7247\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u6700\u5e38\u7528\u7684\u5e93\u6709PIL\uff08Python Imaging Library [&hellip;]","protected":false},"author":3,"featured_media":1017452,"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\/1017430"}],"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=1017430"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1017430\/revisions"}],"predecessor-version":[{"id":1017454,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1017430\/revisions\/1017454"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1017452"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1017430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1017430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1017430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}