Python uses TensorFlow for image processing, pythontensorflow

Source: Internet
Author: User

Python uses TensorFlow for image processing, pythontensorflow

I. Zoom in and out images

There are three ways to use TensorFlow to zoom in and out images:

1. tf. image. resize_nearest_neighbor (): critical point interpolation
2. tf. image. resize_bilinear (): bilinear interpolation
3. tf. image. resize_bicubic (): Dual-cube interpolation algorithm

The following is the sample code:

# Encoding: UTF-8 # using TensorFlow for image scaling import tensorflow as tfimport cv2import numpy as np # reading image img = cv2.imread ("1.jpg") # displaying original image cv2.imshow (" resource ", img) h, w, depth = img. shapeimg = 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) # dual-cube interpolation algorithm 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) # cv2.imshow ("result_nn", nn_image) cv2.imshow ("result_bi", bi_image) cv2.imshow ("success", bic_image) cv2.waitKey (0) Success ()

Ii. Adjust the brightness of the image

There are two ways to adjust the brightness of an image using TensorFlow:
1. tf. image. adjust_brightness (): Global brightness adjustment
2. tf. image. random_brightness (): Random brightness adjustment

The following is the sample code:

# Encoding: UTF-8 # adjust the brightness of an image using TensorFlow import tensorflow as tfimport cv2import numpy as np # Read image img = cv2.imread ("1.jpg") # display original image cv2.imshow (" resource ", img) img = np. expand_dims (img, 0) # adjust_brightnessbright_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. Adjust the image contrast

There are two ways to adjust the contrast of an image using TensorFlow:
1. tf. image. adjust_contrast (): Global contrast Adjustment
2. tf. image. random_contrast (): random contrast Adjustment

The code is similar to the brightness adjustment of the image.

Iv. Adjust the image saturation

When using TensorFlow to adjust the image saturation, use the following functions:

tf.image.adjust_saturation() 

The saturation adjustment range is 0 ~ 5

The following sample code:

# Encoding: UTF-8 # adjust the brightness of an image using TensorFlow import tensorflow as tfimport cv2import numpy as np # Read image img = cv2.imread ("1.jpg") # display original image cv2.imshow (" resource ", img) # adjust the image saturation 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 ()

V. Picture Standardization

Before using TensorFlow to train image data, you often need to perform the image standardization operation, which is different from normalization. Normalization does not change the image histogram, the standardization operation changes the histogram of the image. Standardized operations use the following functions:

tf.image.per_image_standardization() 

The following is the sample code:

# Encoding: UTF-8 # adjust the brightness of an image using TensorFlow import tensorflow as tfimport cv2import numpy as np # Read image img = cv2.imread ("1.jpg") # display original image 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 ()

Vi. Color Space conversion of images

Use TensorFlow to convert the color space of an image, including the conversion between HSV, RGB, and grayscale color spaces. The function is as follows:

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

The code is similar to the code used to standardize images.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.