Ejemplo n.º 1
0
def billboard_hack():
    """
    Hack and replace the billboard!

    Parameters:
    ----------- 

    Returns:
    --------
    Ihack  - Hacked RGB intensity image, 8-bit np.array (i.e., uint8).
    """
    # Bounding box in Y & D Square image.
    bbox = np.array([[404, 490, 404, 490], [38, 38, 354, 354]])

    # Point correspondences.
    Iyd_pts = np.array([[416, 485, 488, 410], [40, 61, 353, 349]])
    Ist_pts = np.array([[2, 218, 218, 2], [2, 2, 409, 409]])

    Iyd = imread('../billboard/yonge_dundas_square.jpg')
    Ist = imread('../billboard/uoft_soldiers_tower_dark.png')

    Ihack = np.asarray(Iyd)
    Ist = np.asarray(Ist)

    #--- FILL ME IN ---

    # Let's do the histogram equalization first.
    st_equalized = histogram_eq(Ist)

    # Compute the perspective homography we need...
    H, A = dlt_homography(Iyd_pts, Ist_pts)

    # Main 'for' loop to do the warp and insertion

    #define the polygon of the billboard
    billboard_polygon = Path(Iyd_pts.T)
    #loop through the bounding box
    for x in range(np.min(bbox.T[:, 0]), np.max(bbox.T[:, 0])):
        for y in range(np.min(bbox.T[:, 1]), np.max(bbox.T[:, 1])):
            #check if the point in the bounding box is within the polygon
            if (billboard_polygon.contains_point(np.array([x, y]).T) == False):
                continue
            #define the homogenous point of the billboard
            homogenous_point = np.array([[x, y, 1.0]]).T
            #get the corresponding point in the soldiers tower
            correspondence = H.dot(homogenous_point)
            #normalize the correspondence point by w
            correspondence = correspondence / correspondence[2, 0]
            #bilinearly interpolate the soldiers tower
            interpolated = bilinear_interp(st_equalized, correspondence[:-1,
                                                                        0:])
            #set the intensity of the yd picture to st
            Ihack[y, x] = np.array([interpolated, interpolated, interpolated])

    return Ihack
Ejemplo n.º 2
0
def billboard_hack():
    """
    Hack and replace the billboard!

    Parameters:
    ----------- 

    Returns:
    --------
    Ihack  - Hacked RGB intensity image, 8-bit np.array (i.e., uint8).
    """
    # Bounding box in Y & D Square image.
    bbox = np.array([[404, 490, 404, 490], [38, 38, 354, 354]])

    # Point correspondences.
    Iyd_pts = np.array([[416, 485, 488, 410], [40, 61, 353, 349]])
    Ist_pts = np.array([[2, 218, 218, 2], [2, 2, 409, 409]])

    Iyd = imread('../billboard/yonge_dundas_square.jpg')
    Ist = imread('../billboard/uoft_soldiers_tower_dark.png')

    Ihack = np.asarray(Iyd)
    Ist = np.asarray(Ist)

    # Apply Histogram equalization
    Ist = histogram_eq(Ist)

    # Compute homography
    H, A = dlt_homography(Iyd_pts, Ist_pts)

    # Define billboard polygon
    vertices = np.array([[x, y] for x, y in zip(Iyd_pts[0], Iyd_pts[1])])
    billboard = Path(vertices)

    # Insert Soldiers Tower picture into Young-Dundas picture
    h, w, _ = Iyd.shape
    for y in range(h):
        for x in range(w):
            if billboard.contains_points([[x, y]]):
                p = H.dot(np.array([x, y, 1]))
                xp, yp, _ = p / p[2]
                val = bilinear_interp(Ist, np.array([[xp], [yp]]))
                Iyd[y, x] = np.array([val, val, val])

    # Uncomment to see picture
    #plt.imshow(Ihack)
    #plt.show()
    #imwrite('billboard_hacked_2.jpg', Iyd);

    return Iyd
