def correlation_coefficient(path, template_path):
    '''
    Correlation coefficient equals to covariance of image region and template through variance of region multiplied by
    the variance of template.

    :param path: path to the image
    :param template_path: path to the template
    :return: image, which shows the best matching
    '''

    # initializing image and other helping variables
    image_array = open_image(path)
    template_array = open_image(template_path)
    image_height, image_width = image_array.shape
    template_height, template_width = template_array.shape
    half_template_height = math.floor(template_height / 2)
    half_template_width = math.floor(template_width / 2)
    resulting_img = np.zeros(image_height * image_width).reshape(
        image_height, image_width)

    # iterating over the image
    for i in range(half_template_height, image_height - half_template_height):

        for j in range(half_template_width, image_width - half_template_width):
            # region extraction
            if template_height % 2 == 1 and template_width % 2 == 1:
                cor_region = image_array[i - half_template_height:i +
                                         half_template_height + 1,
                                         j - half_template_width:j +
                                         half_template_width + 1]
            elif template_height % 2 == 1:
                cor_region = image_array[i - half_template_height:i +
                                         half_template_height + 1,
                                         j - half_template_width:j +
                                         half_template_width]
            elif template_width % 2 == 1:
                cor_region = image_array[i - half_template_height:i +
                                         half_template_height,
                                         j - half_template_width:j +
                                         half_template_width + 1]
            else:
                cor_region = image_array[i - half_template_height:i +
                                         half_template_height,
                                         j - half_template_width:j +
                                         half_template_width]

            covar_region_template = sum(sum(np.cov(cor_region,
                                                   template_array)))

            variance_region = np.var(cor_region)
            variance_template = np.var(template_array)

            resulting_img[i, j] = covar_region_template / (variance_region *
                                                           variance_template)

    return resulting_img
def histogram_equalization(path):
    '''
    Calculates equalized histogram of the image.
    Formula: floor(cumulative_histogram(i)*quantity_of_grey_values/(image_height*image_width))
    :param path:
    :return:
    '''
    image_array = open_image(path)

    image_height, image_width = image_array.shape
    size = image_height * image_width

    # histogram and cumulative histogram are created
    hist = histogram(path)
    quantity_of_grey_values = len(hist)
    cumul_hist = cumulative_histogram(hist)

    # iterating over cumulative histogram and appending values to the equalized histogram
    equalized_histogram = []

    for value in cumul_hist:

        equalized_histogram.append(
            floor(value * quantity_of_grey_values / size))

    return equalized_histogram
Beispiel #3
0
def change_contrast_brightness(path, contrast=1, brightness=0):

    '''
    Changes contrast and brightness of the given image with parameters contrast and brightness.
    Formula contrast*image[i,j]+brightness

    :param path: path to the image
    :param contrast: contrast of the new image, default 1
    :param brightness: brightness of the new image, default 0
    :return: new image
    '''

    image_array = open_image(path)

    for i in range(len(image_array)):

        for j in range(len(image_array[i])):

            value = contrast*image_array[i, j] + brightness

            # if values go out of boundaries they have to be normalized
            if value < 0:
                image_array[i, j] = 0
            elif value > 255:
                image_array[i, j] = 255
            else:
                image_array[i, j] = contrast*image_array[i, j] + brightness

    return image_array
def median_filter(path, region_size):
    '''
    Values for every pixel equals to the median of all values in the region
    :param path: path to the image
    :param region_size: size of the region that will be extracted and its median will be calculated
    :return: resulting image
    '''

    # initializing image and other helping variables
    image_array = open_image(path)
    image_height, image_width = image_array.shape
    half_size = math.floor(region_size / 2)
    resulting_img = np.zeros(image_height * image_width).reshape(
        image_height, image_width)
    # iterating over the image
    for i in range(half_size, image_height - half_size):

        for j in range(half_size, image_width - half_size):
            # extraction of region of the given size and calculation of its median
            region = (image_array[i - half_size:i + half_size + 1,
                                  j - half_size:j + half_size + 1]).ravel()
            # median is calculated in C-extension module exmod
            resulting_img[i, j] = exmod.median(list(region))

    return resulting_img
def linear_filter(path, mask):
    '''
    Filters the image on the given path with linear filter mask.
    :param path:
    :param mask:
    :return: filtered image
    '''

    image_array = open_image(path)
    size_x, size_y = image_array.shape
    height_m, width_m = mask.shape
    border_height = math.floor(height_m / 2)
    border_width = math.floor(width_m / 2)
    result = np.zeros(size_x * size_y).reshape(size_x, size_y)

    # The factor for normalisation is set
    factor = sum(sum(mask))

    for i in range(border_height, size_x - border_height):
        for j in range(border_width, size_y - border_width):
            # region extraction
            region = image_array[i - border_height:i + border_height + 1,
                                 j - border_width:j + border_width + 1]
            # multiplication of region with mask and summing up of its values
            value = sum(sum(region * mask))
            # normalization of value
            if factor != 0:
                value = value / factor
            result[i, j] = value
    return result
Beispiel #6
0
def champfer_matching(path, template_path):
    '''
    Champfer Matching Algorithm - comparison of binary images based on distance transformation and
    Manhattan-Distance
    :param path: path to the image
    :param template_path: path to the template
    :return: resulting image, minimum values on this image show points, where template can match
    '''

    # initializing image and other helping variables
    image_array = open_image(path)
    template_array = open_image(template_path)
    image_height, image_width = image_array.shape
    template_height, template_width = template_array.shape
    half_template_height = math.floor(template_height / 2)
    half_template_width = math.floor(template_width / 2)
    distance_transformation_matrix = distance_transformation(path)
    resulting_img = np.zeros((image_height - template_height + 1) *
                             (image_width - template_width + 1)).reshape(
                                 image_height - template_height + 1,
                                 image_width - template_width + 1)

    quantity_of_foreground_pix = 0  # quantity of foreground pixels in template
    for i in range(template_height - 1):

        for j in range(template_width - 1):

            if template_array[i, j] == 0:

                quantity_of_foreground_pix += 1

    # iterating over the image
    for i in range(0, image_height - template_height):

        for j in range(0, image_width - template_width):
            value = 0
            for k in range(template_height):

                for m in range(template_width):
                    # summing up values in distance transformation matrix
                    if template_array[k, m] == 0:
                        value += distance_transformation_matrix[i + k, j + m]
            if quantity_of_foreground_pix != 0:
                resulting_img[i, j] = value / quantity_of_foreground_pix

    return resulting_img
def kuwahara_filter(path, mask_size):
    '''
    Applies kuwahara filter on the given image
    :param path: path to the image
    :param mask_size: should be odd number, at least 3
    :return: filtered image
    '''

    # variable initialization
    image_array = open_image(path)
    image_height, image_width = image_array.shape
    half_size = math.floor(mask_size / 2)
    resulting_img = np.zeros(image_height * image_width).reshape(
        image_height, image_width)

    for i in range(half_size, image_height - half_size):

        for j in range(half_size, image_width - half_size):

            # region extraction
            top_left = (
                image_array[i - half_size:i + 1, j - half_size:j +
                            1]).ravel()  # extracts every region from array
            top_right = (
                image_array[i - half_size:i + 1, j:j + half_size +
                            1]).ravel()  # and converts it into 1D array
            bottom_left = (image_array[i:i + half_size + 1,
                                       j - half_size:j + 1]).ravel()
            bottom_right = (image_array[i:i + half_size + 1,
                                        j:j + half_size + 1]).ravel()

            # variance calculation
            #tl_var = top_left.var()
            #tr_var = top_right.var()
            #bl_var = bottom_left.var()
            #br_var = bottom_right.var()

            # variance is calculated in C-extension module exmod
            tl_var = exmod.var(list(top_left))
            tr_var = exmod.var(list(top_right))
            bl_var = exmod.var(list(bottom_left))
            br_var = exmod.var(list(bottom_right))

            # calculates the min of variances of all regions
            m = np.amin([tl_var, tr_var, bl_var, br_var])

            # finds out which region has the lowest variance, current pixel gets than the mean of that region
            if m == tl_var:
                resulting_img[i, j] = np.mean(top_left)
            elif m == tr_var:
                resulting_img[i, j] = np.mean(top_right)
            elif m == bl_var:
                resulting_img[i, j] = np.mean(bottom_left)
            elif m == br_var:
                resulting_img[i, j] = np.mean(bottom_right)
    return resulting_img
Beispiel #8
0
def check_if_image_grey(path):
    '''
    Function should check whether the picture is grey or coloured
    param path: path to the image that should be checked
    return: should return a boolean; True means it is a grey picture and False means it is a coloured pictures
    '''

    image_array = open_image(path)

    # image array should have only 2 dimensions to be grey image
    if np.ndim(image_array) == 2:
        return True
    else:
        return False
def distance_transformation(path):

    """
    Distance transformation of the binary image based on Manhattan Distance (city block distance)

    :param path: path to the image
    :return: distance transformation array of the image
    SAMPLE MATLAB CODE
    """
    image_array = open_image(path)
    height_img, width_img = image_array.shape
    distance = (255*np.ones(height_img*width_img)).reshape(height_img, width_img)
    dL = np.zeros(4)  # saving manhattan distance values
    dR = np.zeros(4)  # saving manhattan distance values

    # initializing distance of foreground pixels with 0
    for i in range(0, height_img-1):

        for j in range(0, width_img-2):

            if image_array[i, j] == 255:
                distance[i, j] = 0

    # iteration over the image LEFT -> RIGHT
    for i in range(1, height_img):

        for j in range(1, width_img-1):

            if distance[i, j] > 0:
                dL[0] = 2 + distance[i-1, j-1]
                dL[1] = 1 + distance[i-1, j]
                dL[2] = 2 + distance[i-1, j+1]
                dL[3] = 1 + distance[i, j-1]
                # calculation minimum of distance values of the region
                distance[i, j] = min(dL)

    # iteration over the image RIGHT -> LEFT
    for i in (0, height_img-2):

        for j in range(width_img-1, 0):

            if distance[i, j] > 0:
                dR[0] = 2 + distance[i+1, j+1]
                dR[1] = 1 + distance[i+1, j]
                dR[2] = 2 + distance[i+1, j-1]
                dR[3] = 1 + distance[i, j+1]
                # calculation minimum of distance values of the region
                distance[i, j] = min(dR)
    return distance
Beispiel #10
0
def histogram(path):
    '''
    Function takes path to the grey_value_image and returns histogram of grey values
    :param path:
    :return:
    '''
    image_array = open_image(path)

    histogram_list = np.zeros(255, dtype=int)

    for row in image_array:

        for element in row:

            histogram_list[element] += 1

    return histogram_list
def convert_to_grey(path,
                    weight_red=0.299,
                    weight_green=0.587,
                    weight_blue=0.114):
    '''
    Function converts RGB-image to grey_value_image and depends on red, green and blue weight values
    :param path: the picture's path
    :param weight_red: weight of Red colour, for tv-signals equals 0.299
    :param weight_green: weight of Green colour, for tv-signals equals to 0.587
    :param weight_blue: weight of Blue colour, for tv-signals equals to 0.114
    :return: returns the grey_value_image or plots it
    '''

    image_array = open_image(path)
    i_grau = weight_red * image_array[:, :,
                                      0] + weight_green * image_array[:, :,
                                                                      1] + weight_blue * image_array[:, :,
                                                                                                     2]
    return i_grau
Beispiel #12
0
def mirror_image(path, parameter='b'):
    '''
    Function mirrors image to vertical or horizontal axis or both of them.
    :param path: if parameter == v - image will be mirrored against vertical axis
    if parameter == h - image will be mirrored against horizontal axis
    if parameter == b - image will be mirrored against both axis
    :return:
    '''

    image_array = open_image(path)

    if parameter == 'b':
        return np.flip(np.flip(image_array, 0), 1)
    elif parameter == 'v':
        return np.flip(image_array, 0)
    elif parameter == 'h':
        return np.flip(image_array, 1)
    else:
        print("Wrong value for parameter")
        return 0
