Exemple #1
0
def improve(name1, name2, i):
    data1 = op.read_pgm(name1)
    [row, column] = data1[1]
    array1 = data1[0]
    image1 = array1.reshape(row, column)

    data2 = op.read_pgm(name2)
    [row, column] = data2[1]
    array2 = data2[0]
    image2 = array2.reshape(row, column)

    for a in range(row):
        for b in range(column):
            if image2[a][b] == i:
                image1[a][b] == i

    return (image1)
Exemple #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)
Exemple #3
0
def increase_resolution(name, t):
    data = op.read_pgm(name)
    [row, column] = data[1]
    array = data[0]
    image = array.reshape(row, column)

    new_image = np.zeros((row * t, column * t))
    # the nearest neighbour
    for i in range(0, row):
        for j in range(0, column):

            for m in range(i * t, i * t + t):
                for n in range(j * t, j * t + t):
                    new_image[m][n] = image[i][j]

    return (new_image)
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,new_column])  #the array to contain the after smooth array

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

    start=time.time()
    d = {x: 0 for x in range(0, 4)}
    d[0]=n*n
    for j in range (int((n-1)/2),new_row-n+1):
        for k in range(1, new_column - n):
            for m in range (0,n):
                d[new_image[j + c][k - 1]] -=1
                d[new_image[j - c][k - 1]] -= 1
                d[new_image[j][k - 1]] -= 1
                d[new_image[j + c][k + n - 1]] +=1
                d[new_image[j - c][k + n - 1]] += 1
                d[new_image[j][k + n - 1]] += 1
                # for x in range(0, n):
                #     if x == new_image[j + int((n-1)/2)][k - 1]:
                #         d[x] -= 1
                #     if x == new_image[j - int((n-1)/2)][k - 1]:
                #         d[x] -= 1
                #     if x == new_image[j ][k - 1]:
                #         d[x] -= 1
                #     if x == new_image[j ][k + n - 1]:
                #         d[x] += 1
                #     if x == new_image[j + int((n-1)/2)][k + n - 1]:
                #         d[x] += 1
                #     if x == new_image[j - int((n-1)/2)][k + n - 1]:
                #         d[x] += 1
            sum = 0
            for i in range(0, 3):
                if sum + d[i] > (n * n) / 2:
                    med = i
                else:
                    sum = d[i]

            med = int(med)
            # print(med)
            after_smooth[j][k] = med


    elapsed=(time.time()-start)
    print('time used',elapsed)
    # file = open('2Dcheck\\Afterimf2CircleWithTriangle.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)
