博客1--tensorflow的圖像基本處理操作


話不多,具體內容在開源中國里我的博客:https://my.oschina.net/u/3770644

代碼:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#導入必要包
import matplotlib.pyplot as plt
import tensorflow as tf
#從本地磁盤讀取圖像數據
image_raw_data = tf.gfile.FastGFile("C:/path/to/picture.jpg", 'rb').read()
#創建會話,使用tf.image.decode_jpeg 解碼jpg格式圖片 tf.image.decode_png解碼png格式圖片
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)

#在神經網絡處理圖像時,使用的數據都是實數類型,而圖片的像素取值為0-255,所以需要先進行0-1值域的轉化
img_data1 = tf.image.convert_image_dtype(img_data, dtype=tf.float32)

#調整圖片的大小,參數說明:圖像,調整大小,使用算法 0-雙線性插值法 1-最近鄰居法 2-雙三次插值法 3-面積插值法
resized1 = tf.image.resize_images(img_data1, [300, 300], method=0)
resized2 = tf.image.resize_images(img_data1, [300, 300], method=1)
resized3 = tf.image.resize_images(img_data1, [300, 300], method=2)
resized4 = tf.image.resize_images(img_data1, [300, 300], method=3)

#以圖片的剪裁實現圖片大小的調整
croped = tf.image.resize_image_with_crop_or_pad(img_data1, 500, 500)
padded = tf.image.resize_image_with_crop_or_pad(img_data1, 2000, 2000)

#以圖像的比例0-1調整圖像的大小. 指定區域圖像的裁剪、填充tf.image.crop_to_bounding_box \ tf.image.pad_to_bounding_box
central_cropped = tf.image.central_crop(img_data1, 0.5)

#圖像翻轉
flipped1 = tf.image.flip_up_down(img_data1)
flipped2 = tf.image.flip_left_right(img_data1)
flipped3 = tf.image.transpose_image(img_data1)
#隨機概率翻轉
flipped4 = tf.image.random_flip_up_down(img_data1)
flipped5 = tf.image.random_flip_left_right(img_data1)

#亮度的調整
adjusted1 = tf.image.adjust_brightness(img_data1, -0.5)
adjusted2 = tf.image.adjust_brightness(adjusted1, 0.8)
adjusted3 = tf.clip_by_value(adjusted2, 0.0, 1.0) #截斷,防止亮度調整過度,拉回0-1

#對比度的調整
adjusted4 = tf.image.random_contrast(img_data1, 0.5, 2) #隨機0.5-2

#色相的調整
adjusted5 = tf.image.adjust_hue(img_data1, 0.5)
adjusted6 = tf.image.random_hue(img_data1, 0.4) #隨機0-0.8

#飽和度調整
adjusted7 = tf.image.adjust_saturation(img_data1, 5)
adjusted8 = tf.image.random_saturation(img_data1, 2, 10)
#標准化
adjusted9 = tf.image.per_image_standardization(img_data1)

#標注框tf.image.draw_bounding_boxes函數需要圖像矩陣輸入為實數tf.image.convert_image_dtype(img_data),且是一個4維的矩陣batch,所以需要矩陣加一
img_data2 = tf.image.resize_images(img_data1, [250, 150], method=1)
batched = tf.expand_dims(tf.image.convert_image_dtype(img_data2, tf.float32), 0) #數據處理
boxs = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]]) #定義框的位置--比例定義坐標起止點
result = tf.image.draw_bounding_boxes(batched, boxs)
result = tf.reduce_sum(result, 0) #降維處理
#tf.image.sample_distroted_bounding_box完成隨機圖像截取
boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.32, 0.47, 0.5, 0.56]]])
begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(tf.shape(img_data2), bounding_boxes=boxes, min_object_covered=0.4) ##框,框中比例
batched1 = tf.expand_dims(tf.image.convert_image_dtype(img_data2, tf.float32), 0)
image_with_box = tf.image.draw_bounding_boxes(batched1, bbox_for_draw)
image_with_box = tf.reduce_sum(image_with_box, 0) #
distorted_image = tf.slice(img_data1, begin, size)
distorted_image = tf.reduce_sum(distorted_image, 0) #

#多張圖片在一起打開展示
plt.figure()
plt.subplot(5, 6, 1)
plt.imshow(img_data.eval())
plt.subplot(5, 6, 2)
plt.imshow(resized1.eval())
plt.subplot(5, 6, 3)
plt.imshow(resized2.eval())
plt.subplot(5, 6, 4)
plt.imshow(resized3.eval())
plt.subplot(5, 6, 5)
plt.imshow(resized4.eval())
plt.subplot(5, 6, 6)
plt.imshow(croped.eval())
plt.subplot(5, 6, 7)
plt.imshow(padded.eval())
plt.subplot(5, 6, 8)
plt.imshow(central_cropped.eval())
plt.subplot(5, 6, 9)
plt.imshow(flipped1.eval())
plt.subplot(5, 6, 10)
plt.imshow(flipped2.eval())
plt.subplot(5, 6, 11)
plt.imshow(flipped3.eval())
plt.subplot(5, 6, 12)
plt.imshow(flipped4.eval())
plt.subplot(5, 6, 13)
plt.imshow(flipped5.eval())
plt.subplot(5, 6, 14)
plt.imshow(adjusted1.eval())
plt.subplot(5, 6, 15)
plt.imshow(adjusted2.eval())
plt.subplot(5, 6, 16)
plt.imshow(adjusted3.eval())
plt.subplot(5, 6, 17)
plt.imshow(adjusted4.eval())
plt.subplot(5, 6, 18)
plt.imshow(adjusted5.eval())
plt.subplot(5, 6, 19)
plt.imshow(adjusted6.eval())
plt.subplot(5, 6, 20)
plt.imshow(adjusted7.eval())
plt.subplot(5, 6, 21)
plt.imshow(adjusted8.eval())
plt.subplot(5, 6, 22)
plt.imshow(adjusted9.eval())
plt.show()
plt.figure()
plt.subplot(1, 2, 1)
plt.imshow(result.eval())
plt.subplot(1, 2, 2)
plt.imshow(image_with_box.eval())
plt.show()
代碼中有詳細的批注,大家可以理解。不懂的可以留言討論。

其實,這跟書上的略有不同。因為按照書上的代碼,進行result的imshow()顯示的時候,報了如下的錯誤:

 

根據錯誤得提示,我們可以發現,是imshow(result)的時候提示result的維度不匹配的問題。因為我們再處理時對圖片的數據進行了加維的操作,就是說result這時候是一個四維的結果,但是呢,imshow()函數能夠識別的是二維和三維的數據,所以需要對其進行降維操作。在后面得隨機框中是一樣的道理。所以使用了reduce_sum(result,0)函數來降維。

還有一點需要提醒是:在框時,需要提前對圖像進行壓縮(裁剪)處理,因為小圖片的框易於顯示,否則你只有將圖片拉大后才能看清框在哪,有的時候還會沒有。其他的就沒有什么需要注意的地方。

最后來看看我們的運行結果吧,色彩很絢麗哦,個人比較喜歡紫色的呢。

 

 

目前還是指定區域框選,那天有興趣研究下,自動識別輪廓框選,這樣是不是就是人臉識別了呢?哈哈哈


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM