Beispiel #1
0
def skinDetection(ndata, sdata, K, n_iter, epsilon, theta, img):
    # Skin Color detector
    #
    # INPUT:
    # ndata         : data for non-skin color
    # sdata         : data for skin-color
    # K             : number of modes
    # n_iter        : number of iterations
    # epsilon       : regularization parameter
    # theta         : threshold
    # img           : input image
    #
    # OUTPUT:
    # result        : Result of the detector for every image pixel
    n_weight, n_mean, n_covariance = estGaussMixEM(ndata, K, n_iter, epsilon)
    s_weight, s_mean, s_covariance = estGaussMixEM(sdata, K, n_iter, epsilon)

    result = img.copy()

    for i in range(result.shape[0]):
        for j in range(result.shape[1]):
            px_given_n = np.exp(
                getLogLikelihood(n_mean, n_weight, n_covariance, result[i, j]))
            px_given_s = np.exp(
                getLogLikelihood(s_mean, s_weight, s_covariance, result[i, j]))
            if px_given_s / px_given_n > theta:
                result[i, j] = [1, 1, 1]
            else:
                result[i, j] = [0, 0, 0]

    return result
Beispiel #2
0
def skinDetection(ndata, sdata, K, n_iter, epsilon, theta, img):
    # Skin Color detector
    #
    # INPUT:
    # ndata         : data for non-skin color
    # sdata         : data for skin-color
    # K             : number of modes
    # n_iter        : number of iterations
    # epsilon       : regularization parameter
    # theta         : threshold
    # img           : input image
    #
    # OUTPUT:
    # result        : Result of the detector for every image pixel

    #####Insert your code here for subtask 1g#####
    skin_data = estGaussMixEM(sdata, K, n_iter, epsilon)
    nons_data = estGaussMixEM(ndata, K, n_iter, epsilon)
    result = np.zeros((img.shape[0], img.shape[1]))
    for i in range(img.shape[0]):
        for j in range(img.shape[1]):
            rbg = img[i, j]
            pskin = getLikelihood(skin_data[1], skin_data[0], skin_data[2],
                                  rbg)
            pnonskin = getLikelihood(nons_data[1], nons_data[0], nons_data[2],
                                     rbg)
            judge = pskin / pnonskin
            if judge > theta:
                result[i, j] = 1
            else:
                result[i, j] = 0

    return result
Beispiel #3
0
def skinDetection(ndata, sdata, K, n_iter, epsilon, theta, img):
    # Skin Color detector
    #
    # INPUT:
    # ndata         : data for non-skin color
    # sdata         : data for skin-color
    # K             : number of modes
    # n_iter        : number of iterations
    # epsilon       : regularization parameter
    # theta         : threshold
    # img           : input image
    #
    # OUTPUT:
    # result        : Result of the detector for every image pixel

    #####Insert your code here for subtask 1g#####
    nweights, nmeans, ncovariances = estGaussMixEM(ndata, K, n_iter, epsilon)
    sweights, smeans, scovariances = estGaussMixEM(sdata, K, n_iter, epsilon)
    result = np.zeros(img.shape)
    for i in range(len(img)):
        for j in range(len(img[0])):
            nlhd = 0
            slhd = 0
            for k in range(K):
                nlhd = nlhd + nweights[k] * multiGauss(img[i, j], nmeans[k],
                                                       ncovariances[:, :, k])
                slhd = slhd + sweights[k] * multiGauss(img[i, j], smeans[k],
                                                       scovariances[:, :, k])
            # nlhd=getLogLikelihood(nmeans, nweights, ncovariances, img[i,j])
            # slhd=getLogLikelihood(smeans, sweights, scovariances, img[i,j])
            if slhd / nlhd > theta:
                result[i, j] = [255, 255, 255]

    return result
