Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.21 KB | None | 0 0
  1. import cv2
  2. import numpy
  3. import math
  4. from cvlib import greyscale_list_to_cvimg
  5. from cvlib import rgblist_to_cvimg
  6. from cvlib import add_tuples
  7. from cvlib import multiply_tuple
  8. from labb5a import cvimg_to_list
  9. import random
  10.  
  11.  
  12. def pixel_constraint(hlow, hhigh, slow, shigh, vlow, vhigh):
  13.     """
  14.    Returns the function spec_pixel_constraint that takes
  15.    the value of a pixel and determines whether that pixel
  16.    is within the given color
  17.    intervals.
  18.    """
  19.  
  20.     def spec_pixel_contraint(pixel):
  21.         h = pixel[0]
  22.         s = pixel[1]
  23.         v = pixel[2]
  24.         if not isinstance(pixel, tuple):
  25.             raise TypeError("Input is not a tuple")
  26.         if not len(pixel) == 3:
  27.             raise IndexError("Wrong tuple size")
  28.         if not (0 <= h <= 255 and 0 <= s <= 255 and 0 <= v <= 255):
  29.             raise Exception("Incorrect values in tuple")
  30.         if h > hlow and h < hhigh \
  31.                 and v > vlow and v < vhigh \
  32.                 and s > slow and s < shigh:
  33.             return 1
  34.         else:
  35.             return 0
  36.     return spec_pixel_contraint
  37.  
  38.  
  39. def generator_from_image(orig_list):
  40.     """Returns the function pixel_in_image that takes a
  41.    list of tuples and returns a specific tuple in that list."""
  42.     def spec_img(i):
  43.         if i > len(orig_list) + 1:
  44.             raise IndexError("Index out of bounds")
  45.         return orig_list[i]
  46.     return spec_img
  47.  
  48.  
  49. def stars(index):
  50.     """Takes a tuple and randomly sets the
  51.    value to represent black or white."""
  52.  
  53.     val = random.random() * 255 if random.random() > 0.99 else 0
  54.     return (val, val, val)
  55.  
  56.  
  57. def gradient_condition(pixel):
  58.     """Takes the v-value of an hsv tuple
  59.    and returns a value between 0.0 and 1.0.
  60.    """
  61.     return pixel[2] / 255
  62.  
  63.  
  64. def combine_images(mask, mask_function, image_generator1, image_generator2):
  65.     """Merges 2 images with a smooth transition."""
  66.  
  67.     try:
  68.         res = [add_tuples(multiply_tuple(image_generator1(i),
  69.                mask_function(mask[i])),
  70.                multiply_tuple(image_generator2(i),
  71.                1 - mask_function(mask[i]))) for i in range(len(mask))]
  72.         return res
  73.     except IndexError:
  74.         raise IndexError("Input pictures might be of different size.")
  75.     except TypeError:
  76.         raise TypeError("Input is not a picture")
  77.  
  78.  
  79. def test5b1():
  80.     hsv_plane = cv2.cvtColor(cv2.imread("flygplan.jpg"), cv2.COLOR_BGR2HSV)
  81.     is_sky = pixel_constraint(100, 150, 50, 200, 100, 255)
  82.     plane_list = cvimg_to_list(hsv_plane)
  83.     sky_pixels = list(map(lambda x: x*255, map(is_sky, plane_list)))
  84.     cv2.imshow('sky', greyscale_list_to_cvimg(sky_pixels, hsv_plane.shape[0],
  85.                hsv_plane.shape[1]))
  86.     cv2.waitKey(0)
  87.  
  88.  
  89. def test5b2():
  90.     orig_img = cv2.imread("flygplan.jpg")
  91.     orig_list = cvimg_to_list(orig_img)
  92.     generator = generator_from_image(orig_list)
  93.     new_list = [generator(i) for i in range(len(orig_list))]
  94.     cv2.imshow('original', orig_img)
  95.     cv2.imshow('new', rgblist_to_cvimg(new_list, orig_img.shape[0],
  96.                                        orig_img.shape[1]))
  97.     cv2.waitKey(0)
  98.  
  99.  
  100. def test5b3():
  101.     bgr_plane = cv2.imread("flygplan.jpg")
  102.     hsv_plane = cv2.cvtColor(cv2.imread("flygplan.jpg"), cv2.COLOR_BGR2HSV)
  103.     condition = pixel_constraint(100, 150, 50, 200, 100, 255)
  104.     hsv_plane_list = cvimg_to_list(hsv_plane)
  105.     bgr_plane_list = cvimg_to_list(bgr_plane)
  106.     plane_gen = generator_from_image(cvimg_to_list(bgr_plane))
  107.     result = combine_images(hsv_plane_list, condition, stars, plane_gen)
  108.     new_img = rgblist_to_cvimg(result, bgr_plane.shape[0], bgr_plane.shape[1])
  109.     cv2.imshow('Final image', new_img)
  110.     cv2.waitKey(0)
  111.  
  112.  
  113. def test5b4():
  114.     bgr_plane = cv2.imread("flygplan.jpg")
  115.     mask = cvimg_to_list(cv2.cvtColor(cv2.imread("gradient.jpg"),
  116.                                       cv2.COLOR_BGR2HSV))
  117.     img_gen2 = generator_from_image(cvimg_to_list(cv2.imread("flygplan.jpg")))
  118.     img_gen1 = generator_from_image(cvimg_to_list(cv2.imread("flower.jpg")))
  119.     res = combine_images(mask, gradient_condition, img_gen1, img_gen2)
  120.     new_img = rgblist_to_cvimg(res, bgr_plane.shape[0], bgr_plane.shape[1])
  121.     cv2.imshow('Final image', new_img)
  122.     cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement