Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy
- import math
- from cvlib import greyscale_list_to_cvimg
- from cvlib import rgblist_to_cvimg
- from cvlib import add_tuples
- from cvlib import multiply_tuple
- from labb5a import cvimg_to_list
- import random
- def pixel_constraint(hlow, hhigh, slow, shigh, vlow, vhigh):
- """
- Returns the function spec_pixel_constraint that takes
- the value of a pixel and determines whether that pixel
- is within the given color
- intervals.
- """
- def spec_pixel_contraint(pixel):
- h = pixel[0]
- s = pixel[1]
- v = pixel[2]
- if not isinstance(pixel, tuple):
- raise TypeError("Input is not a tuple")
- if not len(pixel) == 3:
- raise IndexError("Wrong tuple size")
- if not (0 <= h <= 255 and 0 <= s <= 255 and 0 <= v <= 255):
- raise Exception("Incorrect values in tuple")
- if h > hlow and h < hhigh \
- and v > vlow and v < vhigh \
- and s > slow and s < shigh:
- return 1
- else:
- return 0
- return spec_pixel_contraint
- def generator_from_image(orig_list):
- """Returns the function pixel_in_image that takes a
- list of tuples and returns a specific tuple in that list."""
- def spec_img(i):
- if i > len(orig_list) + 1:
- raise IndexError("Index out of bounds")
- return orig_list[i]
- return spec_img
- def stars(index):
- """Takes a tuple and randomly sets the
- value to represent black or white."""
- val = random.random() * 255 if random.random() > 0.99 else 0
- return (val, val, val)
- def gradient_condition(pixel):
- """Takes the v-value of an hsv tuple
- and returns a value between 0.0 and 1.0.
- """
- return pixel[2] / 255
- def combine_images(mask, mask_function, image_generator1, image_generator2):
- """Merges 2 images with a smooth transition."""
- try:
- res = [add_tuples(multiply_tuple(image_generator1(i),
- mask_function(mask[i])),
- multiply_tuple(image_generator2(i),
- 1 - mask_function(mask[i]))) for i in range(len(mask))]
- return res
- except IndexError:
- raise IndexError("Input pictures might be of different size.")
- except TypeError:
- raise TypeError("Input is not a picture")
- def test5b1():
- hsv_plane = cv2.cvtColor(cv2.imread("flygplan.jpg"), cv2.COLOR_BGR2HSV)
- is_sky = pixel_constraint(100, 150, 50, 200, 100, 255)
- plane_list = cvimg_to_list(hsv_plane)
- sky_pixels = list(map(lambda x: x*255, map(is_sky, plane_list)))
- cv2.imshow('sky', greyscale_list_to_cvimg(sky_pixels, hsv_plane.shape[0],
- hsv_plane.shape[1]))
- cv2.waitKey(0)
- def test5b2():
- orig_img = cv2.imread("flygplan.jpg")
- orig_list = cvimg_to_list(orig_img)
- generator = generator_from_image(orig_list)
- new_list = [generator(i) for i in range(len(orig_list))]
- cv2.imshow('original', orig_img)
- cv2.imshow('new', rgblist_to_cvimg(new_list, orig_img.shape[0],
- orig_img.shape[1]))
- cv2.waitKey(0)
- def test5b3():
- bgr_plane = cv2.imread("flygplan.jpg")
- hsv_plane = cv2.cvtColor(cv2.imread("flygplan.jpg"), cv2.COLOR_BGR2HSV)
- condition = pixel_constraint(100, 150, 50, 200, 100, 255)
- hsv_plane_list = cvimg_to_list(hsv_plane)
- bgr_plane_list = cvimg_to_list(bgr_plane)
- plane_gen = generator_from_image(cvimg_to_list(bgr_plane))
- result = combine_images(hsv_plane_list, condition, stars, plane_gen)
- new_img = rgblist_to_cvimg(result, bgr_plane.shape[0], bgr_plane.shape[1])
- cv2.imshow('Final image', new_img)
- cv2.waitKey(0)
- def test5b4():
- bgr_plane = cv2.imread("flygplan.jpg")
- mask = cvimg_to_list(cv2.cvtColor(cv2.imread("gradient.jpg"),
- cv2.COLOR_BGR2HSV))
- img_gen2 = generator_from_image(cvimg_to_list(cv2.imread("flygplan.jpg")))
- img_gen1 = generator_from_image(cvimg_to_list(cv2.imread("flower.jpg")))
- res = combine_images(mask, gradient_condition, img_gen1, img_gen2)
- new_img = rgblist_to_cvimg(res, bgr_plane.shape[0], bgr_plane.shape[1])
- cv2.imshow('Final image', new_img)
- cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement