Exemplo n.º 1
0
    def bilinear_interpolation(self, image, fx, fy):
        """resizes an image using bilinear interpolation approximation for resampling
        image: the image to be resampled
        fx: scale along x direction (eg. 0.5, 1.5, 2.5)
        fx: scale along y direction (eg. 0.5, 1.5, 2.5)
        returns a resized image based on the bi-linear interpolation method
        """

        # Write your code for bi-linear interpolation here
        [width, height] = image.shape       # get dimensions of the image
        w = int(width * fx)                 # calculate new width
        h = int(height * fy)                # calculate new height
        new_image = numpy.zeros((w, h), dtype='uint8')
        intrp = interpolation()
        for x in range(w):
            for y in range(h):
                i = int(x/fx)
                j = int(y/fy)
                if (i+1) >= width:
                    i = i-1
                elif (j+1) >= height:
                    j = j-1
                pt1 = [i, j, image[i, j]]               # Q11
                pt2 = [i+1, j, image[i+1, j]]           # Q21
                pt3 = [i, j+1, image[i, j+1]]           # Q12
                pt4 = [i+1, j+1, image[i+1, j+1]]       # Q22
                unknown = [(x/fx), (y/fy)]
                result = intrp.bilinear_interpolation(pt1, pt2, pt3, pt4, unknown)
                new_image[x][y] = result

        return new_image
Exemplo n.º 2
0
    def bilinear_interpolation(self, image, fx, fy):
        """resizes an image using bilinear interpolation approximation for resampling
        image: the image to be resampled
        fx: scale along x direction (eg. 0.5, 1.5, 2.5)
        fx: scale along y direction (eg. 0.5, 1.5, 2.5)
        returns a resized image based on the bilinear interpolation method
        """

        # Write your code for bilinear interpolation here
        originalImageRows, originalImageCols = image.shape
        print("Original Image Shape:  %s" % image.shape[0], image.shape[1])

        obj = inter.interpolation()

        fxscale = float(fx)
        fyscale = float(fy)

        # shape return tuple (0: row, 1: col)
        newImagerows = math.floor(originalImageRows * fyscale)
        newImagecols = math.floor(originalImageCols * fxscale)

        newImage = np.empty([newImagerows, newImagecols], dtype=np.uint8)
        print("Scaled New Image Shape:  %s" % newImage.shape[0],
              newImage.shape[1])

        for y in range(0, newImagerows):
            for x in range(0, newImagecols):
                dx = x / fxscale
                dy = y / fyscale

                leftX = math.floor(dx)
                rightX = math.ceil(dx)  #leftX --dx-- rightX
                topY = math.floor(dy)
                bottomY = math.ceil(dy)  #topY
                #|
                #dy
                #|
                #bottomY
                if rightX >= originalImageCols:
                    rightX = originalImageCols - 1
                if bottomY >= originalImageRows:
                    bottomY = originalImageRows - 1
                    #          _C O L S__
                    #           leftX  dX     rightX
                q11 = image[topY, leftX]  #R    topY| q11    r1    q21
                q12 = image[bottomY, leftX]  #O      dY|        P
                q21 = image[topY, rightX]  #W        |
                q22 = image[bottomY, rightX]  #S bottomY| q12    r2    q22

                newImage[y, x] = obj.bilinear_interpolation(
                    (leftX, q11, q12), (rightX, q21, q22), bottomY, topY,
                    (dx, dy))

        return newImage
Exemplo n.º 3
0
    def nearest_neighbor(self, image, fx, fy):
        """resizes an image using bilinear interpolation approximation for resampling
        image: the image to be resampled
        fx: scale along x direction (eg. 0.5, 1.5, 2.5)
        fx: scale along y direction (eg. 0.5, 1.5, 2.5)
        returns a resized image based on the nearest neighbor interpolation method
        """

        #Write your code for nearest neighbor interpolation here
        originalImageRows, originalImagecols = image.shape
        print("Original Image Shape:  %s" % image.shape[0], image.shape[1])

        fxscale = float(fx)
        fyscale = float(fy)

        newImageRows = math.floor(originalImageRows * fyscale)
        newImageCols = math.floor(originalImagecols * fxscale)

        newImage = np.empty([newImageRows, newImageCols], dtype=np.uint8)
        print("Original Image Shape:  %s" % newImage.shape[0],
              newImage.shape[1])

        obj = inter.interpolation()

        for y in range(0, newImageRows):
            for x in range(0, newImageCols):
                dx = x / fxscale
                dy = y / fyscale
                rightX = math.ceil(dx)
                leftX = math.floor(dx)

                if math.ceil(dy) < originalImageRows:
                    dy = round(dy)
                else:
                    dy = math.floor(dy)

                if rightX < originalImagecols:
                    pix = obj.linear_interpolation((leftX, image[dy, leftX]),
                                                   (rightX, image[dy, rightX]),
                                                   dx)
                else:
                    pix = image[dy, leftX]

                newImage[y, x] = pix

        return newImage