def billboard_hack():
    """
    Hack and replace the billboard!

    Parameters:
    ----------- 

    Returns:
    --------
    Ihack  - Hacked RGB intensity image, 8-bit np.array (i.e., uint8).
    """
    bbox = np.array([[404, 490, 404, 490], [38, 38, 354, 354]])

    # Point correspondences.
    Iyd_pts = np.array([[416, 485, 488, 410], [40, 61, 353, 349]])
    Ist_pts = np.array([[2, 218, 218, 2], [2, 2, 409, 409]])

    Iyd = imread('../billboard/yonge_dundas_square.jpg')
    Ist = imread('../billboard/uoft_soldiers_tower_dark.png')

    Ihack = np.asarray(Iyd)
    Ist = np.asarray(Ist)

    im = Image.fromarray(Iyd)
    im.save("original_image.jpeg")

    # -----------------------------------------------------------------------------------------------------------------#
    # --- FILL ME IN ---
    # Let's do the histogram equalization first.
    Ist_balanced = histogram_eq(Ist)

    # Compute the perspective homography we need...
    H, A = dlt_homography(Iyd_pts, Ist_pts)

    # Main 'for' loop to do the warp and insertion -
    # this could be vectorized to be faster if needed!
    UL = [bbox[0][0], bbox[1][0]]  # Upper Left
    UR = [bbox[0][1], bbox[1][1]]  # Upper Right
    # Updated bounding box definition:
    UR = [488, 59]
    BL = [bbox[0][2], bbox[1][2]]  # Bottom Left
    BR = [bbox[0][3], bbox[1][3]]  # Bottom Right
    Iyd_poly = [UL, UR, BR, BL, UL]
    codes = [
        Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY
    ]
    billboard_path = Path(Iyd_poly, codes)
    for v in range(len(Iyd)):
        for u in range(len(Iyd[0])):
            if billboard_path.contains_point([u, v]):
                [x, y, normal] = np.dot(H, np.array([u, v, 1]))
                [x, y, normal] = [x, y, normal] / normal
                if (x < 219 and y < 410):
                    Ihack[v][u] = bilinear_interp(Ist_balanced,
                                                  np.array([[x, y]]).T)
    im = Image.fromarray(Ihack)
    im.save("final_image.jpeg")

    Ihack = Ihack.astype(np.uint8)
    #------------------

    return Ihack
import matplotlib.pyplot as plt
#%matlibplot inline
import numpy as np
from imageio import imread
from bilinear_interp import bilinear_interp

# Load input image, choose subpixel location.
I  = imread('../billboard/peppers_grey.png')
pt = np.array([[142.45, 286.73]]).T  # (x, y), where x is first row.
b = bilinear_interp(I, pt)
print(b)
Ejemplo n.º 5
0
def billboard_hack():
    """
    Hack and replace the billboard!

    Parameters:
    -----------

    Returns:
    --------
    Ihack  - Hacked RGB intensity image, 8-bit np.array (i.e., uint8).
    """
    # Bounding box in Y & D Square image.
    bbox = np.array([[404, 490, 404, 490], [38, 38, 354, 354]])

    # Point correspondences.
    Iyd_pts = np.array([[416, 485, 488, 410], [40, 61, 353, 349]])
    Ist_pts = np.array([[2, 218, 218, 2], [2, 2, 409, 409]])

    Iyd = imread('../billboard/yonge_dundas_square.jpg')
    Ist = imread('../billboard/uoft_soldiers_tower_dark.png')

    Ihack = np.asarray(Iyd)
    Ist = np.asarray(Ist)

    #--- FILL ME IN ---

    # Let's do the histogram equalization first.
    Ist_eq = histogram_eq(Ist)

    # Compute the perspective homography we need...
    H, A = dlt_homography(Iyd_pts, Ist_pts)

    # Main 'for' loop to do the warp and insertion -
    # this could be vectorized to be faster if needed!
    # bbox [x][y]
    # adjust the path a little bit. change the bbox [1,1] plus 20
    path = Path(
        [(bbox[0, 0], bbox[1, 0]), (bbox[0, 1], bbox[1, 1] + 20),
         (bbox[0, 3], bbox[1, 3]), (bbox[0, 2], bbox[1, 2]),
         (bbox[0, 0], bbox[1, 0])],
        [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY])

    # (x, y)  y is the perpendicular coordinate

    for y in range(Iyd.shape[0]):
        for x in range(Iyd.shape[1]):
            if path.contains_point([x, y]) == 1:
                # calculate the coordinate in picture Ist
                [u, v, w] = np.dot(H, [x, y, 1])
                [u, v, w] = [u, v, w] / w
                # if (u < Ist.shape[1] and v < Ist.shape[0]):
                if (u < 219 and v < 410):
                    Ihack[y, x] = bilinear_interp(Ist_eq, np.array([[u, v]]).T)

    #------------------

    # plt.imshow(Ihack)
    # plt.show()
    # imwrite(Ihack, 'billboard_hacked.png');

    return Ihack
