<filename>utils.py<gh_stars>1-10
# <NAME> nº9283607
# SCC0251 - Prof. <NAME>
# Teaching Assistant: <NAME>

import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np
import cv2


def plot3d_hsv(img, rgb_img):
    h, s, v = cv2.split(img)
    fig = plt.figure()
    axis = fig.add_subplot(1, 1, 1, projection="3d")

    pixel_colors = rgb_img.reshape((np.shape(rgb_img)[0] * np.shape(rgb_img)[1], 3))
    norm = colors.Normalize(vmin=-1., vmax=1.)
    norm.autoscale(pixel_colors)
    pixel_colors = norm(pixel_colors).tolist()

    axis.scatter(h.flatten(), s.flatten(), v.flatten(), facecolors=pixel_colors, marker=".")
    axis.set_xlabel("Hue")
    axis.set_ylabel("Saturation")
    axis.set_zlabel("Value")
    plt.show()


def plot3d_rgb(img):
    r, g, b = cv2.split(img)
    fig = plt.figure()
    axis = fig.add_subplot(1, 1, 1, projection="3d")

    pixel_colors = img.reshape((np.shape(img)[0] * np.shape(img)[1], 3))
    norm = colors.Normalize(vmin=-1., vmax=1.)
    norm.autoscale(pixel_colors)
    pixel_colors = norm(pixel_colors).tolist()

    axis.scatter(r.flatten(), g.flatten(), b.flatten(), facecolors=pixel_colors, marker=".")
    axis.set_xlabel("Red")
    axis.set_ylabel("Green")
    axis.set_zlabel("Blue")
    plt.show()
    return


def adjust_gamma(image, gamma=1.0):
    # build a lookup table mapping the pixel values [0, 255] to
    # their adjusted gamma values
    inv_gamma = 1.0 / gamma
    table = np.array([((i / 255.0) ** inv_gamma) * 255
                      for i in np.arange(0, 256)]).astype("uint8")

    # apply gamma correction using the lookup table
    return cv2.LUT(image, table)


def precision(expected_result, result):
    if expected_result != 0 and result != 0:
        if expected_result > result:
            return 100 - (np.abs(expected_result - result)) * 100 / expected_result
        else:
            return 100 - (np.abs(expected_result - result)) * 100 / result
    if expected_result != 0 and result == 0:
        return 0.0
    elif expected_result == 0 and result != 0:
        return 0.0
    else:
        return 100.0
<filename>utils.py<gh_stars>1-10
# <NAME> nº9283607
# SCC0251 - Prof. <NAME>
# Teaching Assistant: <NAME>

import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np
import cv2


def plot3d_hsv(img, rgb_img):
    h, s, v = cv2.split(img)
    fig = plt.figure()
    axis = fig.add_subplot(1, 1, 1, projection="3d")

    pixel_colors = rgb_img.reshape((np.shape(rgb_img)[0] * np.shape(rgb_img)[1], 3))
    norm = colors.Normalize(vmin=-1., vmax=1.)
    norm.autoscale(pixel_colors)
    pixel_colors = norm(pixel_colors).tolist()

    axis.scatter(h.flatten(), s.flatten(), v.flatten(), facecolors=pixel_colors, marker=".")
    axis.set_xlabel("Hue")
    axis.set_ylabel("Saturation")
    axis.set_zlabel("Value")
    plt.show()


def plot3d_rgb(img):
    r, g, b = cv2.split(img)
    fig = plt.figure()
    axis = fig.add_subplot(1, 1, 1, projection="3d")

    pixel_colors = img.reshape((np.shape(img)[0] * np.shape(img)[1], 3))
    norm = colors.Normalize(vmin=-1., vmax=1.)
    norm.autoscale(pixel_colors)
    pixel_colors = norm(pixel_colors).tolist()

    axis.scatter(r.flatten(), g.flatten(), b.flatten(), facecolors=pixel_colors, marker=".")
    axis.set_xlabel("Red")
    axis.set_ylabel("Green")
    axis.set_zlabel("Blue")
    plt.show()
    return


def adjust_gamma(image, gamma=1.0):
    # build a lookup table mapping the pixel values [0, 255] to
    # their adjusted gamma values
    inv_gamma = 1.0 / gamma
    table = np.array([((i / 255.0) ** inv_gamma) * 255
                      for i in np.arange(0, 256)]).astype("uint8")

    # apply gamma correction using the lookup table
    return cv2.LUT(image, table)


def precision(expected_result, result):
    if expected_result != 0 and result != 0:
        if expected_result > result:
            return 100 - (np.abs(expected_result - result)) * 100 / expected_result
        else:
            return 100 - (np.abs(expected_result - result)) * 100 / result
    if expected_result != 0 and result == 0:
        return 0.0
    elif expected_result == 0 and result != 0:
        return 0.0
    else:
        return 100.0
# <NAME> nº9283607 # SCC0251 - Prof. <NAME> # Teaching Assistant: <NAME> # build a lookup table mapping the pixel values [0, 255] to # their adjusted gamma values # apply gamma correction using the lookup table