Exemplo n.º 4
0
    def bilinear_interpolation(self, image, fx, fy):
        """resizes an image using bilinear interpolation approximation for resampling
        image: the image to be resampled
        fx: scale along x direction (eg. 0.5, 1.5, 2.5)
        fx: scale along y direction (eg. 0.5, 1.5, 2.5)
        returns a resized image based on the bilinear interpolation method
        """

        # Write your code for bilinear interpolation here
        shape = image.shape
        print(shape)
        numro, numco = image.shape
        numro = math.ceil(numro * fx)
        numco = math.ceil(numco * fy)
        image1 = np.zeros((numro, numco), np.uint8)
        for rowX in range(image1.shape[0]):
            for colY in range(image1.shape[1]):
                #(N1,N2,N3,N4)=image.nearestneighbor4(i,j)
                newx = rowX / fx
                newy = colY / fy
                newx1 = math.floor(newx)
                if newx1 >= image.shape[0]:
                    newx1 = image.shape[0] - 1
                newy1 = math.floor(newy)
                if newy1 >= image.shape[1]:
                    newy1 = image.shape[1] - 1
                newx2 = math.ceil(newx)
                if newx2 >= image.shape[0]:
                    newx2 = image.shape[0] - 1
                newy2 = math.ceil(newy)
                if newy2 >= image.shape[1]:
                    newy2 = image.shape[1] - 1

                N1 = (newx1, newy1, image[newx1, newy1])
                N2 = (newx1, newy2, image[newx1, newy2])
                N3 = (newx2, newy1, image[newx2, newy1])
                N4 = (newx2, newy2, image[newx2, newy2])
                unknown = (newx, newy)
                operator = interpolation.interpolation()
                #imageChange[rowX,colY]= intensity.bilinear_interpolation(N1,N2,N3, N4, unknown)
                image1[rowX, colY] = operator.bilinear_interpolation(
                    N1, N2, N3, N4, unknown)

        return image1
Exemplo n.º 5
0
import numpy as np
import math
##########################################################
from resize import interpolation
interpolation_obj = interpolation.interpolation()


##########################################################
class resample:
    def resize(self, image, fx=None, fy=None, interpolation=None):
        """
        calls the appropriate funciton to resample an image based on the interpolation method
        image: the image to be resampled
        fx: scale along x direction (eg. 0.5, 1.5, 2.5)
        fx: scale along y direction (eg. 0.5, 1.5, 2.5)
        interpolation: method used for interpolation ('either bilinear or nearest_neighbor)
        returns a resized image based on the interpolation method

        """
        if interpolation == 'bilinear':
            return self.bilinear_interpolation(image, float(fx), float(fy))

        elif interpolation == 'nearest_neighbor':
            return self.nearest_neighbor(image, float(fx), float(fy))

    def nearest_neighbor(self, image, fx, fy):
        """
        resizes an image using nearest neighbor approximation for resampling
        image: the image to be resampled
        fx: scale along x direction (eg. 0.5, 1.5, 2.5)
        fx: scale along y direction (eg. 0.5, 1.5, 2.5)
Exemplo n.º 6
0
import numpy
import math
from resize.interpolation import interpolation
interpolate=interpolation()


class resample:

    def resize(self, image, fx = None, fy = None, interpolation = None):
        """calls the appropriate funciton to resample an image based on the interpolation method
        image: the image to be resampled
        fx: scale along x direction (eg. 0.5, 1.5, 2.5)
        fx: scale along y direction (eg. 0.5, 1.5, 2.5)
        interpolation: method used for interpolation ('either bilinear or nearest_neighbor)
        returns a resized image based on the interpolation method
        """

        if interpolation == 'bilinear':
            return self.bilinear_interpolation(image, fx, fy)

        elif interpolation == 'nearest_neighbor':
            return self.nearest_neighbor(image, fx, fy)

    def nearest_neighbor(self, image, fx, fy):
        """resizes an image using bilinear interpolation approximation for resampling
        image: the image to be resampled
        fx: scale along x direction (eg. 0.5, 1.5, 2.5)
        fx: scale along y direction (eg. 0.5, 1.5, 2.5)
        returns a resized image based on the nearest neighbor interpolation method
        """