Exemple #5
0
def three_d(a, b, t, n):
    L = 0
    three_d_list = []
    head = 'data\\v1_00'
    tail = '.pgm'
    image_length = L
    k = 0
    m = 0
    # buile 3D array
    for x in range(a, int(b + 1)):
        if x < 10:
            path = head + '00' + str(x) + tail  # the pgm path when small than 10
            if os.path.exists(path):
                data = op.read_pgm(path)
                image = data[0]
                [image_height, image_width] = data[1]
                map = image.reshape(image_height, image_width)
                three_d_list.append(map)

        elif x >= 10 and x < 100:
            path = head + '0' + str(x) + tail  # the pgm path when small than 100 but bigger and equal to 10
            if os.path.exists(path):
                data = op.read_pgm(path)
                image = data[0]
                [image_height, image_width] = data[1]
                map = image.reshape(image_height, image_width)
                three_d_list.append(map)

        else:
            path = head + str(x) + tail  # the pgm path when bigger than 100
            if os.path.exists(path):
                data = op.read_pgm(path)
                image = data[0]
                [image_height, image_width] = data[1]
                map = image.reshape(image_height, image_width)
                three_d_list.append(map)

    three_d_array = np.array(three_d_list)
    [z,y,x] = three_d_array.shape

    # increase resolution in 3D
    Z = z * t
    Y = y * t
    X = x * t
    increase_resolution_array = np.zeros(((Z,Y,X)))
    for i in range (0,z):
        for j in range (0,y):
            for m in range (0,x):

                for p in range (i * t,i * t  + t):
                    for q in range (j * t,j * t + t):
                        for o in range (m * t,m * t + t):
                            increase_resolution_array[p][q][o] = three_d_array[i][j][m]

    # use the padding to extend the edge of the array in 3D
    c = int((n - 1) / 2)
    new_three_d_array = np.zeros([Z + 2 * c, Y + 2 * c, X + 2 * c])
    for i in range(c, Z + c):
        for j in range(c, Y + c):
            for m in range(c, X + c):
                new_three_d_array[i][j][m] = increase_resolution_array[i - c][j - c][m - c]

    for m in range (c, X + c):
        for i in range(c, Z + c):
            for j in range(0, c):
                new_three_d_array[i][j][m] = increase_resolution_array[i - c][0][m - c]
    for m in range (c, X + c):
        for i in range(c, Z + c):
            for j in range(Y + c, Y + 2 * c):
                new_three_d_array[i][j][m] = increase_resolution_array[i - c][Y - 1][m - c]
    for m in range (c, Y + c):
        for i in range(c, Z + c):
            for j in range(0,c):
                new_three_d_array[i][m][j] = increase_resolution_array[i - c][m - c][0]
    for m in range (c, Y + c):
        for i in range(c, Z + c):
            for j in range(X + c, X + 2 * c):
                new_three_d_array[i][m][j] = increase_resolution_array[i - c][m - c][X - 1]
    for m in range (c, X + c):
        for i in range(c, Y + c):
            for j in range(0, c):
                new_three_d_array[j][i][m] = increase_resolution_array[0][i - c][m - c]
    for m in range (c, X + c):
        for i in range(c, Y + c):
            for j in range(Z + c, Z + 2 * c):
                new_three_d_array[j][i][m] = increase_resolution_array[Z - 1][i - c][m - c]

    return (new_three_d_array.shape, new_three_d_array, three_d_array)
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)
Exemple #7
0
def main():
    a = int(input('the begin pgm file:'))
    b = int(input('the last pgm file:'))
    t = int(input('the number of times to increase the width and height:'))
    n = int(input('the size of filter:'))
    axis = str(input('the constant axis you choose is:'))

    # gs.get_slice(a,b,t,n,axis)

    head = 'test\\'
    name1 = head + str(axis) + 'AfterSmooth.pgm'
    name2 = head + str(axis) + 'BeforeSmooth.pgm'
    name5 = head + str(axis) + 'add the miss pixel'
    data1 = op.read_pgm(name1)
    data2 = op.read_pgm(name2)
    array1 = data1[0]
    list1 = array1.tolist()
    array2 = data2[0]
    [row, column] = data2[1]
    image2 = array2.reshape(row, column)
    list2 = array2.tolist()
    #get the pixels' value and the frequency of pixels occurence
    d1 = {}
    for i in list1:
        if i not in d1.keys():
            d1[i] = list1.count(i)
    print(d1)

    d2 = {}
    for i in list2:
        if i not in d2.keys():
            d2[i] = list2.count(i)
    print(d2)
    # check pixels
    # if some pixels' value are missed, then put the missed pixel in the before smooth figure into the after smooth figure
    if len(d1) < len(d2):
      for i in d1.keys():
          a = d1[i] - d2[i]
          b = d2[i]
          if a / b > 0.1 or i == 51:
              print('the pixel value need to improve is: ', i)
              B = np.zeros([row, column])
              for p in range(row):
                  for q in range(column):
                      if image2[p][q] == i:
                          B[p][q] = i
              plt.title('the pixel value need to improve is: ' + str(i))
              plt.imshow(B)
              plt.show()

              name = 'test\\' + str(i) + '.pgm'
              file = open(name, 'w')
              [m, k] = B.shape
              max = 85
              file.write('P2\n' + str(k) + ' ' + str(m) + '\n' + str(max) + '\n')
              for p in range(0, m):
                  for q in range(0, k):
                      file.write(str(int(B[p][q])) + '\n')
              file.close()

              # calculate the weight
              G = we.gentic_weight(n, name)

              # smooth the missing pixels by using weight median filter
              name3 = wmf.weight_median_filter(n, G, name, i)

              # combine two figure
              final_image2 = cb.improve(name1, name3, i)

              plt.title('improve smooth figure')
              plt.imshow(final_image2)
              plt.show()

              file = open('test\\improve smooth figure.pgm', 'w')
              [m, k] = final_image2.shape
              max = 85
              file.write('P2\n' + str(k) + ' ' + str(m) + '\n' + str(max) + '\n')
              for p in range(0, m):
                  for q in range(0, k):
                      file.write(str(int(final_image2[p][q])) + '\n')
              file.close()
      for j in d2.keys():
          if j not in d1.keys():
              A = np.zeros([row, column])
              for p in range(row):
                  for q in range(column):
                      if image2[p][q] == j:
                          A[p][q] = j
              plt.title('the missing pixel: ' + str(j))
              plt.imshow(A)
              plt.show()

              name4 = 'test\\' + str(j) + '.pgm'
              file = open(name4, 'w')
              [m, k] = image2.shape
              max = 85
              file.write('P2\n' + str(k) + ' ' + str(m) + '\n' + str(max) + '\n')
              for p in range(0, m):
                  for q in range(0, k):
                      file.write(str(int(A[p][q])) + '\n')
              file.close()
              # combine two figure
              final_image2 = cb.improve('test\\improve smooth figure.pgm', name4, j)

              plt.title('the final after smooth figure')
              plt.imshow(final_image2)
              plt.show()

              file = open('test\\final after smooth figure.pgm', 'w')
              [m, k] = final_image2.shape
              max = 85
              file.write('P2\n' + str(k) + ' ' + str(m) + '\n' + str(max) + '\n')
              for p in range(0, m):
                  for q in range(0, k):
                      file.write(str(int(final_image2[p][q])) + '\n')
              file.close()
    else:
        # if the value's frequency of occurence exist large differences between before smooth figure and after smooth figure
        for i in d1.keys():
            a = d1[i] - d2[i]
            b = d2[i]
            if a / b > 0.1 or i == 51:
                print('the pixel value need to improve is: ', i)
                B = np.zeros([row, column])
                for p in range(row):
                    for q in range(column):
                        if image2[p][q] == i:
                            B[p][q] = i
                plt.title('the pixel value need to improve is: ' + str(i))
                plt.imshow(B)
                plt.show()

                name = 'test\\' + str(i) + '.pgm'
                file = open(name, 'w')
                [m, k] = B.shape
                max = 85
                file.write('P2\n' + str(k) + ' ' + str(m) + '\n' + str(max) + '\n')
                for p in range(0, m):
                    for q in range(0, k):
                        file.write(str(int(B[p][q])) + '\n')
                file.close()

                # calculate the weight
                G = we.gentic_weight(n, name)

                # smooth the missing pixels by using weight median filter
                name3 = wmf.weight_median_filter(n, G, name, i)

                # combine two figure
                final_image2 = cb.improve(name1, name3, i)

                plt.title('the final after smooth figure')
                plt.imshow(final_image2)
                plt.show()

                file = open('test\\final after smooth figure.pgm', 'w')
                [m, k] = final_image2.shape
                max = 85
                file.write('P2\n' + str(k) + ' ' + str(m) + '\n' + str(max) + '\n')
                for p in range(0, m):
                    for q in range(0, k):
                        file.write(str(int(final_image2[p][q])) + '\n')
                file.close()