Ejemplo n.º 6
0
def billboard_hack():
    """
    Hack and replace the billboard!

    Parameters:
    ----------- 

    Returns:
    --------
    Ihack  - Hacked RGB intensity image, 8-bit np.array (i.e., uint8).
    """
    # Bounding box in Y & D Square image.
    bbox = np.array([[404, 490, 404, 490], [38, 38, 354, 354]])

    # Extract bounding box min and max
    xmin = 404
    xmax = 490
    ymin = 38
    ymax = 354

    # Point correspondences.
    Iyd_pts = np.array([[416, 485, 488, 410], [40, 61, 353, 349]])
    Ist_pts = np.array([[2, 218, 218, 2], [2, 2, 409, 409]])

    # Get images
    Iyd = imread('../billboard/yonge_dundas_square.jpg')
    Ist = imread('../billboard/uoft_soldiers_tower_dark.png')
    Ihack = Iyd

    Ist = np.asarray(Ist)

    # Let's do the histogram equalization first.
    Ist = histogram_eq(Ist)

    # Compute the perspective homography we need...
    H, A = dlt_homography(Iyd_pts, Ist_pts)

    # Generate bounds in the Yonge-Dundas image for where we want to replace
    bound_pts = []
    for i in range(4):
        bound_pts += [(Iyd_pts[0][i], Iyd_pts[1][i])]
    bounds = Path(bound_pts)

    # Main for loop to calculate pixel values. Iterate through all bounding box pts
    for x in range(xmin, xmax):
        for y in range(ymin, ymax):
            # If the point is not in the bound for the ad, ignore it
            if not bounds.contains_point((x, y)):
                continue

            # If point is in the bound for the ad, calculate the corresponding
            # point in the soldiers tower image using homography
            pt_yd = np.array((x, y, 1))
            pt_st = np.matmul(H, pt_yd)
            pt_st /= pt_st[2]

            # Use bilinear interpolation to find the pixel value we should put
            # in the Yonge-Dundas image (replace each channel with the value)
            for channel in range(3):
                Ihack[y][x][channel] = bilinear_interp(
                    Ist, np.reshape(pt_st[:-1], (2, 1)))

    return Ihack
Ejemplo n.º 7
0
def billboard_hack():
    """
    Hack and replace the billboard!
    Parameters:
    ----------- 
    Returns:
    --------
    Ihack  - Hacked RGB intensity image, 8-bit np.array (i.e., uint8).
    """

    # Bounding box in Y & D Square image.
    bbox = np.array([[404, 490, 404, 490], [38, 38, 354, 354]])

    # Point correspondences.
    Iyd_pts = np.array([[416, 485, 488, 410], [40, 61, 353, 349]])
    Ist_pts = np.array([[2, 218, 218, 2], [2, 2, 409, 409]])

    Iyd = imread(r'./pictures/yonge_dundas_square.jpg')
    Ist = imread(r'./pictures/uoft_soldiers_tower_dark.png')

    Ihack = np.asarray(Iyd)
    Ist = np.asarray(Ist)

    #--- FILL ME IN ---

    # Let's do the histogram equalization first.
    Ist_histeq = histogram_eq(Ist)

    # Compute the perspective homography we need...
    H, A = dlt_homography(Iyd_pts, Ist_pts)

    # Define the path of interest (polygon)
    '''
    Path represents a series of possibly disconnected, possibly closed, line and curve segments.
    
    The underlying storage is made up of two parallel numpy arrays:
    vertices: an Nx2 float array of vertices
    codes: an N-length uint8 array of vertex types
    
    '''
    vertices = np.array([[x, y] for x, y in zip(Iyd_pts[0], Iyd_pts[1])])
    polygon = Path(vertices)

    # Find hwight and width of the YD picture
    height, width, _ = Iyd.shape

    # Iterate through
    for x in range(width):
        for y in range(height):
            # uses matplot.lib.paths.contains_points() to verify if the current point is in the path (polygon)
            if polygon.contains_points([[x, y]]):
                # If 'True', then the dot product of the perpective homography matrix, H, and the current normalized
                # coordinates, [x ,y, 1]. This is the new homogeneous coordinate
                p = H.dot(np.array([x, y, 1]))
                # The resultant p coordinate is then normalized to by dividing the 3rd coordinate, thereby giving us the new x
                # and y
                xp, yp, _ = p / p[2]
                # Generate a 2x1 array for the point to comply with the input requirement for bilinear interp
                pt = np.array([[xp], [yp]])
                # Since the new transform warps and resizes the ST picture, it must be resized after its new perspectice
                # coordinates have been found. Use bilinear interpolation for this
                interp = bilinear_interp(Ist, pt)
                # Write the new intensity value for the YD pic. Note that a 3 element array is needed because the YD picture
                # is in RGB
                Ihack[y, x] = np.array([interp, interp, interp])

    #-----------------

    imwrite('billboard_hacked.png', Ihack)
    while (True):
        cv2.imshow('Hacked Billboard', Ihack)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cv2.destroyAllWindows()

    return Ihack