Beispiel #13
0
def main():
    image = open_image.open_image('./image/to_compress1.jpg')
    image.show()
    numpy_rgb_arrays = process_image.find_numpy_arrays(image)

    #Finding the actual length of stored bits which is required to store image data.
    actual_length_of_stored_bits = process_image.find_length_of_stored_bits(
        numpy_rgb_arrays[0])

    encoded_length = 0
    for i in range(1, 4):
        frequencies = process_image.find_freq(numpy_rgb_arrays[i])
        huffman_heap = huffman_tree.create_huffman_tree(frequencies)
        huffman_codes = huffman_encode.huffman_encode(huffman_heap)
        encoded_length = encoded_length + huffman_encode.find_length_of_encoded_bits(
            huffman_codes, frequencies)

    compression_ratio = huffman_encode.find_compression_ratio(
        actual_length_of_stored_bits, encoded_length)
    return compression_ratio
Beispiel #14
0
def test_dataset():
    """ Permet de tester l'algorithme sur le dataset BioID"""

    global args

    distances = []
    errors = []
    accepte = 0

    for i in range(args.nb_img):

        # Construction du nom de fichier
        img_name = str(i)
        if len(img_name) == 1:
            img_name = '000' + img_name
        elif len(img_name) == 2:
            img_name = '00' + img_name
        elif len(img_name) == 3:
            img_name = '0' + img_name

        # Ouverture du premier fichier
        img = open_image('data/BioID_' + img_name + '.pgm')
        positions = open_eye_pos('data/BioID_' + img_name + '.eye')

        # Centre des yeux gauche et droit
        c_gt_r = positions[0], positions[1]
        c_gt_l = positions[2], positions[3]

        # Estimation du centre des yeux
        detection, centres = detect_eye(img, is_gray = True)

        # Dessin des centres verite terrain
        cv2.line(detection, tuple(c_gt_l-np.array((0,2))),
                tuple(c_gt_l+np.array((0,2))), (255,255,0), 1)
        cv2.line(detection, tuple(c_gt_l-np.array((2,0))),
                tuple(c_gt_l+np.array((2,0))), (255,255,0), 1)
        cv2.line(detection, tuple(c_gt_r-np.array((0,2))),
                tuple(c_gt_r+np.array((0,2))), (255,255,0), 1)
        cv2.line(detection, tuple(c_gt_r-np.array((2,0))),
                tuple(c_gt_r+np.array((2,0))), (255,255,0), 1)

        # Si deux yeux ont ete detectes
        if len(centres) == 2:

            # Compte d'images ou la detection a ete faite
            accepte += 1

            # Calcul de la distance
            dist_l = np.trunc(distance_center(c_gt_l, centres[0]) * 100) / 100
            dist_r = np.trunc(distance_center(c_gt_r, centres[1]) * 100) / 100
            distances.append(dist_l)
            distances.append(dist_r)

            # Erreur moyenne
            d = distance_center(c_gt_l, np.array(c_gt_r))
            error = (dist_l + dist_r) / (2 * d)
            errors.append(error)

            # Dessin des distances
            font = cv2.FONT_HERSHEY_SIMPLEX
            orig_l = c_gt_l[0], c_gt_l[1] + 20
            cv2.putText(detection, str(dist_l), orig_l, font, 0.3, (255,255,255))

            orig_r = c_gt_r[0], c_gt_r[1] + 20 
            cv2.putText(detection, str(dist_r), orig_r, font, 0.3, (255,255,255))

            # Affichage
            if args.images:
                plt.imshow(detection, cmap='gray')
                #plt.pause(0.2)
                plt.show()
            gc.collect()

    # Statistiques sur les performances de l'agorithme
    moyenne = np.mean(distances)
    print("\n------ STATISTIQUES ------")
    print("moyenne:", moyenne)
    print("  total:", args.nb_img)
    print("accepte:", accepte)
    print(" rejete:", args.nb_img - accepte)
    print("--------------------------")

    plt.hist(errors, cumulative=True, histtype='step', bins=500, normed=True)
    plt.show()