python's method of image processing with TensorFlow

This article mainly introduces the method of image processing using TensorFlow. The editor thinks it's very good. Now I'll share it with you and give you a reference. Let's follow Xiaobian to have a look

Article directory

1, Zooming in and out of pictures

When you use TensorFlow to zoom in or out of a picture, there are three ways:
1. TF. Image. Resize? Nearest? Neighbor(): interpolation of critical points
2. TF. Image. Resize ﹣ bilinear(): bilinear interpolation
3. TF. Image. Resize ABCD bicubic(): bicubic interpolation algorithm

Here is the sample code:

# encoding:utf-8
# Use TensorFlow to zoom in and out of pictures
import tensorflow as tf
import cv2
import numpy as np
 
# Read pictures
img = cv2.imread("1.jpg")
# Show original picture
cv2.imshow("resource", img)
 
h, w, depth = img.shape
img = np.expand_dims(img, 0)
 
# Critical point interpolation
nn_image = tf.image.resize_nearest_neighbor(img, size=[h+100, w+100])
nn_image = tf.squeeze(nn_image)
with tf.Session() as sess:
  # Run 'init' op
  nn_image = sess.run(nn_image)
nn_image = np.uint8(nn_image)
 
# bilinear interpolation 
bi_image = tf.image.resize_bilinear(img, size=[h+100, w+100])
bi_image = tf.squeeze(bi_image)
with tf.Session() as sess:
  # Run 'init' op
  bi_image = sess.run(bi_image)
bi_image = np.uint8(bi_image)
 
# bicubic interpolation 
bic_image = tf.image.resize_bicubic(img, size=[h+100, w+100])
bic_image = tf.squeeze(bic_image)
with tf.Session() as sess:
  # Run 'init' op
  bic_image = sess.run(bic_image)
bic_image = np.uint8(bic_image)
# Show results picture
cv2.imshow("result_nn", nn_image)
cv2.imshow("result_bi", bi_image)
cv2.imshow("result_bic", bic_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

2, Brightness adjustment of pictures

When you use TensorFlow to adjust the brightness of a picture, there are two ways:
1. TF. Image. Adjust? Brightness(): global adjustment of brightness
2. TF. Image. Random? Brightness(): random adjustment of brightness

Here is the sample code:

# encoding:utf-8
# Use TensorFlow to adjust the brightness of the picture
import tensorflow as tf
import cv2
import numpy as np
 
# Read pictures
img = cv2.imread("1.jpg")
# Show original picture
cv2.imshow("resource", img)
 
img = np.expand_dims(img, 0)
# adjust_brightness
bright_img = tf.image.adjust_brightness(img, delta=.5)
bright_img = tf.squeeze(bright_img)
with tf.Session() as sess:
  # Run 'init' op
  result = sess.run(bright_img)
result = np.uint8(result)
 
rand_image = tf.image.random_brightness(img, max_delta=.5)
rand_image = tf.squeeze(rand_image)
with tf.Session() as sess:
  # Run 'init' op
  result2 = sess.run(rand_image)
result2 = np.uint8(result2)
 
cv2.imshow("result", result)
cv2.imshow("result2", result2)
cv2.waitKey(0)
cv2.destroyAllWindows()

3, Contrast adjustment of pictures

When using TensorFlow to adjust the contrast of a picture, there are two ways:
1. tf.image.adjust_contrast(): global adjustment of contrast
2. tf.image.random_contrast(): random adjustment of contrast

The code is similar to the brightness adjustment of the picture, so we won't go into details here.

4, Saturation adjustment of pictures

When you use TensorFlow to adjust the saturation of a picture, use the following functions:

tf.image.adjust_saturation()

Saturation adjustment range is 0 ~ 5
The following example code:

# encoding:utf-8
# Use TensorFlow to adjust the brightness of the picture
import tensorflow as tf
import cv2
import numpy as np
 
# Read pictures
img = cv2.imread("1.jpg")
# Show original picture
cv2.imshow("resource", img)
 
# Saturation adjustment of image
stand_img = tf.image.adjust_saturation(img, saturation_factor=2.4)
with tf.Session() as sess:
  # Run 'init' op
  result = sess.run(stand_img)
result = np.uint8(result)
 
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

5, Standardization of pictures

Before using TensorFlow to train image data, it is often necessary to perform image standardization operation, which is different from normalization. Normalization does not change the histogram of image, and standardization operation will change the histogram of image. The standardization operation uses the following functions: TF. Image. Per image? Standardization()
Here is the sample code:

# encoding:utf-8
# Use TensorFlow to adjust the brightness of the picture
import tensorflow as tf
import cv2
import numpy as np
 
# Read pictures
img = cv2.imread("1.jpg")
# Show original picture
cv2.imshow("resource", img)
 
# Image standardization operation
stand_img = tf.image.per_image_standardization(img)
with tf.Session() as sess:
  # Run 'init' op
  result = sess.run(stand_img)
result = np.uint8(result)
 
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

6, Color space transformation of image

Use TensorFlow to transform the color space of the image, including HSV, RGB and gray color space. The functions used are as follows:

tf.image.rgb_ to_hsv() 
tf.image.rgb_ to_grayscale() 
tf.image.hsv_ to_rgb()

The code is similar to the code of the standardized operation of the image.
Recommend our Python learning base to see how the seniors learn! From basic Python script, crawler, django, data mining and other programming, as well as sorting out zero basic data to project practice data, to every little partner who loves to learn Python! Every day, seniors regularly explain Python technology, share some learning methods and small details that need attention, and click to join our python learners' gathering place

Published 16 original articles, won praise and 10000 visitors+
Private letter follow

Tags: Session Python encoding Django

Posted on Fri, 14 Feb 2020 06:51:22 -0800 by patrick99e99