{"id":1122886,"date":"2025-01-08T19:26:30","date_gmt":"2025-01-08T11:26:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1122886.html"},"modified":"2025-01-08T19:26:33","modified_gmt":"2025-01-08T11:26:33","slug":"python%e5%a6%82%e4%bd%95%e5%af%b9%e5%9b%be%e5%83%8f%e5%88%87%e5%89%b2%e5%87%ba%e9%87%8d%e8%a6%81%e9%83%a8%e5%88%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1122886.html","title":{"rendered":"python\u5982\u4f55\u5bf9\u56fe\u50cf\u5207\u5272\u51fa\u91cd\u8981\u90e8\u5206"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084538\/01a2e923-9487-4824-b7eb-2660abb729c3.webp\" alt=\"python\u5982\u4f55\u5bf9\u56fe\u50cf\u5207\u5272\u51fa\u91cd\u8981\u90e8\u5206\" \/><\/p>\n<p><p> <strong>Python\u5bf9\u56fe\u50cf\u8fdb\u884c\u5207\u5272\u4ee5\u63d0\u53d6\u91cd\u8981\u90e8\u5206\u7684\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u56fe\u50cf\u5206\u5272\u7b97\u6cd5\u3001\u8fb9\u7f18\u68c0\u6d4b\u3001\u989c\u8272\u7a7a\u95f4\u8f6c\u6362\u3001\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u3001\u5f62\u6001\u5b66\u64cd\u4f5c<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b<\/strong>\u5728\u8fd1\u5e74\u6765\u83b7\u5f97\u4e86\u5e7f\u6cdb\u5e94\u7528\uff0c\u5c24\u5176\u662f\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\uff08CNN\uff09\u901a\u8fc7\u8bad\u7ec3\u53ef\u4ee5\u81ea\u52a8\u8bc6\u522b\u56fe\u50cf\u4e2d\u7684\u91cd\u8981\u90e8\u5206\u5e76\u8fdb\u884c\u5207\u5272\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5b9e\u73b0\u5bf9\u56fe\u50cf\u7684\u6709\u6548\u5207\u5272\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u56fe\u50cf\u5206\u5272\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>\u56fe\u50cf\u5206\u5272\u662f\u5c06\u56fe\u50cf\u5206\u5272\u6210\u591a\u4e2a\u5b50\u90e8\u5206\u6216\u5bf9\u8c61\u7684\u8fc7\u7a0b\uff0c\u4e3b\u8981\u7528\u4e8e\u56fe\u50cf\u7684\u5206\u6790\u548c\u5904\u7406\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u9608\u503c\u5206\u5272\u3001\u533a\u57df\u751f\u957f\u3001\u5206\u6c34\u5cad\u7b97\u6cd5\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u9608\u503c\u5206\u5272<\/h4>\n<\/p>\n<p><p>\u9608\u503c\u5206\u5272\u662f\u4e00\u79cd\u6700\u7b80\u5355\u7684\u56fe\u50cf\u5206\u5272\u65b9\u6cd5\uff0c\u901a\u8fc7\u8bbe\u5b9a\u4e00\u4e2a\u9608\u503c\uff0c\u5c06\u56fe\u50cf\u5206\u4e3a\u524d\u666f\u548c\u80cc\u666f\u3002\u53ef\u4ee5\u4f7f\u7528OpenCV\u5e93\u4e2d\u7684<code>cv2.threshold<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\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;image.jpg&#39;, 0)<\/p>\n<h2><strong>\u5e94\u7528\u9608\u503c\u5206\u5272<\/strong><\/h2>\n<p>ret, thresh = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Thresholded Image&#39;, thresh)<\/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>2\u3001\u533a\u57df\u751f\u957f<\/h4>\n<\/p>\n<p><p>\u533a\u57df\u751f\u957f\u7b97\u6cd5\u4ece\u79cd\u5b50\u70b9\u5f00\u59cb\uff0c\u901a\u8fc7\u8003\u5bdf\u76f8\u90bb\u50cf\u7d20\u7684\u76f8\u4f3c\u6027\u6765\u51b3\u5b9a\u662f\u5426\u5c06\u76f8\u90bb\u50cf\u7d20\u5408\u5e76\u5230\u533a\u57df\u4e2d\u3002OpenCV\u7684<code>cv2.floodFill<\/code>\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;image.jpg&#39;)<\/p>\n<p>h, w = image.shape[:2]<\/p>\n<h2><strong>\u8bbe\u7f6e\u79cd\u5b50\u70b9<\/strong><\/h2>\n<p>seed_point = (w\/\/2, h\/\/2)<\/p>\n<h2><strong>\u8bbe\u7f6e\u9608\u503c<\/strong><\/h2>\n<p>lo_diff = 20<\/p>\n<p>up_diff = 20<\/p>\n<h2><strong>\u514b\u9686\u539f\u56fe\u50cf<\/strong><\/h2>\n<p>mask = np.zeros((h+2, w+2), np.uint8)<\/p>\n<h2><strong>\u5e94\u7528\u533a\u57df\u751f\u957f<\/strong><\/h2>\n<p>cv2.floodFill(image, mask, seed_point, (0, 255, 0), (lo_diff,)*3, (up_diff,)*3, cv2.FLOODFILL_FIXED_RANGE)<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;FloodFilled Image&#39;, image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5206\u6c34\u5cad\u7b97\u6cd5<\/h4>\n<\/p>\n<p><p>\u5206\u6c34\u5cad\u7b97\u6cd5\u662f\u4e00\u79cd\u57fa\u4e8e\u62d3\u6251\u5b66\u7684\u56fe\u50cf\u5206\u5272\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5177\u6709\u660e\u663e\u8fb9\u754c\u7684\u56fe\u50cf\u3002OpenCV\u63d0\u4f9b\u4e86<code>cv2.watershed<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u5206\u6c34\u5cad\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;image.jpg&#39;)<\/p>\n<p>gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<h2><strong>\u5e94\u7528\u9608\u503c\u5206\u5272<\/strong><\/h2>\n<p>ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)<\/p>\n<h2><strong>\u566a\u58f0\u53bb\u9664<\/strong><\/h2>\n<p>kernel = np.ones((3, 3), np.uint8)<\/p>\n<p>opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)<\/p>\n<h2><strong>\u786e\u5b9a\u80cc\u666f\u533a\u57df<\/strong><\/h2>\n<p>sure_bg = cv2.dilate(opening, kernel, iterations=3)<\/p>\n<h2><strong>\u786e\u5b9a\u524d\u666f\u533a\u57df<\/strong><\/h2>\n<p>dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 5)<\/p>\n<p>ret, sure_fg = cv2.threshold(dist_transform, 0.7 * dist_transform.max(), 255, 0)<\/p>\n<h2><strong>\u627e\u5230\u672a\u77e5\u533a\u57df<\/strong><\/h2>\n<p>sure_fg = np.uint8(sure_fg)<\/p>\n<p>unknown = cv2.subtract(sure_bg, sure_fg)<\/p>\n<h2><strong>\u6807\u8bb0\u6807\u7b7e<\/strong><\/h2>\n<p>ret, markers = cv2.connectedComponents(sure_fg)<\/p>\n<p>markers = markers + 1<\/p>\n<p>markers[unknown == 255] = 0<\/p>\n<h2><strong>\u5e94\u7528\u5206\u6c34\u5cad\u7b97\u6cd5<\/strong><\/h2>\n<p>markers = cv2.watershed(image, markers)<\/p>\n<p>image[markers == -1] = [255, 0, 0]<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Watershed Image&#39;, image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8fb9\u7f18\u68c0\u6d4b<\/h3>\n<\/p>\n<p><p>\u8fb9\u7f18\u68c0\u6d4b\u662f\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u6b65\u9aa4\uff0c\u901a\u8fc7\u68c0\u6d4b\u56fe\u50cf\u4e2d\u7684\u8fb9\u7f18\u6765\u63d0\u53d6\u91cd\u8981\u90e8\u5206\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ecCanny\u8fb9\u7f18\u68c0\u6d4b\u3001Sobel\u7b97\u5b50\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001Canny\u8fb9\u7f18\u68c0\u6d4b<\/h4>\n<\/p>\n<p><p>Canny\u8fb9\u7f18\u68c0\u6d4b\u662f\u4e00\u79cd\u591a\u7ea7\u8fb9\u7f18\u68c0\u6d4b\u7b97\u6cd5\uff0c\u80fd\u591f\u6709\u6548\u5730\u63d0\u53d6\u56fe\u50cf\u4e2d\u7684\u8fb9\u7f18\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;image.jpg&#39;, 0)<\/p>\n<h2><strong>\u5e94\u7528Canny\u8fb9\u7f18\u68c0\u6d4b<\/strong><\/h2>\n<p>edges = cv2.Canny(image, 100, 200)<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Canny Edge Detection&#39;, edges)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001Sobel\u7b97\u5b50<\/h4>\n<\/p>\n<p><p>Sobel\u7b97\u5b50\u662f\u4e00\u79cd\u79bb\u6563\u5fae\u5206\u7b97\u5b50\uff0c\u7ed3\u5408\u4e86\u9ad8\u65af\u5e73\u6ed1\u548c\u5fae\u5206\u6c42\u5bfc\uff0c\u7528\u4e8e\u8ba1\u7b97\u56fe\u50cf\u5f3a\u5ea6\u7684\u68af\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;image.jpg&#39;, 0)<\/p>\n<h2><strong>\u5e94\u7528Sobel\u7b97\u5b50<\/strong><\/h2>\n<p>sobelx = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=5)<\/p>\n<p>sobely = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=5)<\/p>\n<h2><strong>\u5408\u5e76\u7ed3\u679c<\/strong><\/h2>\n<p>sobel_combined = cv2.magnitude(sobelx, sobely)<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Sobel Edge Detection&#39;, sobel_combined)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u989c\u8272\u7a7a\u95f4\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u989c\u8272\u7a7a\u95f4\u8f6c\u6362\u662f\u901a\u8fc7\u5c06\u56fe\u50cf\u4ece\u4e00\u79cd\u989c\u8272\u7a7a\u95f4\u8f6c\u6362\u5230\u53e6\u4e00\u79cd\u989c\u8272\u7a7a\u95f4\u6765\u63d0\u53d6\u91cd\u8981\u90e8\u5206\u3002\u5e38\u7528\u7684\u989c\u8272\u7a7a\u95f4\u5305\u62ecRGB\u3001HSV\u3001Lab\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001RGB\u5230HSV<\/h4>\n<\/p>\n<p><p>HSV\u989c\u8272\u7a7a\u95f4\u66f4\u7b26\u5408\u4eba\u7c7b\u7684\u89c6\u89c9\u611f\u77e5\uff0c\u80fd\u591f\u66f4\u597d\u5730\u5206\u79bb\u56fe\u50cf\u4e2d\u7684\u989c\u8272\u4fe1\u606f\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;image.jpg&#39;)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3aHSV\u989c\u8272\u7a7a\u95f4<\/strong><\/h2>\n<p>hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)<\/p>\n<h2><strong>\u5b9a\u4e49\u989c\u8272\u8303\u56f4<\/strong><\/h2>\n<p>lower_bound = (30, 40, 40)<\/p>\n<p>upper_bound = (90, 255, 255)<\/p>\n<h2><strong>\u63d0\u53d6\u6307\u5b9a\u989c\u8272\u8303\u56f4\u5185\u7684\u90e8\u5206<\/strong><\/h2>\n<p>mask = cv2.inRange(hsv_image, lower_bound, upper_bound)<\/p>\n<p>result = cv2.bitwise_and(image, image, mask=mask)<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;HSV Color Space&#39;, result)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001RGB\u5230Lab<\/h4>\n<\/p>\n<p><p>Lab\u989c\u8272\u7a7a\u95f4\u662f\u57fa\u4e8e\u4eba\u7c7b\u89c6\u89c9\u6a21\u578b\u7684\u989c\u8272\u7a7a\u95f4\uff0c\u66f4\u80fd\u4f53\u73b0\u989c\u8272\u7684\u611f\u77e5\u5dee\u5f02\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;image.jpg&#39;)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3aLab\u989c\u8272\u7a7a\u95f4<\/strong><\/h2>\n<p>lab_image = cv2.cvtColor(image, cv2.COLOR_BGR2Lab)<\/p>\n<h2><strong>\u63d0\u53d6L\u901a\u9053<\/strong><\/h2>\n<p>l_channel, a_channel, b_channel = cv2.split(lab_image)<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;L Channel&#39;, l_channel)<\/p>\n<p>cv2.imshow(&#39;A Channel&#39;, a_channel)<\/p>\n<p>cv2.imshow(&#39;B Channel&#39;, b_channel)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u7279\u522b\u662f\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\uff08CNN\uff09\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\u8868\u73b0\u51fa\u8272\uff0c\u53ef\u4ee5\u81ea\u52a8\u5b66\u4e60\u56fe\u50cf\u4e2d\u7684\u91cd\u8981\u7279\u5f81\u5e76\u8fdb\u884c\u5207\u5272\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u9884\u8bad\u7ec3\u6a21\u578b<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u9884\u8bad\u7ec3\u7684\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\uff0c\u5982U-Net\u3001Mask R-CNN\u7b49\u8fdb\u884c\u56fe\u50cf\u5206\u5272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<p>from keras.models import load_model<\/p>\n<h2><strong>\u52a0\u8f7d\u9884\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model = load_model(&#39;unet_model.h5&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;image.jpg&#39;)<\/p>\n<p>image_resized = cv2.resize(image, (128, 128))<\/p>\n<h2><strong>\u8fdb\u884c\u9884\u6d4b<\/strong><\/h2>\n<p>pred_mask = model.predict(np.expand_dims(image_resized, axis=0))<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Predicted Mask&#39;, pred_mask[0, :, :, 0])<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u81ea\u5b9a\u4e49\u8bad\u7ec3\u6a21\u578b<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u81ea\u5b9a\u4e49\u8bad\u7ec3\u4e00\u4e2a\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u6765\u5b9e\u73b0\u7279\u5b9a\u7684\u56fe\u50cf\u5206\u5272\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<p>from keras.models import Model<\/p>\n<p>from keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate<\/p>\n<h2><strong>\u5b9a\u4e49U-Net\u6a21\u578b\u7ed3\u6784<\/strong><\/h2>\n<p>def unet_model(input_size=(128, 128, 3)):<\/p>\n<p>    inputs = Input(input_size)<\/p>\n<p>    conv1 = Conv2D(64, 3, activation=&#39;relu&#39;, padding=&#39;same&#39;)(inputs)<\/p>\n<p>    conv1 = Conv2D(64, 3, activation=&#39;relu&#39;, padding=&#39;same&#39;)(conv1)<\/p>\n<p>    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)<\/p>\n<p>    conv2 = Conv2D(128, 3, activation=&#39;relu&#39;, padding=&#39;same&#39;)(pool1)<\/p>\n<p>    conv2 = Conv2D(128, 3, activation=&#39;relu&#39;, padding=&#39;same&#39;)(conv2)<\/p>\n<p>    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)<\/p>\n<p>    conv3 = Conv2D(256, 3, activation=&#39;relu&#39;, padding=&#39;same&#39;)(pool2)<\/p>\n<p>    conv3 = Conv2D(256, 3, activation=&#39;relu&#39;, padding=&#39;same&#39;)(conv3)<\/p>\n<p>    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)<\/p>\n<p>    conv4 = Conv2D(512, 3, activation=&#39;relu&#39;, padding=&#39;same&#39;)(pool3)<\/p>\n<p>    conv4 = Conv2D(512, 3, activation=&#39;relu&#39;, padding=&#39;same&#39;)(conv4)<\/p>\n<p>    pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)<\/p>\n<p>    conv5 = Conv2D(1024, 3, activation=&#39;relu&#39;, padding=&#39;same&#39;)(pool4)<\/p>\n<p>    conv5 = Conv2D(1024, 3, activation=&#39;relu&#39;, padding=&#39;same&#39;)(conv5)<\/p>\n<p>    up6 = concatenate([UpSampling2D(size=(2, 2))(conv5), conv4], axis=3)<\/p>\n<p>    conv6 = Conv2D(512, 3, activation=&#39;relu&#39;, padding=&#39;same&#39;)(up6)<\/p>\n<p>    conv6 = Conv2D(512, 3, activation=&#39;relu&#39;, padding=&#39;same&#39;)(conv6)<\/p>\n<p>    up7 = concatenate([UpSampling2D(size=(2, 2))(conv6), conv3], axis=3)<\/p>\n<p>    conv7 = Conv2D(256, 3, activation=&#39;relu&#39;, padding=&#39;same&#39;)(up7)<\/p>\n<p>    conv7 = Conv2D(256, 3, activation=&#39;relu&#39;, padding=&#39;same&#39;)(conv7)<\/p>\n<p>    up8 = concatenate([UpSampling2D(size=(2, 2))(conv7), conv2], axis=3)<\/p>\n<p>    conv8 = Conv2D(128, 3, activation=&#39;relu&#39;, padding=&#39;same&#39;)(up8)<\/p>\n<p>    conv8 = Conv2D(128, 3, activation=&#39;relu&#39;, padding=&#39;same&#39;)(conv8)<\/p>\n<p>    up9 = concatenate([UpSampling2D(size=(2, 2))(conv8), conv1], axis=3)<\/p>\n<p>    conv9 = Conv2D(64, 3, activation=&#39;relu&#39;, padding=&#39;same&#39;)(up9)<\/p>\n<p>    conv9 = Conv2D(64, 3, activation=&#39;relu&#39;, padding=&#39;same&#39;)(conv9)<\/p>\n<p>    conv10 = Conv2D(1, 1, activation=&#39;sigmoid&#39;)(conv9)<\/p>\n<p>    model = Model(inputs=[inputs], outputs=[conv10])<\/p>\n<p>    return model<\/p>\n<h2><strong>\u521b\u5efa\u6a21\u578b<\/strong><\/h2>\n<p>model = unet_model()<\/p>\n<p>model.compile(optimizer=&#39;adam&#39;, loss=&#39;binary_crossentropy&#39;, metrics=[&#39;accuracy&#39;])<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b\uff08\u793a\u4f8b\u4ee3\u7801\uff0c\u5b9e\u9645\u9700\u8981\u51c6\u5907\u8bad\u7ec3\u6570\u636e\u96c6\uff09<\/strong><\/h2>\n<h2><strong>model.fit(x_train, y_train, batch_size=32, epochs=10, validation_split=0.2)<\/strong><\/h2>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;image.jpg&#39;)<\/p>\n<p>image_resized = cv2.resize(image, (128, 128))<\/p>\n<h2><strong>\u8fdb\u884c\u9884\u6d4b<\/strong><\/h2>\n<p>pred_mask = model.predict(np.expand_dims(image_resized, axis=0))<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Predicted Mask&#39;, pred_mask[0, :, :, 0])<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5f62\u6001\u5b66\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5f62\u6001\u5b66\u64cd\u4f5c\u662f\u57fa\u4e8e\u56fe\u50cf\u5f62\u72b6\u7684\u4e00\u79cd\u5904\u7406\u65b9\u6cd5\uff0c\u5e38\u7528\u4e8e\u56fe\u50cf\u9884\u5904\u7406\u548c\u540e\u5904\u7406\u3002\u5e38\u89c1\u7684\u5f62\u6001\u5b66\u64cd\u4f5c\u5305\u62ec\u8150\u8680\u3001\u81a8\u80c0\u3001\u5f00\u8fd0\u7b97\u3001\u95ed\u8fd0\u7b97\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8150\u8680\u548c\u81a8\u80c0<\/h4>\n<\/p>\n<p><p>\u8150\u8680\u548c\u81a8\u80c0\u662f\u6700\u57fa\u672c\u7684\u5f62\u6001\u5b66\u64cd\u4f5c\uff0c\u7528\u4e8e\u53bb\u9664\u566a\u58f0\u548c\u586b\u5145\u56fe\u50cf\u4e2d\u7684\u5c0f\u5b54\u6d1e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;image.jpg&#39;, 0)<\/p>\n<h2><strong>\u5b9a\u4e49\u6838<\/strong><\/h2>\n<p>kernel = np.ones((5, 5), np.uint8)<\/p>\n<h2><strong>\u5e94\u7528\u8150\u8680<\/strong><\/h2>\n<p>erosion = cv2.erode(image, kernel, iterations=1)<\/p>\n<h2><strong>\u5e94\u7528\u81a8\u80c0<\/strong><\/h2>\n<p>dilation = cv2.dilate(image, kernel, iterations=1)<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Erosion&#39;, erosion)<\/p>\n<p>cv2.imshow(&#39;Dilation&#39;, dilation)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5f00\u8fd0\u7b97\u548c\u95ed\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p>\u5f00\u8fd0\u7b97\u662f\u5148\u8150\u8680\u540e\u81a8\u80c0\uff0c\u7528\u4e8e\u53bb\u9664\u5c0f\u7269\u4f53\uff1b\u95ed\u8fd0\u7b97\u662f\u5148\u81a8\u80c0\u540e\u8150\u8680\uff0c\u7528\u4e8e\u586b\u5145\u5c0f\u5b54\u6d1e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;image.jpg&#39;, 0)<\/p>\n<h2><strong>\u5b9a\u4e49\u6838<\/strong><\/h2>\n<p>kernel = np.ones((5, 5), np.uint8)<\/p>\n<h2><strong>\u5e94\u7528\u5f00\u8fd0\u7b97<\/strong><\/h2>\n<p>opening = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)<\/p>\n<h2><strong>\u5e94\u7528\u95ed\u8fd0\u7b97<\/strong><\/h2>\n<p>closing = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Opening&#39;, opening)<\/p>\n<p>cv2.imshow(&#39;Closing&#39;, closing)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u5bf9\u56fe\u50cf\u8fdb\u884c\u5207\u5272\u4ee5\u63d0\u53d6\u91cd\u8981\u90e8\u5206\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u56fe\u50cf\u5206\u5272\u7b97\u6cd5\u3001\u8fb9\u7f18\u68c0\u6d4b\u3001\u989c\u8272\u7a7a\u95f4\u8f6c\u6362\u3001\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u548c\u5f62\u6001\u5b66\u64cd\u4f5c\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u56fe\u50cf\u7684\u6709\u6548\u5207\u5272\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u5408\u7406\u7684\u9884\u5904\u7406\u548c\u540e\u5904\u7406\u90fd\u662f\u4fdd\u8bc1\u5207\u5272\u8d28\u91cf\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u6700\u4f73\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5e93\u6765\u5207\u5272\u56fe\u50cf\u4e2d\u7684\u91cd\u8981\u90e8\u5206\uff1f<\/strong><br \/>\u4f7f\u7528Python\u8fdb\u884c\u56fe\u50cf\u5207\u5272\u65f6\uff0c\u53ef\u4ee5\u5229\u7528OpenCV\u3001PIL\uff08Pillow\uff09\u7b49\u5e93\u3002OpenCV\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u901a\u8fc7\u56fe\u50cf\u7684\u5750\u6807\u6765\u5b9a\u4e49\u8981\u5207\u5272\u7684\u533a\u57df\u3002\u800cPIL\u5219\u9002\u5408\u8fdb\u884c\u7b80\u5355\u7684\u56fe\u50cf\u5904\u7406\u548c\u5207\u5272\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u540e\uff0c\u53ef\u4ee5\u52a0\u8f7d\u56fe\u50cf\u3001\u6307\u5b9a\u5207\u5272\u533a\u57df\u7684\u5750\u6807\uff0c\u5e76\u4f7f\u7528\u76f8\u5e94\u7684\u51fd\u6570\u8fdb\u884c\u5207\u5272\u3002<\/p>\n<p><strong>\u5207\u5272\u56fe\u50cf\u65f6\u5982\u4f55\u786e\u5b9a\u91cd\u8981\u90e8\u5206\u7684\u533a\u57df\uff1f<\/strong><br \/>\u786e\u5b9a\u91cd\u8981\u90e8\u5206\u901a\u5e38\u4f9d\u8d56\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u3002\u53ef\u4ee5\u4f7f\u7528\u56fe\u50cf\u5904\u7406\u6280\u672f\uff0c\u5982\u8fb9\u7f18\u68c0\u6d4b\u3001\u8f6e\u5ed3\u63d0\u53d6\u6216\u57fa\u4e8e\u989c\u8272\u7684\u5206\u5272\u65b9\u6cd5\uff0c\u6765\u8bc6\u522b\u56fe\u50cf\u4e2d\u7684\u5173\u952e\u533a\u57df\u3002\u6b64\u5916\uff0c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6a21\u578b\u4e5f\u53ef\u4ee5\u88ab\u8bad\u7ec3\u6765\u8bc6\u522b\u7279\u5b9a\u7684\u5bf9\u8c61\u6216\u533a\u57df\uff0c\u4ece\u800c\u81ea\u52a8\u5316\u5207\u5272\u8fc7\u7a0b\u3002<\/p>\n<p><strong>\u5728\u5207\u5272\u56fe\u50cf\u65f6\uff0c\u5982\u4f55\u5904\u7406\u5207\u5272\u540e\u7684\u7ed3\u679c\uff1f<\/strong><br \/>\u5207\u5272\u540e\u7684\u7ed3\u679c\u53ef\u4ee5\u4fdd\u5b58\u4e3a\u65b0\u56fe\u50cf\u6587\u4ef6\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\u3002\u53ef\u4ee5\u4f7f\u7528PIL\u7684save\u65b9\u6cd5\u6216\u8005OpenCV\u7684imwrite\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u6b64\u5916\uff0c\u5904\u7406\u540e\u7684\u56fe\u50cf\u53ef\u4ee5\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5206\u6790\u6216\u64cd\u4f5c\uff0c\u5982\u7279\u5f81\u63d0\u53d6\u3001\u56fe\u50cf\u589e\u5f3a\u7b49\uff0c\u4ee5\u63d0\u5347\u540e\u7eed\u5e94\u7528\u7684\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5bf9\u56fe\u50cf\u8fdb\u884c\u5207\u5272\u4ee5\u63d0\u53d6\u91cd\u8981\u90e8\u5206\u7684\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u56fe\u50cf\u5206\u5272\u7b97\u6cd5\u3001\u8fb9\u7f18\u68c0\u6d4b\u3001\u989c\u8272\u7a7a\u95f4\u8f6c\u6362\u3001\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u3001\u5f62\u6001 [&hellip;]","protected":false},"author":3,"featured_media":1122888,"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\/1122886"}],"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=1122886"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1122886\/revisions"}],"predecessor-version":[{"id":1122889,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1122886\/revisions\/1122889"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1122888"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1122886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1122886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1122886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}