[Python] computing the sum of rectangular haar features using the integral image

Discussão em 'Python' iniciado por Stack, Dezembro 18, 2024.

  1. Stack

    Stack Membro Participativo

    I wanted to use this guys code to calculate the integral image and sum regions for Haar features because I couldn't find anything else reliable and didn't really want to do it myself. But now it seems that this code doesn't work anyway and I can't get it to work but want to make it work using the padded integral image.

    I just don't really understand what to input for the sum_region function. He calculates the integral image using to_integral_image() which is correct, but then the bit I don't understand is the sum_region function which doesn't seem to work for me when I test it, the whole flipping the coordinates and the padding of the integral image has confused me and I can't get my head over it it's really frustrating.

    The integral for me was calculated perfectly, but the sum_region() function just doesn't seem to work. I tried adding one to both the bottom_right coordinates but that didn't work for edge cases.

    If anyone could help me get over this annoying mental brain-fog I'd really appreciate it.

    def to_integral_image(img_arr):
    """
    Calculates the integral image based on this instance's original image data.
    :param img_arr: Image source data
    :type img_arr: numpy.ndarray
    :return Integral image for given image
    :rtype: numpy.ndarray
    """
    # an index of -1 refers to the last row/column
    # since row_sum is calculated starting from (0,0),
    # rowSum(x, -1) == 0 holds for all x
    row_sum = np.zeros(img_arr.shape)
    # we need an additional column and row
    integral_image_arr = np.zeros((img_arr.shape[0] + 1, img_arr.shape[1] + 1))
    for x in range(img_arr.shape[1]):
    for y in range(img_arr.shape[0]):
    row_sum[y, x] = row_sum[y-1, x] + img_arr[y, x]
    integral_image_arr[y+1, x+1] = integral_image_arr[y+1, x-1+1] + row_sum[y, x]
    return integral_image_arr


    def sum_region(integral_img_arr, top_left, bottom_right):
    """
    Calculates the sum in the rectangle specified by the given tuples.
    :param integral_img_arr:
    :type integral_img_arr: numpy.ndarray
    :param top_left: (x, y) of the rectangle's top left corner
    :type top_left: (int, int)
    :param bottom_right: (x, y) of the rectangle's bottom right corner
    :type bottom_right: (int, int)
    :return The sum of all pixels in the given rectangle
    :rtype int
    """
    # swap tuples
    top_left = (top_left[1], top_left[0])
    bottom_right = (bottom_right[1], bottom_right[0])
    if top_left == bottom_right:
    return integral_img_arr[top_left]
    top_right = (bottom_right[0], top_left[1])
    bottom_left = (top_left[0], bottom_right[1])
    return integral_img_arr[bottom_right] - integral_img_arr[top_right] - integral_img_arr[bottom_left] + integral_img_arr[top_left]


    I tried inputting a normal 0 indexed array index and printing out the results but the padding and the flipping of the coordinates is just really confusing and I can't seem to wrap my brain around it all. The input to sum_region is (0,0) and (2,1) which to me in the original image corresponds to the 1 and the 8 and the output I get is 3 when it should be 27. I don't understand where the original author means the origin would be for x,y in the original image, and thus I'm not 100% on what is supposed to be inputted.

    This is what I was using to test it.

    import numpy as np
    from integral_image import *

    original_image = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
    ],)


    integral = to_integral_image(original_image)
    print(original_image)
    print(integral)

    # region in the original image

    top_left = (0,0)
    bottom_right = (2,1)
    sum = sum_region(integral, top_left, bottom_right)
    print(f"Original Top left: {original_image[top_left]}")
    print(f"Original Bottom Right {original_image[bottom_right]}")

    print(f"Sum: {sum}")

    Continue reading...

Compartilhe esta Página