Beispiel #4
0
def skinDetectionTae(ndata, sdata, K, n_iter, epsilon, theta, img):
    # Skin Color detector
    #
    # INPUT:
    # ndata         : data for non-skin color
    # sdata         : data for skin-color
    # K             : number of modes
    # n_iter        : number of iterations
    # epsilon       : regularization parameter
    # theta         : threshold
    # img           : input image
    #
    # OUTPUT:
    # result        : Result of the detector for every image pixel

    height, width, _ = img.shape

    #data from the skin data
    s_weights, s_means, s_covariances = estGaussMixEM(sdata, K, n_iter, epsilon)

    #data from the non skin data
    ns_weights, ns_means, ns_covariances = estGaussMixEM(ndata, K, n_iter, epsilon)

    skin_array = np.ndarray((height, width))
    non_skin_array = np.ndarray((height, width))

    for h in range(height):

        for w in range(width):

            skin_array[h,w] = np.exp(
                                    getLogLikelihood(s_means,
                                                     s_weights,
                                                     s_covariances,
                                                     np.array([img[h, w, 0],
                                                               img[h, w, 1],
                                                               img[h, w, 2]])
                                                     )
                                    )

            non_skin_array[h, w] = np.exp(
                                    getLogLikelihood(ns_means,
                                                     ns_weights,
                                                     ns_covariances,
                                                     np.array([img[h, w, 0],
                                                               img[h, w, 1],
                                                               img[h, w, 2]])
                                                    )
                                    )

    result = skin_array / non_skin_array
    result = np.where(result > theta, 1, 0)

    #####Insert your code here for subtask 1g#####
    return result
Beispiel #5
0
def skinDetection(ndata, sdata, K, n_iter, epsilon, theta, img):
    # Skin Color detector
    #
    # INPUT:
    # ndata         : data for non-skin color
    # sdata         : data for skin-color
    # K             : number of modes = skin_K = 3
    # n_iter        : number of iterations
    # epsilon       : regularization parameter
    # theta         : threshold
    # img           : input image
    #
    # OUTPUT:
    # result        : Result of the detector for every image pixel

    n_weights, n_means, n_covariances = estGaussMixEM(ndata, K, n_iter,
                                                      epsilon)
    s_weights, s_means, s_covariances = estGaussMixEM(sdata, K, n_iter,
                                                      epsilon)

    s_N = ndata.shape[0]
    n_N = sdata.shape[0]
    img_row = img.shape[0]
    img_col = img.shape[1]
    D = ndata.shape[1]
    s_pdf = np.zeros([img_row, img_col])
    n_pdf = np.zeros([img_row, img_col])
    result = np.zeros([img_row, img_col, D])

    #print('skin weights:\n', s_weights, 'skin means:\n', s_means, 'skin covariances:\n', s_covariances)
    #print('non-skin weights:\n', n_weights, 'non-skin means:\n', n_means, 'non-skin covariances:\n', n_covariances)

    for row in range(img_row):
        for col in range(img_col):
            s_pdf[row][col] = getPdf(s_means, s_covariances, img[row][col], K,
                                     D)
            n_pdf[row][col] = getPdf(n_means, n_covariances, img[row][col], K,
                                     D)
            #print('s_pdf[row][col]:\n', s_pdf[row][col])
            #print('n_pdf[row][col]:\n', n_pdf[row][col])
            if (s_pdf[row][col] - 2 * n_pdf[row][col]) > theta:
                # this pixel probably has skin color, so categorize this as white
                result[row][col] = [255, 255, 255]
            else:
                # this pixel probably does NOT have skin color, so categorize this as black
                result[row][col] = [0, 0, 0]

    #####Insert your code here for subtask 1g#####
    return result
Beispiel #6
0
def skinDetection(ndata, sdata, K, n_iter, epsilon, theta, img):
    # Skin Color detector
    #
    # INPUT:
    # ndata         : data for non-skin color
    # sdata         : data for skin-color
    # K             : number of modes
    # n_iter        : number of iterations
    # epsilon       : regularization parameter
    # theta         : threshold
    # img           : input image
    #
    # OUTPUT:
    # result        : Result of the detector for every image pixel

    #####Start Subtask 1g#####
    print('creating GMM for non-skin')
    weight_nonskin, means_nonskin, cov_nonskin = estGaussMixEM(ndata, K, n_iter, epsilon)
    print('GMM for non-skin completed')
    print('creating GMM for skin')
    weight_skin, means_skin, cov_skin = estGaussMixEM(sdata, K, n_iter, epsilon)
    print('GMM for skin completed')

    height, width, _ = img.shape

    noSkin = np.ndarray((height, width))
    skin = np.ndarray((height, width))

    for h in range(height):
        for w in range(width):
            noSkin[h, w] = np.exp(
                getLogLikelihood(means_nonskin, weight_nonskin, cov_nonskin, np.array([img[h, w, 0], img[h, w, 1],
                                                                                       img[h, w, 2]])))
            skin[h, w] = np.exp(
                getLogLikelihood(means_skin, weight_skin, cov_skin, np.array([img[h, w, 0], img[h, w, 1],
                                                                              img[h, w, 2]])))


    # calculate ration and threshold
    result = skin / noSkin
    result = np.where(result > theta, 1, 0)
    #####End Subtask#####
    return result
Beispiel #7
0
def skinDetection(ndata, sdata, K, n_iter, epsilon, theta, img):
    # Skin Color detector
    #
    # INPUT:
    # ndata         : data for non-skin color
    # sdata         : data for skin-color
    # K             : number of modes
    # n_iter        : number of iterations
    # epsilon       : regularization parameter
    # theta         : threshold
    # img           : input image
    #
    # OUTPUT:
    # result        : Result of the detector for every image pixel

    #Datasets of RGB pixel values for skin (sdata) and non-skin (ndata) regions 	for a skin detection experiment. First, train a Gaussian Mixture Model for 		each dataset. Based on these two Mixture Models, the pixels is classified 	in the provided image(faces.png) according to the Likelihood Ratio.
    
    print('creating GMM for non-skin')
    weight_nonskin, means_nonskin, cov_nonskin = estGaussMixEM(ndata, K, n_iter, epsilon)
    print('GMM for non-skin completed')
    print('creating GMM for skin')
    weight_skin, means_skin, cov_skin = estGaussMixEM(sdata, K, n_iter, epsilon)
    print('GMM for skin completed')

    height, width, _ = img.shape

    noSkin = np.ndarray((height, width))
    skin = np.ndarray((height, width))

    for h in range(height):
        for w in range(width):
            noSkin[h, w] = np.exp(
                getLogLikelihood(means_nonskin, weight_nonskin, cov_nonskin, np.array([img[h, w, 0], img[h, w, 1],
                                                                                       img[h, w, 2]])))
            skin[h, w] = np.exp(
                getLogLikelihood(means_skin, weight_skin, cov_skin, np.array([img[h, w, 0], img[h, w, 1],
                                                                              img[h, w, 2]])))


    # calculate ration and threshold
    result = skin / noSkin
    result = np.where(result > theta, 1, 0)
    return result
Beispiel #8
0
    [0.064658231773354, 0.935324018684456]
]
for idx in range(3):
    covariance = regularize_cov(testparams[2, 0][:, :, idx], 0.01)
    absdiff = abs(covariance - regularized_cov[:, :, idx])
    print('Sum of difference of covariances: {0}\n'.format(np.sum(absdiff)))


# compute GMM on all 3 datasets
print('\n')
print('(f) evaluating EM for GMM on all datasets')
for idx in range(3):
    print('evaluating on dataset {0}\n'.format(idx+1))

    # compute GMM
    weights, means, covariances = estGaussMixEM(data[idx], K, n_iter, epsilon)

    # plot result
    plt.subplot()
    plotModes(np.transpose(means), covariances, data[idx])
    plt.title('Data {0}'.format(idx+1))
    plt.show()


# uncomment following lines to generate the result
# for different number of modes k plot the log likelihood for data3
num = 14
logLikelihood = np.zeros(num)
for k in range(num):
    # compute GMM
    weights, means, covariances = estGaussMixEM(data[2], k+1, n_iter, epsilon)