FCN实现图像分割代码
时间: 2023-10-26 19:09:43 浏览: 98
以下是使用FCN进行图像分割的Python代码示例:
```python
import numpy as np
import tensorflow as tf
import cv2
# Load the pre-trained FCN model
model = tf.keras.models.load_model('fcn_model.h5')
# Load the image to be segmented
img = cv2.imread('input_image.jpg')
img = cv2.resize(img, (256, 256)) # Resize to match the input size of the FCN model
# Normalize the image
img = img / 255.0
# Get the predicted segmentation mask
mask = model.predict(np.array([img]))
# Convert the mask to a binary image
mask = (mask > 0.5).astype(np.uint8)
# Resize the mask to match the original image size
mask = cv2.resize(mask[0], (img.shape[1], img.shape[0]))
# Display the original image and the segmented mask
cv2.imshow('Input Image', img)
cv2.imshow('Segmented Mask', mask)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
需要注意的是,此代码需要预先训练好的FCN模型文件(.h5)才能运行。
阅读全文