python imshow彩色_Python:带有imshow的2D彩色地图

I am trying to represent a function of two variable on a two dimensional graph using color.

I came across this example here:

from numpy import exp,arange

from pylab import meshgrid,cm,imshow,contour,clabel,colorbar,axis,title,show

# the function that I'm going to plot

def z_func(x,y):

return (1-(x**2+y**3))*exp(-(x**2+y**2)/2)

x = arange(-3.0,3.0,0.1)

y = arange(-3.0,3.0,0.1)

X,Y = meshgrid(x, y) # grid of point

Z = z_func(X, Y) # evaluation of the function on the grid

im = imshow(Z,cmap=cm.RdBu) # drawing the function

# adding the Contour lines with labels

cset = contour(Z,arange(-1,1.5,0.2),linewidths=2,cmap=cm.Set2)

clabel(cset,inline=True,fmt='%1.1f',fontsize=10)

colorbar(im) # adding the colobar on the right

# latex fashion title

title('$z=(1-x^2+y^3) e^{-(x^2+y^2)/2}$')

show()

which produces

However, the axis scales and limits do not correspond to the real x and y data(both of which are between -3 and 3). How to make them correspond to the actual data?

解决方案

Add extent=(-3, 3, 3, -3) to the call to imshow and

extent=(-3, 3, -3, 3) (note the unfortunately tricky change in signs!) to the call to contour:

import numpy as np

import matplotlib.pyplot as plt

def z_func(x, y):

return (1 - (x ** 2 + y ** 3)) * np.exp(-(x ** 2 + y ** 2) / 2)

x = np.arange(-3.0, 3.0, 0.1)

y = np.arange(-3.0, 3.0, 0.1)

X, Y = np.meshgrid(x, y)

Z = z_func(X, Y)

im = plt.imshow(Z, cmap=plt.cm.RdBu, extent=(-3, 3, 3, -3))

cset = plt.contour(Z, np.arange(-1, 1.5, 0.2), linewidths=2,

cmap=plt.cm.Set2,

extent=(-3, 3, -3, 3))

plt.clabel(cset, inline=True, fmt='%1.1f', fontsize=10)

plt.colorbar(im)

plt.title('$z=(1-x^2+y^3) e^{-(x^2+y^2)/2}$')

plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在上一篇文章中,我们介绍了什么是语义分割以及语义分割的应用场景。本文将带领大家进一步了解如何用Python实现语义分割。 我们将使用Python中的OpenCV和深度学习框架Keras来实现语义分割。我们将训练一个卷积神经网络模型,该模型将使用图像作为输入,并将输出像素级别的标签。我们将使用PASCAL VOC 2012数据集来进行训练和测试。 首先,我们需要下载数据集。可以从以下链接下载: http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar 下载完成后,将其解压缩到本地文件夹中。我们将使用其中的训练集和验证集来训练和测试我们的模型。 接下来,我们需要安装所需的Python库。在终端窗口中运行以下命令: ``` pip install opencv-python numpy keras ``` 我们还需要下载一个预训练的VGG16模型,该模型的权重可以从以下链接下载: https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels.h5 下载完成后,将其保存到本地文件夹中。 现在,我们已经准备好开始实现语义分割了。首先,我们需要加载数据集。我们将使用PASCAL VOC 2012数据集中的图像和标签来训练我们的模型。以下是加载数据集的代码: ```python import os import cv2 import numpy as np # 加载训练集 def load_train_data(data_dir): # 加载图像和标签 images_dir = os.path.join(data_dir, 'JPEGImages') labels_dir = os.path.join(data_dir, 'SegmentationClass') image_file_names = os.listdir(images_dir) label_file_names = os.listdir(labels_dir) image_file_names.sort() label_file_names.sort() images = [] labels = [] for image_file_name, label_file_name in zip(image_file_names, label_file_names): if image_file_name[:-4] != label_file_name[:-4]: raise ValueError('Image and label file names do not match.') image_file_path = os.path.join(images_dir, image_file_name) label_file_path = os.path.join(labels_dir, label_file_name) image = cv2.imread(image_file_path) label = cv2.imread(label_file_path, cv2.IMREAD_GRAYSCALE) images.append(image) labels.append(label) return np.array(images), np.array(labels) # 加载验证集 def load_val_data(data_dir): # 加载图像和标签 images_dir = os.path.join(data_dir, 'JPEGImages') labels_dir = os.path.join(data_dir, 'SegmentationClass') image_file_names = os.listdir(images_dir) label_file_names = os.listdir(labels_dir) image_file_names.sort() label_file_names.sort() images = [] labels = [] for image_file_name, label_file_name in zip(image_file_names, label_file_names): if image_file_name[:-4] != label_file_name[:-4]: raise ValueError('Image and label file names do not match.') image_file_path = os.path.join(images_dir, image_file_name) label_file_path = os.path.join(labels_dir, label_file_name) image = cv2.imread(image_file_path) label = cv2.imread(label_file_path, cv2.IMREAD_GRAYSCALE) images.append(image) labels.append(label) return np.array(images), np.array(labels) ``` 接下来,我们需要对数据集进行预处理。我们将使用VGG16模型的预处理函数对图像进行预处理,并将标签转换为one-hot编码。以下是预处理数据集的代码: ```python from keras.applications.vgg16 import preprocess_input from keras.utils import to_categorical # 预处理训练集 def preprocess_train_data(images, labels): # 对图像进行预处理 images = preprocess_input(images) # 将标签转换为one-hot编码 labels = to_categorical(labels) return images, labels # 预处理验证集 def preprocess_val_data(images, labels): # 对图像进行预处理 images = preprocess_input(images) # 将标签转换为one-hot编码 labels = to_categorical(labels) return images, labels ``` 现在,我们已经准备好开始构建我们的模型了。我们将使用VGG16作为我们的基础模型,只需要去掉最后一层全连接层即可。我们将在基础模型之上添加一些卷积层和上采样层来构建我们的语义分割模型。以下是构建模型的代码: ```python from keras.models import Model from keras.layers import Input, Conv2D, Conv2DTranspose # 构建模型 def build_model(input_shape, num_classes): # 加载VGG16模型 base_model = VGG16(input_shape=input_shape, include_top=False) # 取消VGG16模型的最后一层 base_model.layers.pop() # 冻结VGG16模型的所有层 for layer in base_model.layers: layer.trainable = False # 添加卷积层和上采样层 x = base_model.output x = Conv2D(256, (3, 3), activation='relu', padding='same')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same')(x) x = Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same')(x) x = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(x) x = Conv2D(64, (3, 3), activation='relu', padding='same')(x) x = Conv2D(64, (3, 3), activation='relu', padding='same')(x) x = Conv2D(num_classes, (1, 1), activation='softmax')(x) # 创建模型 model = Model(inputs=base_model.input, outputs=x) return model ``` 接下来,我们需要训练我们的模型。我们将使用交叉熵损失函数和Adam优化器来训练模型。以下是训练模型的代码: ```python from keras.optimizers import Adam # 训练模型 def train_model(model, images, labels, val_images, val_labels, batch_size, epochs): # 编译模型 model.compile(optimizer=Adam(lr=1e-4), loss='categorical_crossentropy', metrics=['accuracy']) # 训练模型 model.fit(images, labels, batch_size=batch_size, epochs=epochs, validation_data=(val_images, val_labels)) ``` 最后,我们需要使用我们的模型来进行预测。以下是预测图像的代码: ```python # 使用模型预测图像 def predict_image(model, image): # 对图像进行预处理 image = preprocess_input(image[np.newaxis, ...]) # 进行预测 pred = model.predict(image) # 将预测结果转换为标签 pred = np.argmax(pred, axis=-1) # 返回预测结果 return pred[0] ``` 现在,我们已经完成了语义分割的Python实现。我们可以使用以下代码来运行我们的程序: ```python from keras.applications.vgg16 import VGG16 DATA_DIR = 'path/to/data' WEIGHTS_FILE = 'path/to/weights.h5' IMAGE_FILE = 'path/to/image.jpg' # 加载数据集 train_images, train_labels = load_train_data(os.path.join(DATA_DIR, 'train')) val_images, val_labels = load_val_data(os.path.join(DATA_DIR, 'val')) # 预处理数据集 train_images, train_labels = preprocess_train_data(train_images, train_labels) val_images, val_labels = preprocess_val_data(val_images, val_labels) # 构建模型 model = build_model(train_images[0].shape, train_labels.shape[-1]) # 训练模型 train_model(model, train_images, train_labels, val_images, val_labels, batch_size=16, epochs=10) # 保存模型权重 model.save_weights(WEIGHTS_FILE) # 加载模型权重 model.load_weights(WEIGHTS_FILE) # 加载图像 image = cv2.imread(IMAGE_FILE) # 进行预测 pred = predict_image(model, image) # 显示预测结果 cv2.imshow('Prediction', pred) cv2.waitKey(0) cv2.destroyAllWindows() ``` 这就是语义分割的Python实现。希望本文可以帮助大家更好地了解和应用语义分割技术。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值