عرض على TensorFlow.org | تشغيل في Google Colab | عرض المصدر على جيثب | تحميل دفتر |
ملخص
في رؤية الكمبيوتر ، يمكن أن يكون لمساحة اللون المحددة أداء مهم للنموذج. في حين RGB هو الأكثر لون الفضاء المشترك، في حالات manay ينفذ نموذج أفضل عند التبديل إلى مساحات اللون بديلة مثل YUV ، YCbCr ، XYZ (CIE) ، الخ
و tensorflow-io توفر حزمة قائمة من لون واجهات برمجة التطبيقات التحويلات المساحة التي يمكن استخدامها لإعداد وزيادة بيانات الصورة.
يثبت
قم بتثبيت الحزم المطلوبة ، وأعد تشغيل وقت التشغيل
pip install -q tensorflow-io
قم بتنزيل نموذج الصورة
على سبيل المثال الصورة المستخدمة في هذا البرنامج التعليمي هو القط في الثلج ، على الرغم من يمكن أن يحل محله أي صور JPEG.
وفيما يلي سيتم تحميل الصور وحفظها على القرص المحلي كما sample.jpg :
curl -o sample.jpg -L https://storage.googleapis.com/download.tensorflow.org/example_images/320px-Felis_catus-cat_on_snow.jpgls -ls sample.jpg
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 17858 100 17858 0 0 235k 0 --:--:-- --:--:-- --:--:-- 235k
20 -rw-rw-r-- 1 kbuilder kokoro 17858 Oct 27 16:33 sample.jpg
إستعمال
قراءة ملف الصورة
قراءة وفك الصورة إلى uint8 التنسور الشكل (213, 320, 3)
import tensorflow as tf
import tensorflow_io as tfio
image = tf.image.decode_jpeg(tf.io.read_file('sample.jpg'))
print(image.shape, image.dtype)
(213, 320, 3) <dtype: 'uint8'>
يمكن عرض الصورة من خلال:
import matplotlib.pyplot as plt
plt.figure()
plt.imshow(image)
plt.axis('off')
plt.show()

تحويل RGB إلى Grayscale
و RGB يمكن تحويلها إلى صورة Grayscale للحد من قناة 3-1 مع tfio.experimental.color.rgb_to_grayscale :
grayscale = tfio.experimental.color.rgb_to_grayscale(image)
print(grayscale.shape, grayscale.dtype)
# use tf.squeeze to remove last channel for plt.imshow to display:
plt.figure()
plt.imshow(tf.squeeze(grayscale, axis=-1), cmap='gray')
plt.axis('off')
plt.show()
(213, 320, 1) <dtype: 'uint8'>

تحويل RGB إلى BGR
بعض البرامج الصور والكاميرا manufacturors قد تفضل BGR ، والتي يمكن الحصول عليها من خلال tfio.experimental.color.rgb_to_bgr :
bgr = tfio.experimental.color.rgb_to_bgr(image)
print(bgr.shape, bgr.dtype)
plt.figure()
plt.imshow(bgr)
plt.axis('off')
plt.show()
(213, 320, 3) <dtype: 'uint8'>

تحويل RGB إلى CIE XYZ
CIE XYZ (أو CIE 1931 XYZ هو لون الفضاء شيوعا في العديد من برامج معالجة الصور. وفيما يلي هو التحويل من RGB إلى CIE XYZ من خلال tfio.experimental.color.rgb_to_xyz . ملاحظة tfio.experimental.color.rgb_to_xyz يفترض العائمة المدخلات نقطة في نطاق [0, 1] هناك حاجة إضافية حتى ما قبل المعالجة:
# convert to float32
image_float32 = tf.cast(image, tf.float32) / 255.0
xyz_float32 = tfio.experimental.color.rgb_to_xyz(image_float32)
# convert back uint8
xyz = tf.cast(xyz_float32 * 255.0, tf.uint8)
print(xyz.shape, xyz.dtype)
plt.figure()
plt.imshow(xyz)
plt.axis('off')
plt.show()
(213, 320, 3) <dtype: 'uint8'>

تحويل RGB إلى YCbCr
وأخيرا، YCbCr هو لون الفضاء الافتراضي في العديد من أنظمة الفيديو. تحويل إلى YCbCr يمكن أن يتم ذلك من خلال tfio.experimental.color.rgb_to_ycbcr :
ycbcr = tfio.experimental.color.rgb_to_ycbcr(image)
print(ycbcr.shape, ycbcr.dtype)
plt.figure()
plt.imshow(ycbcr, cmap='gray')
plt.axis('off')
plt.show()
(213, 320, 3) <dtype: 'uint8'>

ما هو أكثر إثارة للاهتمام، على الرغم من أن YCbCr يمكن أن تتحلل إلى Y' (لمى)، Cb (الأزرق والفرق صفاء)، و Cr (الحمراء الفرق صفاء) المكونات مع بعضها حمل مكون معلومات مفيدة ادراكي:
y, cb, cr = ycbcr[:,:,0], ycbcr[:,:,1], ycbcr[:,:,2]
# Y' component
plt.figure()
plt.imshow(y, cmap='gray')
plt.axis('off')
plt.show()
# Cb component
plt.figure()
plt.imshow(cb, cmap='gray')
plt.axis('off')
plt.show()
# Cr component
plt.figure()
plt.imshow(cr, cmap='gray')
plt.axis('off')
plt.show()



عرض على TensorFlow.org
تشغيل في Google Colab
عرض المصدر على جيثب
تحميل دفتر