عرض على TensorFlow.org | تشغيل في Google Colab | عرض المصدر على جيثب | تحميل دفتر |
سيوضح هذا الكمبيوتر الدفتري كيفية استخدام بعض عمليات الصور في TensorFlow Addons.
فيما يلي قائمة بعمليات الصور التي ستغطيها في هذا المثال:
pip install -q -U tensorflow-addons
import tensorflow as tf
import numpy as np
import tensorflow_addons as tfa
import matplotlib.pyplot as plt
img_path = tf.keras.utils.get_file('tensorflow.png','https://tensorflow.org/images/tf_logo.png')
Downloading data from https://tensorflow.org/images/tf_logo.png 40960/39781 [==============================] - 0s 3us/step
img_raw = tf.io.read_file(img_path)
img = tf.io.decode_image(img_raw)
img = tf.image.convert_image_dtype(img, tf.float32)
img = tf.image.resize(img, [500,500])
plt.title("TensorFlow Logo with shape {}".format(img.shape))
_ = plt.imshow(img)
bw_img = 1.0 - tf.image.rgb_to_grayscale(img)
plt.title("Mask image with shape {}".format(bw_img.shape))
_ = plt.imshow(bw_img[...,0], cmap='gray')
التصفية المتوسطة هي تقنية ترشيح تُستخدم غالبًا لإزالة التشويش من صورة أو إشارة. تكمن الفكرة في تشغيل صورة بكسل بكسل واستبدالها بمتوسط قيم وحدات البكسل المجاورة.
mean = tfa.image.mean_filter2d(img, filter_shape=11)
_ = plt.imshow(mean)
تقوم هذه العملية بتدوير الصورة المعطاة بالزاوية (بالراديان) التي يدخلها المستخدم.
rotate = tfa.image.rotate(img, tf.constant(np.pi/8))
_ = plt.imshow(rotate)
تقوم هذه العملية بتحويل الصورة المعطاة على أساس متجه التحويل المعطى من قبل المستخدم.
transform = tfa.image.transform(img, [1.0, 1.0, -250, 0.0, 1.0, 0.0, 0.0, 0.0])
_ = plt.imshow(transform)
تغير هذه العملية مقياس اللون لصورة RGB معينة إلى YIQ ولكن هنا يتم اختيار قيم دلتا الصبغة والتشبع عشوائيًا من النطاق المحدد.
delta = 0.5
lower_saturation = 0.1
upper_saturation = 0.9
lower_value = 0.2
upper_value = 0.8
rand_hsvinyiq = tfa.image.random_hsv_in_yiq(img, delta, lower_saturation, upper_saturation, lower_value, upper_value)
_ = plt.imshow(rand_hsvinyiq)
تغير هذه العملية مقياس اللون لصورة RGB معينة إلى YIQ ولكن هنا بدلاً من الاختيار العشوائي ، تعد قيم دلتا الصبغة والتشبع مدخلات من المستخدم.
delta = 0.5
saturation = 0.3
value = 0.6
adj_hsvinyiq = tfa.image.adjust_hsv_in_yiq(img, delta, saturation, value)
_ = plt.imshow(adj_hsvinyiq)
هذه العملية مخصصة للالتواء غير الخطي لأي صورة محددة بواسطة مجال التدفق لمتجه الإزاحة (تستخدم هنا القيم العشوائية على سبيل المثال).
input_img = tf.image.convert_image_dtype(tf.expand_dims(img, 0), tf.dtypes.float32)
flow_shape = [1, input_img.shape[1], input_img.shape[2], 2]
init_flows = np.float32(np.random.normal(size=flow_shape) * 2.0)
dense_img_warp = tfa.image.dense_image_warp(input_img, init_flows)
dense_img_warp = tf.squeeze(dense_img_warp, 0)
_ = plt.imshow(dense_img_warp)
تعمل هذه العملية على تحديث قيمة البكسل بمسافة إقليدية من بكسل المقدمة إلى الخلفية.
- ملاحظة: يأخذ فقط صورة ثنائية وينتج عنه صورة محولة. إذا تم إعطاء صورة مختلفة ، فسيؤدي ذلك إلى صورة ذات قيمة واحدة
gray = tf.image.convert_image_dtype(bw_img,tf.uint8)
# The op expects a batch of images, so add a batch dimension
gray = tf.expand_dims(gray, 0)
eucid = tfa.image.euclidean_dist_transform(gray)
eucid = tf.squeeze(eucid, (0, -1))
_ = plt.imshow(eucid, cmap='gray')