V = np.zeros((40, 8))
""" V is hard coded for 20 points, could change to arbitrary size by
    appending rows instead of filling in place. """
# for each point fill in two rows of V (can this be done cleaner?)
for i in range(source.shape[0]):
    c = source[i, 0]
    d = source[i, 1]
    a = target[i, 0]
    b = target[i, 1]
    V[2 * i] = [c, d, 1, 0, 0, 0, -1 * a * c, -1 * a * d]
    V[2 * i + 1] = [0, 0, 0, c, d, 1, -1 * b * c, -1 * b * d]

# find H by getting inverse of V, dot with T, append 1 and reshape to square.
h = np.dot(psuedo_inverse(V), T)
h = np.append(h, 1)
H = np.reshape(h, (3, 3))

# loop through new image pixels
for a in range(new_imdat.shape[0]):
    for b in range(new_imdat.shape[1]):
        # find the c,d from input image corresponding to a,b output
        v = np.array([a, b, 1])
        cd = np.dot(inv(H), v)
        c = cd[0] / cd[2]
        d = cd[1] / cd[2]
        # use bilinear_interp to assign value of corresponding pixel
        new_imdat[a, b] = bi.bilinear_interp(d, c, imdat)

Image.fromarray(new_imdat.astype('uint8')).save("test1.jpg")
Image.fromarray(new_imdat.astype('uint8')).show()
Ejemplo n.º 9
0
def billboard_hack():
    """
    Hack and replace the billboard!

    Parameters:
    ----------- 

    Returns:
    --------
    Ihack  - Hacked RGB intensity image, 8-bit np.array (i.e., uint8).
    """
    # Bounding box in Y & D Square image.
    bbox = np.array([[404, 490, 404, 490], [38, 38, 354, 354]])

    # Point correspondences.
    Iyd_pts = np.array([[416, 485, 488, 410], [40, 61, 353, 349]])
    Ist_pts = np.array([[2, 218, 218, 2], [2, 2, 409, 409]])

    Iyd = imread('../billboard/yonge_dundas_square.jpg')
    Ist = imread('../billboard/uoft_soldiers_tower_dark.png')

    Ihack = np.asarray(Iyd)
    Ist = np.asarray(Ist)

    #--- FILL ME IN ---

    # Let's do the histogram equalization first.
    J = histogram_eq(Ist)

    # Compute the perspective homography we need...
    H, A = dlt_homography(Iyd_pts, Ist_pts)
    a = H.dot(np.array([410, 349, 1]))
    #print (a/a[-1])

    # Main 'for' loop to do the warp and insertion -
    # this could be vectorized to be faster if needed!

    #make the path
    path = []
    for i in range(4):
        tup = (Iyd_pts[1][i], Iyd_pts[0][i])
        path.append(tup)
    path = Path(path)

    #loop through the bounding box
    for i in range(404, 490):
        for j in range(38, 354):
            #only treat points within the shape
            if Path.contains_points(path, [[j, i]]):
                #computing the homogeneous representation of the point
                new_coord = H.dot(np.array([i, j, 1]))
                #transfering the homogeneous form to normal form
                new_coord = np.array([[new_coord[0] / new_coord[-1]],
                                      [new_coord[1] / new_coord[-1]]])
                #perform bilinear interpolation on J
                new_pt = bilinear_interp(J, new_coord)
                #write the point in
                Ihack[j][i] = [new_pt, new_pt, new_pt]
    # You may wish to make use of the contains_points() method
    # available in the matplotlib.path.Path class!

    #------------------

    #plt.imshow(Ihack)
    #plt.show()
    #imwrite(Ihack, 'billboard_hacked.png');

    return Ihack