{"id":1049918,"date":"2024-12-31T14:03:18","date_gmt":"2024-12-31T06:03:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1049918.html"},"modified":"2024-12-31T14:03:20","modified_gmt":"2024-12-31T06:03:20","slug":"python%e5%a4%8d%e6%9d%82%e8%83%8c%e6%99%af%e5%a6%82%e4%bd%95%e5%88%86%e5%89%b2%e5%87%ba%e9%9d%99%e6%80%81%e6%89%8b%e5%8a%bf","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1049918.html","title":{"rendered":"python\u590d\u6742\u80cc\u666f\u5982\u4f55\u5206\u5272\u51fa\u9759\u6001\u624b\u52bf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/ec3c51ad-7f5b-487f-9653-5d15ed0ed7e5.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u590d\u6742\u80cc\u666f\u5982\u4f55\u5206\u5272\u51fa\u9759\u6001\u624b\u52bf\" \/><\/p>\n<h2><strong>Python\u590d\u6742\u80cc\u666f\u5982\u4f55\u5206\u5272\u51fa\u9759\u6001\u624b\u52bf<\/strong><\/h2>\n<p><p>\u5728\u590d\u6742\u80cc\u666f\u4e2d\u5206\u5272\u51fa\u9759\u6001\u624b\u52bf\u662f\u8ba1\u7b97\u673a\u89c6\u89c9\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u8bfe\u9898\uff0c<strong>\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u989c\u8272\u7a7a\u95f4\u8f6c\u6362\u3001\u80cc\u666f\u51cf\u9664\u3001\u8fb9\u7f18\u68c0\u6d4b\u3001\u6df1\u5ea6\u5b66\u4e60<\/strong>\u7b49\u3002\u989c\u8272\u7a7a\u95f4\u8f6c\u6362\u548c\u80cc\u666f\u51cf\u9664\u662f\u8f83\u4e3a\u57fa\u7840\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u573a\u666f\u3002\u800c\u6df1\u5ea6\u5b66\u4e60\u65b9\u6cd5\u5219\u66f4\u4e3a\u5f3a\u5927\u548c\u7075\u6d3b\uff0c\u80fd\u591f\u5904\u7406\u590d\u6742\u7684\u80cc\u666f\u548c\u591a\u6837\u5316\u7684\u624b\u52bf\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5b9e\u7528\u7684\u4ee3\u7801\u793a\u4f8b\u548c\u5177\u4f53\u5efa\u8bae\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u989c\u8272\u7a7a\u95f4\u8f6c\u6362<\/h2>\n<\/p>\n<p><p>\u989c\u8272\u7a7a\u95f4\u8f6c\u6362\u662f\u624b\u52bf\u5206\u5272\u4e2d\u7684\u5e38\u7528\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u5c06\u56fe\u50cf\u4eceRGB\u989c\u8272\u7a7a\u95f4\u8f6c\u6362\u5230\u5176\u4ed6\u989c\u8272\u7a7a\u95f4\uff0c\u5982HSV\u6216YCrCb\uff0c\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u5206\u5272\u51fa\u624b\u52bf\u3002<\/p>\n<\/p>\n<p><h3>1.1 HSV\u989c\u8272\u7a7a\u95f4<\/h3>\n<\/p>\n<p><p>HSV\u989c\u8272\u7a7a\u95f4\u5c06\u989c\u8272\u8868\u793a\u4e3a\u8272\u8c03\uff08Hue\uff09\u3001\u9971\u548c\u5ea6\uff08Saturation\uff09\u548c\u4eae\u5ea6\uff08Value\uff09\u3002\u5728HSV\u989c\u8272\u7a7a\u95f4\u4e2d\uff0c\u624b\u52bf\u7684\u989c\u8272\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u4e0e\u80cc\u666f\u5206\u79bb\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;hand.jpg&#39;)<\/p>\n<h2><strong>\u8f6c\u6362\u5230HSV\u989c\u8272\u7a7a\u95f4<\/strong><\/h2>\n<p>hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)<\/p>\n<h2><strong>\u5b9a\u4e49\u624b\u52bf\u7684HSV\u8303\u56f4<\/strong><\/h2>\n<p>lower_skin = np.array([0, 20, 70], dtype=np.uint8)<\/p>\n<p>upper_skin = np.array([20, 255, 255], dtype=np.uint8)<\/p>\n<h2><strong>\u521b\u5efa\u906e\u7f69<\/strong><\/h2>\n<p>mask = cv2.inRange(hsv, lower_skin, upper_skin)<\/p>\n<h2><strong>\u5e94\u7528\u906e\u7f69<\/strong><\/h2>\n<p>res = cv2.bitwise_and(image, image, mask=mask)<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Original&#39;, image)<\/p>\n<p>cv2.imshow(&#39;Mask&#39;, mask)<\/p>\n<p>cv2.imshow(&#39;Result&#39;, res)<\/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><h3>1.2 YCrCb\u989c\u8272\u7a7a\u95f4<\/h3>\n<\/p>\n<p><p>YCrCb\u989c\u8272\u7a7a\u95f4\u5c06\u989c\u8272\u8868\u793a\u4e3a\u4eae\u5ea6\uff08Y\uff09\u3001\u84dd\u8272\u8272\u5dee\uff08Cr\uff09\u548c\u7ea2\u8272\u8272\u5dee\uff08Cb\uff09\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0cYCrCb\u989c\u8272\u7a7a\u95f4\u53ef\u4ee5\u66f4\u597d\u5730\u5206\u5272\u624b\u52bf\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;hand.jpg&#39;)<\/p>\n<h2><strong>\u8f6c\u6362\u5230YCrCb\u989c\u8272\u7a7a\u95f4<\/strong><\/h2>\n<p>ycrcb = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)<\/p>\n<h2><strong>\u5b9a\u4e49\u624b\u52bf\u7684YCrCb\u8303\u56f4<\/strong><\/h2>\n<p>lower_skin = np.array([0, 133, 77], dtype=np.uint8)<\/p>\n<p>upper_skin = np.array([255, 173, 127], dtype=np.uint8)<\/p>\n<h2><strong>\u521b\u5efa\u906e\u7f69<\/strong><\/h2>\n<p>mask = cv2.inRange(ycrcb, lower_skin, upper_skin)<\/p>\n<h2><strong>\u5e94\u7528\u906e\u7f69<\/strong><\/h2>\n<p>res = cv2.bitwise_and(image, image, mask=mask)<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Original&#39;, image)<\/p>\n<p>cv2.imshow(&#39;Mask&#39;, mask)<\/p>\n<p>cv2.imshow(&#39;Result&#39;, res)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u80cc\u666f\u51cf\u9664<\/h2>\n<\/p>\n<p><p>\u80cc\u666f\u51cf\u9664\u662f\u4e00\u79cd\u5728\u89c6\u9891\u5904\u7406\u4e2d\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u51cf\u53bb\u80cc\u666f\uff0c\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u5206\u5272\u51fa\u524d\u666f\u4e2d\u7684\u624b\u52bf\u3002<\/p>\n<\/p>\n<p><h3>2.1 \u57fa\u4e8e\u5e27\u5dee\u6cd5\u7684\u80cc\u666f\u51cf\u9664<\/h3>\n<\/p>\n<p><p>\u5e27\u5dee\u6cd5\u662f\u4e00\u79cd\u7b80\u5355\u7684\u80cc\u666f\u51cf\u9664\u65b9\u6cd5\uff0c\u901a\u8fc7\u6bd4\u8f83\u8fde\u7eed\u5e27\u4e4b\u95f4\u7684\u5dee\u5f02\u6765\u68c0\u6d4b\u524d\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u6253\u5f00\u89c6\u9891\u6355\u83b7<\/strong><\/h2>\n<p>cap = cv2.VideoCapture(&#39;hand_video.mp4&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u7b2c\u4e00\u5e27<\/strong><\/h2>\n<p>ret, frame1 = cap.read()<\/p>\n<p>gray1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)<\/p>\n<p>gray1 = cv2.GaussianBlur(gray1, (21, 21), 0)<\/p>\n<p>while cap.isOpened():<\/p>\n<p>    ret, frame2 = cap.read()<\/p>\n<p>    if not ret:<\/p>\n<p>        break<\/p>\n<p>    # \u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u5e76\u8fdb\u884c\u9ad8\u65af\u6a21\u7cca<\/p>\n<p>    gray2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)<\/p>\n<p>    gray2 = cv2.GaussianBlur(gray2, (21, 21), 0)<\/p>\n<p>    # \u8ba1\u7b97\u5e27\u5dee<\/p>\n<p>    frame_diff = cv2.absdiff(gray1, gray2)<\/p>\n<p>    # \u9608\u503c\u5316<\/p>\n<p>    _, thresh = cv2.threshold(frame_diff, 25, 255, cv2.THRESH_BINARY)<\/p>\n<p>    # \u663e\u793a\u7ed3\u679c<\/p>\n<p>    cv2.imshow(&#39;Frame Difference&#39;, thresh)<\/p>\n<p>    # \u66f4\u65b0\u4e0a\u4e00\u5e27<\/p>\n<p>    gray1 = gray2.copy()<\/p>\n<p>    if cv2.waitKey(30) &amp; 0xFF == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<p>cap.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.2 \u57fa\u4e8e\u80cc\u666f\u5efa\u6a21\u7684\u80cc\u666f\u51cf\u9664<\/h3>\n<\/p>\n<p><p>\u80cc\u666f\u5efa\u6a21\u662f\u4e00\u79cd\u66f4\u4e3a\u590d\u6742\u7684\u80cc\u666f\u51cf\u9664\u65b9\u6cd5\uff0c\u901a\u8fc7\u5efa\u6a21\u80cc\u666f\u6765\u68c0\u6d4b\u524d\u666f\u4e2d\u7684\u624b\u52bf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u521b\u5efa\u80cc\u666f\u51cf\u9664\u5668<\/strong><\/h2>\n<p>bg_subtractor = cv2.createBackgroundSubtractorMOG2(history=100, varThreshold=50)<\/p>\n<h2><strong>\u6253\u5f00\u89c6\u9891\u6355\u83b7<\/strong><\/h2>\n<p>cap = cv2.VideoCapture(&#39;hand_video.mp4&#39;)<\/p>\n<p>while cap.isOpened():<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    if not ret:<\/p>\n<p>        break<\/p>\n<p>    # \u5e94\u7528\u80cc\u666f\u51cf\u9664<\/p>\n<p>    fg_mask = bg_subtractor.apply(frame)<\/p>\n<p>    # \u663e\u793a\u7ed3\u679c<\/p>\n<p>    cv2.imshow(&#39;Foreground Mask&#39;, fg_mask)<\/p>\n<p>    if cv2.waitKey(30) &amp; 0xFF == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<p>cap.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u8fb9\u7f18\u68c0\u6d4b<\/h2>\n<\/p>\n<p><p>\u8fb9\u7f18\u68c0\u6d4b\u662f\u4e00\u79cd\u68c0\u6d4b\u56fe\u50cf\u4e2d\u663e\u8457\u53d8\u5316\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u5206\u5272\u624b\u52bf\u3002<\/p>\n<\/p>\n<p><h3>3.1 Canny\u8fb9\u7f18\u68c0\u6d4b<\/h3>\n<\/p>\n<p><p>Canny\u8fb9\u7f18\u68c0\u6d4b\u662f\u4e00\u79cd\u5e38\u7528\u7684\u8fb9\u7f18\u68c0\u6d4b\u7b97\u6cd5\uff0c\u901a\u8fc7\u68c0\u6d4b\u56fe\u50cf\u4e2d\u7684\u68af\u5ea6\u53d8\u5316\u6765\u8bc6\u522b\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;hand.jpg&#39;, cv2.IMREAD_GRAYSCALE)<\/p>\n<h2><strong>\u5e94\u7528\u9ad8\u65af\u6a21\u7cca<\/strong><\/h2>\n<p>blurred = cv2.GaussianBlur(image, (5, 5), 0)<\/p>\n<h2><strong>\u5e94\u7528Canny\u8fb9\u7f18\u68c0\u6d4b<\/strong><\/h2>\n<p>edges = cv2.Canny(blurred, 50, 150)<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Edges&#39;, edges)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2 Sobel\u8fb9\u7f18\u68c0\u6d4b<\/h3>\n<\/p>\n<p><p>Sobel\u8fb9\u7f18\u68c0\u6d4b\u662f\u4e00\u79cd\u57fa\u4e8e\u68af\u5ea6\u7684\u8fb9\u7f18\u68c0\u6d4b\u7b97\u6cd5\uff0c\u901a\u8fc7\u8ba1\u7b97\u56fe\u50cf\u7684\u68af\u5ea6\u6765\u8bc6\u522b\u8fb9\u7f18\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;hand.jpg&#39;, cv2.IMREAD_GRAYSCALE)<\/p>\n<h2><strong>\u8ba1\u7b97Sobel\u68af\u5ea6<\/strong><\/h2>\n<p>grad_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=5)<\/p>\n<p>grad_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=5)<\/p>\n<h2><strong>\u8ba1\u7b97\u68af\u5ea6\u5e45\u503c<\/strong><\/h2>\n<p>magnitude = cv2.magnitude(grad_x, grad_y)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3auint8\u7c7b\u578b<\/strong><\/h2>\n<p>magnitude = np.uint8(magnitude)<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Sobel Edges&#39;, magnitude)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u6df1\u5ea6\u5b66\u4e60<\/h2>\n<\/p>\n<p><p>\u6df1\u5ea6\u5b66\u4e60\u65b9\u6cd5\u5728\u624b\u52bf\u5206\u5272\u4e2d\u8868\u73b0\u51fa\u8272\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u590d\u6742\u80cc\u666f\u548c\u591a\u6837\u5316\u624b\u52bf\u65f6\u3002<\/p>\n<\/p>\n<p><h3>4.1 \u4f7f\u7528\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\uff08CNN\uff09<\/h3>\n<\/p>\n<p><p>\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\uff08CNN\uff09\u662f\u4e00\u79cd\u5e38\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u7684\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\uff0c\u53ef\u4ee5\u7528\u4e8e\u624b\u52bf\u5206\u5272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>from tensorflow.keras.models import Sequential<\/p>\n<p>from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense<\/p>\n<h2><strong>\u521b\u5efa\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\u6a21\u578b<\/strong><\/h2>\n<p>model = Sequential([<\/p>\n<p>    Conv2D(32, (3, 3), activation=&#39;relu&#39;, input_shape=(128, 128, 3)),<\/p>\n<p>    MaxPooling2D(pool_size=(2, 2)),<\/p>\n<p>    Conv2D(64, (3, 3), activation=&#39;relu&#39;),<\/p>\n<p>    MaxPooling2D(pool_size=(2, 2)),<\/p>\n<p>    Flatten(),<\/p>\n<p>    Dense(128, activation=&#39;relu&#39;),<\/p>\n<p>    Dense(1, activation=&#39;sigmoid&#39;)<\/p>\n<p>])<\/p>\n<h2><strong>\u7f16\u8bd1\u6a21\u578b<\/strong><\/h2>\n<p>model.compile(optimizer=&#39;adam&#39;, loss=&#39;binary_crossentropy&#39;, metrics=[&#39;accuracy&#39;])<\/p>\n<h2><strong>\u6253\u5370\u6a21\u578b\u6458\u8981<\/strong><\/h2>\n<p>model.summary()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.2 \u4f7f\u7528U-Net\u7f51\u7edc<\/h3>\n<\/p>\n<p><p>U-Net\u662f\u4e00\u79cd\u5e38\u7528\u4e8e\u56fe\u50cf\u5206\u5272\u7684\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\uff0c\u53ef\u4ee5\u7528\u4e8e\u624b\u52bf\u5206\u5272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate<\/p>\n<p>from tensorflow.keras.models import Model<\/p>\n<h2><strong>\u5b9a\u4e49U-Net\u6a21\u578b<\/strong><\/h2>\n<p>inputs = Input((128, 128, 3))<\/p>\n<p>conv1 = Conv2D(64, (3, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;)(inputs)<\/p>\n<p>conv1 = Conv2D(64, (3, 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, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;)(pool1)<\/p>\n<p>conv2 = Conv2D(128, (3, 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, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;)(pool2)<\/p>\n<p>conv3 = Conv2D(256, (3, 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, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;)(pool3)<\/p>\n<p>conv4 = Conv2D(512, (3, 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, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;)(pool4)<\/p>\n<p>conv5 = Conv2D(1024, (3, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;)(conv5)<\/p>\n<p>up6 = concatenate([UpSampling2D(size=(2, 2))(conv5), conv4], axis=-1)<\/p>\n<p>conv6 = Conv2D(512, (3, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;)(up6)<\/p>\n<p>conv6 = Conv2D(512, (3, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;)(conv6)<\/p>\n<p>up7 = concatenate([UpSampling2D(size=(2, 2))(conv6), conv3], axis=-1)<\/p>\n<p>conv7 = Conv2D(256, (3, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;)(up7)<\/p>\n<p>conv7 = Conv2D(256, (3, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;)(conv7)<\/p>\n<p>up8 = concatenate([UpSampling2D(size=(2, 2))(conv7), conv2], axis=-1)<\/p>\n<p>conv8 = Conv2D(128, (3, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;)(up8)<\/p>\n<p>conv8 = Conv2D(128, (3, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;)(conv8)<\/p>\n<p>up9 = concatenate([UpSampling2D(size=(2, 2))(conv8), conv1], axis=-1)<\/p>\n<p>conv9 = Conv2D(64, (3, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;)(up9)<\/p>\n<p>conv9 = Conv2D(64, (3, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;)(conv9)<\/p>\n<p>outputs = Conv2D(1, (1, 1), activation=&#39;sigmoid&#39;)(conv9)<\/p>\n<p>model = Model(inputs=[inputs], outputs=[outputs])<\/p>\n<h2><strong>\u7f16\u8bd1\u6a21\u578b<\/strong><\/h2>\n<p>model.compile(optimizer=&#39;adam&#39;, loss=&#39;binary_crossentropy&#39;, metrics=[&#39;accuracy&#39;])<\/p>\n<h2><strong>\u6253\u5370\u6a21\u578b\u6458\u8981<\/strong><\/h2>\n<p>model.summary()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u540e\u5904\u7406\u548c\u4f18\u5316<\/h2>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u624b\u52bf\u5206\u5272\u540e\uff0c\u901a\u5e38\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u540e\u5904\u7406\u548c\u4f18\u5316\u6b65\u9aa4\uff0c\u4ee5\u63d0\u9ad8\u5206\u5272\u7ed3\u679c\u7684\u8d28\u91cf\u3002<\/p>\n<\/p>\n<p><h3>5.1 \u5f62\u6001\u5b66\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5f62\u6001\u5b66\u64cd\u4f5c\u662f\u4e00\u79cd\u56fe\u50cf\u5904\u7406\u6280\u672f\uff0c\u53ef\u4ee5\u7528\u4e8e\u53bb\u9664\u566a\u58f0\u548c\u586b\u8865\u7a7a\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\u4e8c\u503c\u56fe\u50cf<\/strong><\/h2>\n<p>binary_image = cv2.imread(&#39;binary_image.jpg&#39;, cv2.IMREAD_GRAYSCALE)<\/p>\n<h2><strong>\u5b9a\u4e49\u5f62\u6001\u5b66\u5185\u6838<\/strong><\/h2>\n<p>kernel = np.ones((5, 5), np.uint8)<\/p>\n<h2><strong>\u5e94\u7528\u5f62\u6001\u5b66\u64cd\u4f5c<\/strong><\/h2>\n<p>opening = cv2.morphologyEx(binary_image, cv2.MORPH_OPEN, kernel)<\/p>\n<p>closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel)<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Original&#39;, binary_image)<\/p>\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>5.2 \u8f6e\u5ed3\u68c0\u6d4b<\/h3>\n<\/p>\n<p><p>\u8f6e\u5ed3\u68c0\u6d4b\u662f\u4e00\u79cd\u7528\u4e8e\u8bc6\u522b\u548c\u5206\u6790\u56fe\u50cf\u4e2d\u5f62\u72b6\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u5206\u5272\u540e\u7684\u624b\u52bf\u68c0\u6d4b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u4e8c\u503c\u56fe\u50cf<\/strong><\/h2>\n<p>binary_image = cv2.imread(&#39;binary_image.jpg&#39;, cv2.IMREAD_GRAYSCALE)<\/p>\n<h2><strong>\u68c0\u6d4b\u8f6e\u5ed3<\/strong><\/h2>\n<p>contours, _ = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)<\/p>\n<h2><strong>\u7ed8\u5236\u8f6e\u5ed3<\/strong><\/h2>\n<p>image = cv2.cvtColor(binary_image, cv2.COLOR_GRAY2BGR)<\/p>\n<p>cv2.drawContours(image, contours, -1, (0, 255, 0), 2)<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Contours&#39;, image)<\/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\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u590d\u6742\u80cc\u666f\u4e2d\u6210\u529f\u5206\u5272\u51fa\u9759\u6001\u624b\u52bf\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\uff0c\u5e76\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u8c03\u6574\u548c\u4f18\u5316\uff0c\u4ee5\u83b7\u5f97\u6700\u4f73\u7684\u5206\u5272\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728\u590d\u6742\u80cc\u666f\u4e0b\uff0c\u5982\u4f55\u63d0\u9ad8\u9759\u6001\u624b\u52bf\u8bc6\u522b\u7684\u51c6\u786e\u6027\uff1f<\/strong><br \/>\u63d0\u9ad8\u9759\u6001\u624b\u52bf\u8bc6\u522b\u7684\u51c6\u786e\u6027\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u9009\u62e9\u5408\u9002\u7684<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6a21\u578b\u975e\u5e38\u91cd\u8981\uff0c\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u5982\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\uff08CNN\uff09\u5728\u56fe\u50cf\u8bc6\u522b\u65b9\u9762\u8868\u73b0\u4f18\u5f02\u3002\u5176\u6b21\uff0c\u53ef\u4ee5\u901a\u8fc7\u6570\u636e\u589e\u5f3a\u6280\u672f\u6765\u6269\u5145\u8bad\u7ec3\u6570\u636e\u96c6\uff0c\u4ee5\u4fbf\u6a21\u578b\u80fd\u591f\u66f4\u597d\u5730\u9002\u5e94\u4e0d\u540c\u7684\u80cc\u666f\u548c\u624b\u52bf\u53d8\u5316\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u56fe\u50cf\u9884\u5904\u7406\u6280\u672f\uff0c\u5982\u8fb9\u7f18\u68c0\u6d4b\u6216\u989c\u8272\u7a7a\u95f4\u8f6c\u6362\uff0c\u4e5f\u6709\u52a9\u4e8e\u7a81\u51fa\u624b\u52bf\u7279\u5f81\uff0c\u51cf\u5c11\u80cc\u666f\u5e72\u6270\u3002<\/p>\n<p><strong>\u5728\u590d\u6742\u80cc\u666f\u4e2d\uff0c\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u56fe\u50cf\u5904\u7406\u6280\u672f\uff1f<\/strong><br \/>\u9009\u62e9\u5408\u9002\u7684\u56fe\u50cf\u5904\u7406\u6280\u672f\u901a\u5e38\u53d6\u51b3\u4e8e\u5177\u4f53\u5e94\u7528\u573a\u666f\u548c\u624b\u52bf\u7279\u5f81\u3002\u5e38\u89c1\u7684\u6280\u672f\u5305\u62ec\u80cc\u666f\u51cf\u9664\u3001\u8272\u5f69\u5206\u5272\u548c\u5f62\u6001\u5b66\u64cd\u4f5c\u3002\u80cc\u666f\u51cf\u9664\u53ef\u4ee5\u6709\u6548\u6d88\u9664\u9759\u6001\u80cc\u666f\u5bf9\u624b\u52bf\u8bc6\u522b\u7684\u5f71\u54cd\uff0c\u800c\u8272\u5f69\u5206\u5272\u5219\u9002\u7528\u4e8e\u624b\u52bf\u989c\u8272\u660e\u663e\u4e0e\u80cc\u666f\u8272\u5dee\u522b\u8f83\u5927\u7684\u60c5\u51b5\u3002\u5f62\u6001\u5b66\u64cd\u4f5c\uff0c\u5982\u81a8\u80c0\u548c\u8150\u8680\uff0c\u53ef\u4ee5\u5e2e\u52a9\u63d0\u53d6\u624b\u52bf\u7684\u5f62\u72b6\u7279\u5f81\uff0c\u63d0\u5347\u540e\u7eed\u8bc6\u522b\u7684\u6548\u679c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5229\u7528Python\u5e93\u5b9e\u73b0\u590d\u6742\u80cc\u666f\u4e0b\u7684\u624b\u52bf\u5206\u5272\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528OpenCV\u548cTensorFlow\u7b49\u5e93\u6765\u5b9e\u73b0\u590d\u6742\u80cc\u666f\u4e0b\u7684\u624b\u52bf\u5206\u5272\u3002\u4f7f\u7528OpenCV\uff0c\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u5982<code>cv2.threshold()<\/code>\u548c<code>cv2.findContours()<\/code>\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u548c\u624b\u52bf\u63d0\u53d6\u3002TensorFlow\u5219\u53ef\u4ee5\u7528\u6765\u6784\u5efa\u548c\u8bad\u7ec3\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\uff0c\u4ee5\u8fdb\u884c\u66f4\u9ad8\u7ea7\u7684\u624b\u52bf\u8bc6\u522b\u4efb\u52a1\u3002\u6b64\u5916\uff0c\u4f7f\u7528Keras\u7b49\u9ad8\u5c42API\u53ef\u4ee5\u7b80\u5316\u6a21\u578b\u7684\u6784\u5efa\u548c\u8bad\u7ec3\u8fc7\u7a0b\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u80fd\u591f\u5feb\u901f\u5b9e\u73b0\u624b\u52bf\u5206\u5272\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u590d\u6742\u80cc\u666f\u5982\u4f55\u5206\u5272\u51fa\u9759\u6001\u624b\u52bf \u5728\u590d\u6742\u80cc\u666f\u4e2d\u5206\u5272\u51fa\u9759\u6001\u624b\u52bf\u662f\u8ba1\u7b97\u673a\u89c6\u89c9\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u8bfe\u9898\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec [&hellip;]","protected":false},"author":3,"featured_media":1049930,"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\/1049918"}],"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=1049918"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1049918\/revisions"}],"predecessor-version":[{"id":1049932,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1049918\/revisions\/1049932"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1049930"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1049918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1049918"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1049918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}