def gray(content_dir,content_gray): a = np.asarray(Image.open(content_dir).convert('L')).astype('float') depath = 10 # (0-100) grad = np.gradient(a) # 取图像灰度的梯度值 grad_x, grad_y = grad # 分别取横纵图像梯度值 grad_x = grad_x * depath / 100. grad_y = grad_y * depath / 100. A = np.sqrt(grad_x ** 2 + grad_y ** 2 + 1.) uni_x = grad_x / A uni_y = grad_y / A uni_z = 1. / A vec_el = np.pi / 2.2 # 光源的俯视角度,弧度值 vec_az = np.pi / 4 # 光源的方位角度,弧度值 dx = np.cos(vec_el) * np.cos(vec_az) # 光源对 x 轴的影响 dy = np.cos(vec_el) * np.sin(vec_az) # 光源对 y 轴的影响 dz = np.sin(vec_el) # 光源对 z 轴的影响 b = 255 * (dx * uni_x + dy * uni_y + dz * uni_z) # 光源归一化 b = b.clip(0, 255) im = Image.fromarray(b.astype('uint8')) # 重构图像 im.save(content_gray) return
时间: 2023-12-20 17:06:13 浏览: 29
这是一个Python函数,用于将一张彩色图片转换为灰度图并进行光照处理。具体实现过程如下:
1. 使用PIL库中的Image.open()打开彩色图片,并使用convert()方法将其转换为灰度图。
2. 定义一个参数depath,表示光照强度的深浅程度。
3. 使用numpy库中的gradient()方法获取图像的梯度值,分别表示横向和纵向的梯度值。
4. 将梯度值乘以光照强度,得到光照后的梯度值。
5. 计算每个像素点的法向量,并将其归一化。
6. 定义光源的俯视角度和方位角度,计算光源对每个像素点的影响。
7. 将光源归一化后的影响值与原始像素值相乘,得到光照后的像素值。
8. 将像素值限制在0-255之间,避免出现超出像素值范围的情况。
9. 将光照后的像素值重新构建成一张图片,并保存到指定路径。
10. 最后返回保存路径。
相关问题
def load_images_and_labels(dataset_dir, image_size): images = [] labels = [] class_labels = os.listdir(dataset_dir) for i, class_label in enumerate(class_labels): class_dir = os.path.join(dataset_dir, class_label) for image_file in os.listdir(class_dir): image_path = os.path.join(class_dir, image_file) image = Image.open(image_path).convert('RGB') image = image.resize(image_size) image = np.array(image) images.append(image) labels.append(i) images = np.array(images) labels = np.array(labels) return images, labels
这段代码的作用是什么?
这段代码定义了一个函数load_images_and_labels,用于加载指定目录下的图像和标签数据。函数的参数包括dataset_dir和image_size,其中dataset_dir表示图像数据所在的目录,image_size表示图像的尺寸。函数返回两个numpy数组,分别为images和labels,分别表示图像数据和对应的标签数据。
具体实现过程为,首先通过os.listdir函数获取dataset_dir下的所有子目录,每个子目录表示一个类别。然后遍历每个子目录,读取该目录下的所有图像文件,并将其转换为RGB格式、调整大小为image_size,并转换为numpy数组。同时,将该图像对应的标签记录在labels数组中,并用整数表示类别。最后,将所有图像和标签数据分别存储在numpy数组images和labels中,并返回这两个数组。
import tensorflow.compat.v1 as tf tf.disable_v2_behavior() from PIL import Image import matplotlib.pyplot as plt import input_data import model import numpy as np import xlsxwriter num_threads = 4 def evaluate_one_image(): test_dir = 'data/test/' import glob import xlwt test_img = glob.glob(test_dir + '*.jpg') workbook = xlsxwriter.Workbook('formatting.xlsx') worksheet = workbook.add_worksheet('My Worksheet') for index,img in enumerate(test_img): image = Image.open(img) image = image.resize([208, 208]) image_array = np.array(image) with tf.Graph().as_default(): BATCH_SIZE = 1 N_CLASSES = 4 image = tf.cast(image_array, tf.float32) image = tf.image.per_image_standardization(image) image = tf.reshape(image, [1, 208, 208, 3]) logit = model.cnn_inference(image, BATCH_SIZE, N_CLASSES) logit = tf.nn.softmax(logit) x = tf.placeholder(tf.float32, shape=[208, 208, 3]) logs_train_dir = 'log/' saver = tf.train.Saver() with tf.Session() as sess: prediction = sess.run(logit, feed_dict={x: image_array}) max_index = np.argmax(prediction) workbook.close() if __name__ == '__main__': evaluate_one_image()改为多线程运算
要将代码改为多线程运算,您可以使用 TensorFlow 的 `tf.train.Coordinator()` 和 `tf.train.start_queue_runners()` 来管理和启动多个线程。以下是修改后的代码示例:
```python
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from PIL import Image
import matplotlib.pyplot as plt
import input_data
import model
import numpy as np
import xlsxwriter
num_threads = 4
def evaluate_one_image(image_array, index):
with tf.Graph().as_default():
BATCH_SIZE = 1
N_CLASSES = 4
image = tf.cast(image_array, tf.float32)
image = tf.image.per_image_standardization(image)
image = tf.reshape(image, [1, 208, 208, 3])
logit = model.cnn_inference(image, BATCH_SIZE, N_CLASSES)
logit = tf.nn.softmax(logit)
x = tf.placeholder(tf.float32, shape=[208, 208, 3])
logs_train_dir = 'log/'
saver = tf.train.Saver()
with tf.Session() as sess:
prediction = sess.run(logit, feed_dict={x: image_array})
max_index = np.argmax(prediction)
# 将结果写入 Excel 表格
workbook = xlsxwriter.Workbook('formatting.xlsx')
worksheet = workbook.add_worksheet('My Worksheet')
worksheet.write(index, 0, max_index)
workbook.close()
def evaluate_images(test_img):
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for index, img in enumerate(test_img):
image = Image.open(img)
image = image.resize([208, 208])
image_array = np.array(image)
# 在每个线程中调用 evaluate_one_image()
tf.compat.v1.threading.Thread(target=evaluate_one_image, args=(image_array, index)).start()
# 请求停止所有线程
coord.request_stop()
# 等待所有线程完成
coord.join(threads)
if __name__ == '__main__':
test_dir = 'data/test/'
import glob
import xlwt
test_img = glob.glob(test_dir + '*.jpg')
evaluate_images(test_img)
```
在修改后的代码中,我们首先定义了一个新的函数 `evaluate_one_image()`,它接受图像数组和索引作为参数,表示要评估的单个图像。在这个函数中,我们将使用传递的图像数组进行评估操作。
然后,在 `evaluate_images()` 函数中,我们使用 `tf.train.Coordinator()` 和 `tf.train.start_queue_runners()` 启动了多个线程。在每个线程中,我们使用 `tf.compat.v1.threading.Thread()` 创建一个新的线程,并在其中调用 `evaluate_one_image()` 函数来评估图像。
最后,在主函数中通过调用 `evaluate_images()` 函数来启动评估过程。
这样,您就可以利用多线程同时评估多个图像了。
希望对您有帮助!如果您还有其他问题,请随时提问。