הצג באתר TensorFlow.org | הפעל בגוגל קולאב | צפה במקור ב-GitHub | הורד מחברת |
סקירה כללית
בראייה ממוחשבת, למרחב הצבע הנבחר יכול להיות ביצועים משמעותיים של הדגם. בעוד RGB הוא מרחב הצבע הנפוץ ביותר, במצבי manay מבצע מודל טוב יותר כאשר מחליפים מקומות צבע חלופיים כגון YUV , YCbCr , XYZ (CIE) , וכו '
tensorflow-io החבילה מספקת רשימה של ממשקי API שטח מרות צבע שיכול לשמש כדי להכין להרחיב את נתון תמונה.
להכין
התקן את החבילות הנדרשות והפעל מחדש את זמן הריצה
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 לגווני אפור
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' (luma), Cb (chroma כחול-הבדל), ו Cr (chroma אדום-הבדל) רכיבים עם כל carry רכיב מידע משמעותי מבחינה תפיסתית:
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
הפעל בגוגל קולאב
צפה במקור ב-GitHub
הורד מחברת