Basic 1(py01)

import math

import numpy as np

import cv2

import sys

# # Implement the functions below.

def extractRed(image):

    “”” Returns the red channel of the input image.

    “””

    new_image = []

    w, h, p = image.shape

    for i in range(0, w):

        temp = []

        

        for j in range (0, h):

            # get green value at each pixel

            r = image[i][j][2]

            temp.append(r)

            

        new_image.append(temp)

    

    a = np.asarray(new_image)

    return a

def extractGreen(image):

    “”” Returns the green channel of the input image.

    “””

    new_image = []

    w, h, p = image.shape

    for i in range(0, w):

        temp = []

        

        for j in range (0, h):

            # get green value at each pixel

            g = image[i][j][1]

            temp.append(g)

            

        new_image.append(temp)

    

    a = np.asarray(new_image)

    return a

def extractBlue(image):

    “”” Returns the blue channel of the input image.

    “””

    new_image = []

    w, h, p = image.shape

    for i in range(0, w):

        temp = []

        

        for j in range (0, h):

            # get green value at each pixel

            b = image[i][j][0]

            temp.append(b)

            

        new_image.append(temp)

    

    a = np.asarray(new_image)

    return a   

def swapGreenBlue(image):

    “”” Returns an image with the green and blue channels of the input image swapped.

    “””

    

    w, h, p = image.shape

    

    

    for i in range(0, w):

        for j in range (0, h):

            # get r,g,b values at each pixel.

            g = image[i][j][0]

            b = image[i][j][1]

            image[i][j][0] = b

            image[i][j][1] = g

            

    return image

def copyPasteMiddle(src, dst, shape):

    “”” Copies the middle region of size shape from src to the middle of dst.

        Note: Assumes that src and dst are monochrome images, i.e. 2d arrays.

        Note: Where ‘middle’ is ambiguous because of any difference in the oddness

        or evenness of the size of the copied region and the image size, the function

        rounds downwards.  E.g. in copying a ` = (1,1) from a src image of size (2,2)

        into an dst image of size (3,3), the function copies the range [0:1,0:1] of

        the src into the range [1:2,1:2] of the dst.

    “””

    

    image1=mono1

    image2=mpnp2

    center1 = src.shape / 2+1;

    center2= dst.shape / 2 +1;

    im1(center(1) – 50: im2_center(1) + 50, im2_center – 50: im2_center + 50) = im1(im1_center(1) – 50: im1_center(1) + 50, im1_center – 50: im1_center + 50);

   

    print shape

def imageStats(image):

    “”” Returns the tuple (min,max,mean,stddev) of statistics for the input monochrome image.

    “””

    min_green = np.amin(image)

    max_green = np.amax(image)

    mean_green = image.mean()

    stddev_green = np.std(image)

    return min_green, max_green, mean_green, stddev_green

    

def normalized(image, scalingFactor):

    “””Returns an image with the same mean as the original but with values

    scaled about the mean so as to have a standard deviation of stddev.

    Note: This function makes no defense against the creation

    of out-of-range pixel values.  Consider converting the input image to

    a float64 type before passing in an image.

    “””

    #image = image.astype(np.float64)

    

    mean = image.mean()

    stddev_green = np.std(image)

    

    image = image – mean

    image = image / stddev_green

    image = image * scalingFactor

    image = image + mean

    return image

    

    

def shiftImageLeft(image, shift):

    “”” Outputs the input monochrome image shifted shift pixels to the left.

        

        The returned image has the same shape as the original with

        the BORDER_REPLICATE rule to fill-in missing values.  See

        

        http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/copyMakeBorder/copyMakeBorder.html?highlight=copy

        for further explanation.

    “””

    replicate = cv2.copyMakeBorder(image, 0, 0, 0, shift, cv2.BORDER_REPLICATE)

    shifted = replicate[:, shift:]

    for i in range (0, shift):

        shifted [:, i]=0

    return shifted

def differenceImage(img1, img2):

    “””Returns the normalized value of the difference between the two input images.

    “””

    #image = image.astype(np.float64)

    

    w, h = img1.shape

    for w in xrange(h):

        img1[:,w]/= abs(img1[:,w]).max()

   

    img3=img1-img2

    return img3

def addNoise(image, channel, sigma):

    “”” Returns a copy of the input color image with Gaussian noise added to

    channel (0-2).  The parameter sigma controls the standard deviation of the noise.

    Note: This function makes no defense against the creation

    of out-of-range pixel values.  Consider converting the input image to

    a float64 type before passing in an image.

    “””

    #image = image.astype(np.float64)

    new_image = []

    w, h, p = image.shape

    

    for i in range(0, w):

        temp = []

        

        for j in range (0, h):

            # get green value at each pixel

            g = image[i][j][channel]

            temp.append(g)

            

        new_image.append(temp)

    a = np.asarray(new_image)

    

    mean =a.mean()

    

    gu = np.random.normal(mean, sigma,a.shape)

    

    signal = a + gu

    for i in range(0, w):        

        for j in range (0, h):

            # get green value at each pixel

            image[i][j][channel] = signal[i][j]

            

   

    return image