def find_most_frequency(L):
    d = {}

    for i in L:
        if i not in d.keys():
            d[i] = L.count(i)
    A = sorted(d.items(), key=operator.itemgetter(1), reverse=True)

    if len(A) == 1:
        max = A[0][0]
        fre = A[0][1]
    elif A[0][1] == A[1][1]:
        max = fm.find_median(L)
        fre = A[0][1]
    else:
        max = A[0][0]
        fre = A[0][1]

    # print(d.items())
    # print(A)
    # print(len(A))
    # print(A[0])
    # print(A[0][1])
    # print(max)
    return (max, fre)
Esempio n. 2
0
def fitness_test(G, n, name):
    data1 = op.read_pgm(name)
    [row, column] = data1[1]
    array1 = data1[0]
    image1 = array1.reshape(row, column)

    data2 = op.read_pgm(name)
    [row, column] = data2[1]
    array2 = data2[0]
    image2 = array2.reshape(row, column)
    after_smooth = np.zeros([row, column])
    C = np.zeros(n * n)
    c = int((n - 1) / 2)
    for m in range(row - n):
        for k in range(column - n):
            q = 0
            filter_array = np.zeros([n * n])
            for i in range(m, m + n):
                for j in range(k, k + n):
                    filter_array[q] = int(image2[i][j])
                    q += 1
            filter_list = filter_array.tolist()
            for x in range(len(filter_list)):
                C[x] = G[x] * filter_list[x]
            Q = C.tolist()
            K = fmf.find_most_frequency(Q)
            if K[0] == 0 and K[1] > (n * n - 1) / 2:
                M = 0
            else:
                med = fd.find_median(C)
                for y in range(n * n):
                    if C[y] == med:
                        M = filter_list[y]
            after_smooth[m + c][k + c] = M

    total = row * column
    same = 0
    for a in range(row):
        for b in range(column):
            if after_smooth[a][b] == image1[a][b]:
                same += 1

    fitness = float(same / total)

    return (fitness)
Esempio n. 3
0
def find_most_frequency(L):
    d = {}
    for i in L:
        if i not in d.keys():
            d[i] = L.count(i)  # create a dictionary to store all the value
    A = sorted(
        d.items(), key=operator.itemgetter(1), reverse=True
    )  # sort the value from max to min according to the frequency of occurrence

    if len(A) == 1:
        max = A[0][0]
        fre = A[0][1]
    # if two maximum value's frequency of occurrence is equal, then use the median value to replace the maximum value
    elif A[0][1] == A[1][1]:
        max = fm.find_median(L)
        fre = A[0][1]
    else:
        max = A[0][0]
        fre = A[0][1]

    return (max, fre)
Esempio n. 4
0
def three_d_median_filter(a, b, t, n):
    data = tda.three_d(a, b, t, n)
    three_d_array = data[1]
    [z, y, x] = data[0]

    start = time.time()
    filter_three_d_array = np.zeros([n * n * n])
    c = int(((n - 1) / 2))
    three_d_after_smooth = np.zeros([z, y, x])
    for d in range(z - n):
        for e in range(y - n):
            for f in range(x - n):
                q = 0
                # build the filter
                filter_three_d_array = np.zeros([n * n * n])
                for m in range(d, d + n):
                    for j in range(e, e + n):
                        for i in range(f, f + n):
                            filter_three_d_array[q] = three_d_array[m][j][i]
                            q += 1
                filter_three_d_list = filter_three_d_array.tolist()
                Z = fmf.find_most_frequency(filter_three_d_list)
                z = Z[1]
                # if the more than half pixels have the same value, the median is equal to the highest frequency of occurrence value
                if z > (n * n * n - 1) / 2:
                    med = Z[0]
                else:
                    med = fd.find_median(filter_three_d_array)

                med = int(med)
                three_d_after_smooth[d + c - 1][e + c - 1][f + c - 1] = med

    elapsed = (time.time() - start)
    print('time used', elapsed)

    return (three_d_after_smooth.shape, three_d_after_smooth)
 def testFindMedianWithDuplicateAndNegativeValues(self):
     numbers = ["-1"] * 100 + range(50)
     self.create_input_file(numbers)
     expected_median = -1
     actual_median = find_median.find_median(self.test_filename)
     self.assertEqual(expected_median, actual_median)
 def testFindMedianForSmallSet(self):
     self.create_input_file(range(50))
     expected_median = 24
     actual_median = find_median.find_median(self.test_filename)
     self.assertEqual(expected_median, actual_median)
 def testFindMedianForUnEqualBucketSet(self):
     self.create_input_file(range(502))
     expected_median = 250
     actual_median = find_median.find_median(self.test_filename)
     self.assertEqual(expected_median, actual_median)
Esempio n. 8
0
 def test_find_median(self):
   self.assertEqual(find_median.find_median([0, 1, 2, 4, 6, 5, 3]), 3)
def two_d_improve_median_filter(name):
    t = int(input('the number of times to increase the width and height:'))
    n = int(input('the size of filter:'))
    k = int((n - 1) / 2)

    # A=[9,1,2,3,4,5,6,7,8,5,6,7]
    # array_A=np.array(A)
    # image=array_A.reshape(4,3)
    # print(image)
    data = op.read_pgm(name)
    [row, column] = data[1]
    list = data[0]
    array = np.array(list)
    image = array.reshape(row, column)
    # plt.imshow(image)
    # plt.show()

    new_image = bo.border(name, n, t)

    # plt.imshow(new_image)
    # plt.show()

    # [row,column]=image.shape  #get the original image shape
    [new_row, new_column] = new_image.shape  #get the newimage shape

    c = int(((n - 1) / 2))
    after_smooth = np.zeros([new_row - c, new_column - c
                             ])  #the array to contain the after smooth array

    filter_array = np.zeros([n * n])
    # print(filter_array)

    start = time.time()
    for m in range(new_row - n):
        for k in range(new_column - n):
            q = 0
            filter_array = np.zeros([n * n])
            for i in range(m, m + n):
                for j in range(k, k + n):
                    filter_array[q] = int(new_image[i][j])
                    q += 1

            # # normal
            # med = fd.find_median(filter_array)
            # med = int(med)
            # # print(med)
            # after_smooth[m][k] = med

            # use the find most frequency
            filter_list = filter_array.tolist()
            Z = fmf.find_most_frequency(filter_list)
            z = Z[1]
            if z > (n * n - 1) / 2:
                med = Z[0]
            else:
                med = fd.find_median(filter_array)
            med = int(med)
            # print(med)
            after_smooth[m][k] = med

            # # use the counter
            # filter_list = filter_array.tolist()
            # Max=Counter(filter_list).most_common(1)
            # z=Max[0][1]
            # if z > (n * n - 1) / 2:
            #     med = Max[0][0]
            # else:
            #     med = fd.find_median(filter_array)
            # med = int(med)
            # # print(med)
            # after_smooth[m][k] = med
    elapsed = (time.time() - start)
    print('time used', elapsed)
    # file = open('2Dcheck\\AfterimfCircleWithTriangle1.pgm', 'w')
    # [m, n] = after_smooth.shape
    # max = 2
    # file.write('P2\n' + str(n) + ' ' + str(m) + '\n' + str(max) + '\n')
    # for i in range(0, m):
    #     for j in range(0, n):
    #         file.write(str(int(after_smooth[i][j])) + '\n')
    # file.close()

    plt.imshow(after_smooth)
    plt.show()
    return (after_smooth)