Ejemplo n.º 1
0
def temp_image_folder_resize(input_f, output_f, pixel=(2160, 2160), box=None):
    # pixel = (w, h)
    # box = (x0,y0,x1,y1)

    if not os.path.exists(input_f):
        print('!ERROR! The input_f does not existed!')
        return False
    if not os.path.exists(output_f):
        os.makedirs(output_f)

    path_list = os.listdir(input_f)
    for img_name in path_list:
        img = os.path.join(input_f, img_name)
        img = image_to_gray(img)

        if box is not None:
            img = img[box[1]:box[3], box[0]:box[2]]

        h, w = img.shape[:2]
        if h != pixel[1] or w != pixel[0]:
            img = cv2.resize(img, pixel, interpolation=cv2.INTER_NEAREST)

        to_file = os.path.join(output_f, img_name)
        cv2.imwrite(to_file, img)

    return True
Ejemplo n.º 2
0
def painting_well_segmentation(labels, in_folder, out_folder, files_sort_function, safe_time=5):
    if not os.path.exists(in_folder):
        print('!ERROR! The in_folder does not existed!')
        return False
    # time.sleep(safe_time)
    if not os.path.exists(out_folder):
        # shutil.rmtree(out_folder)
        time.sleep(safe_time)
        os.makedirs(out_folder)
        time.sleep(safe_time)

    img_type = ['jpg', 'png', 'tif']
    offset = 0
    in_files = os.listdir(in_folder)
    in_files = files_sort_function(in_files)
    for f in in_files:
        if f.split('.')[1] in img_type:
            in_img_f = os.path.join(in_folder, f)
            in_img = image_to_gray(in_img_f)
            pixel = in_img.shape[0] * in_img.shape[1]
            this_labels = labels[offset:offset + pixel]
            offset = offset + pixel
            out_file = os.path.join(out_folder, f)
            plt.imsave(out_file, this_labels.reshape(in_img.shape))

    return True
Ejemplo n.º 3
0
def get_folder_negative_film_V3(img1, img2, sub_folder, result_path, sort_function, save=True, show=False):
    # the copy of V2
    # img is the Brightest value of img1 and the Darkest value of img2
    # img sub every image in sub_folder, and save to result_path

    if not os.path.exists(sub_folder):
        print('!ERROR! The sub_folder does not existed!')
        return False
    if not os.path.exists(result_path):
        os.makedirs(result_path)

    img1 = image_to_gray(img1)
    img2 = image_to_gray(img2)
    h1, w1 = img1.shape[:2]
    h2, w2 = img2.shape[:2]
    if h1 != h2 or w1 != w2:
        img2 = cv2.resize(img2, (w1, h1), interpolation=cv2.INTER_NEAREST)

    img = img2
    x = (img1 >= 128) & (img2 >= 128) & (img1 >= img2)
    img[x] = img1[x]

    path_list = os.listdir(sub_folder)
    sort_function(path_list)
    for this_img_name in path_list:
        this_img = os.path.join(sub_folder, this_img_name)
        this_img = image_to_gray(this_img)
        this_h, this_w = this_img.shape[:2]

        if h1 != this_h or w1 != this_w:
            this_img = cv2.resize(this_img, (w1, h1), interpolation=cv2.INTER_NEAREST)

        result = img - this_img
        result[result < 0] = 0
        # result = abs(result)

        if save:
            # plt.savefig(os.path.join(result_path, name))
            to_file = os.path.join(result_path, this_img_name)
            cv2.imwrite(to_file, result)
        if show:
            cv2.imshow('image_sub', result)
            cv2.waitKey(0)
            cv2.destroyAllWindows()

    return True
Ejemplo n.º 4
0
def get_folder_negative_film_V1(img1, img2, sub_folder, result_path, sort_function, save=True, show=False):
    # img1 sub every image in sub_folder, and save to result_path

    # if not os.path.exists(main_path):
    #     print('!ERROR! The main_path does not existed!')
    #     return False
    # sub_folder = os.path.join(main_path, sub_folder)
    if not os.path.exists(sub_folder):
        print('!ERROR! The sub_folder does not existed!')
        return False
    # result_path = os.path.join(main_path, result_path)
    if not os.path.exists(result_path):
        os.makedirs(result_path)

    img1 = image_to_gray(img1)
    h1, w1 = img1.shape[:2]

    path_list = os.listdir(sub_folder)
    sort_function(path_list)
    for img2_name in path_list:
        img2 = os.path.join(sub_folder, img2_name)
        print('>>> get_folder_negative_film_V1(', img2, ')')
        img2 = image_to_gray(img2)
        h2, w2 = img2.shape[:2]

        if h1 != h2 or w1 != w2:
            img2 = cv2.resize(img2, (w1, h1), interpolation=cv2.INTER_NEAREST)

        result = img1 - img2
        result[result < 0] = 0
        # result = abs(result)

        if save:
            # plt.savefig(os.path.join(result_path, name))
            to_file = os.path.join(result_path, img2_name)
            cv2.imwrite(to_file, result)
        if show:
            cv2.imshow('image_sub', result)
            cv2.waitKey(0)
            cv2.destroyAllWindows()

    return True
Ejemplo n.º 5
0
def get_well_difference(input_f, output_f, sort_function):
    # calculate adjacent pictures' difference of one folder
    # notice: it must giving the sort function

    # if not os.path.exists(main_path):
    #     print('!ERROR! The main_path does not existed!')
    #     return False
    # folder = os.path.join(main_path, folder)
    if not os.path.exists(input_f):
        print('!ERROR! The input_f does not existed!')
        return False
    # result_path = os.path.join(main_path, result_path)
    if not os.path.exists(output_f):
        os.makedirs(output_f)

    path_list = os.listdir(input_f)
    path_list = sort_function(path_list)
    for i in range(len(path_list) - 1):
        img1 = os.path.join(input_f, path_list[i])
        img1 = image_to_gray(img1)
        img2 = os.path.join(input_f, path_list[i + 1])
        img2 = image_to_gray(img2)

        h1, w1 = img1.shape[:2]
        h2, w2 = img2.shape[:2]

        if h1 != h2 or w1 != w2:
            img2 = cv2.resize(img2, (w1, h1), interpolation=cv2.INTER_NEAREST)

        result = img1 - img2
        result[result < 0] = 0
        # result = abs(result)

        this_range = np.max(result) - np.min(result)
        result = (result - np.min(result)) / this_range
        result = result * 255

        to_file = os.path.join(output_f, path_list[i])
        cv2.imwrite(to_file, result)

    return True
Ejemplo n.º 6
0
def get_image_image_sub(main_path, img1, img2, result_path='image_sub', name='sub.png', save=True, show=False):
    if not os.path.exists(main_path):
        print('!ERROR! The main_path does not existed!')
        return False
    result_path = os.path.join(main_path, result_path)
    if not os.path.exists(result_path):
        os.makedirs(result_path)
    img1 = image_to_gray(img1)
    img2 = image_to_gray(img2)

    result = img1 - img2
    result[result < 0] = 0
    # result = abs(result)

    if save:
        # plt.savefig(os.path.join(result_path, name))
        to_file = os.path.join(result_path, name)
        cv2.imwrite(to_file, result)
    if show:
        cv2.imshow('image_sub', result)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

    return True
Ejemplo n.º 7
0
def get_image_hist(main_path, well_image, result_path='Hist', name='Image_Hist.png', save=True, show=False):
    if not os.path.exists(main_path):
        print('!ERROR! The main_path does not existed!')
        return False
    result_path = os.path.join(main_path, result_path)
    if not os.path.exists(result_path):
        os.makedirs(result_path)
    img = image_to_gray(well_image)

    h, w = img.shape[:2]
    pixelSequence = img.reshape([h * w, ])
    histogram, bins, patch = plt.hist(pixelSequence, 256, facecolor='black', histtype='bar')
    plt.xlabel("gray label")
    plt.ylabel("number of pixels")
    plt.axis([0, 255, 0, np.max(histogram)])
    if save:
        plt.savefig(os.path.join(result_path, name))
    if show:
        plt.show()

    return True
Ejemplo n.º 8
0
def get_image_auto_binary(main_path, well_image, result_path='auto_binary', inv=False, show=False, blockSize=None,
                          C=16):
    # using cv2.adaptiveThreshold to enhance image(auto intensity threshold)
    # once one imgae
    # NOTICE: blockSize and C
    # blockSize is the MEAN or GAUSSIAN kernal size
    # C is a constant: Distinguish distance
    # if inv is True then do Negative Film

    if not os.path.exists(main_path):
        print('!ERROR! The main_path does not existed!')
        return False
    if not os.path.exists(well_image):
        print('!ERROR! The well_image does not existed!')
        return False
    result_path = os.path.join(main_path, result_path)
    if not os.path.exists(result_path):
        os.makedirs(result_path)

    t_path_list = os.path.split(well_image)  # [r'D:\pro\CD22\SSS_100%\S1', '2018-11-28~IPS_CD13~T1.jpg']
    t1_path_list = os.path.split(t_path_list[0])  # [r'D:\pro\CD22\SSS_100%', 'S1']
    t2_path_list = os.path.split(t1_path_list[0])  # [r'D:\pro\CD22', 'SSS_100%']
    SSS_folder = t2_path_list[1]  # 'SSS_100%'
    S_index = t1_path_list[1]  # 'S1'
    S = int(S_index.split('S')[1])  # 1
    img_name = t_path_list[1]  # '2018-11-28~IPS_CD13~T1.png'
    name_index = img_name[:-4]  # '2018-11-28~IPS_CD13~T1'
    # T = int(name_index.split('~T')[1])  # 1

    # if S <= 2:
    #     return True

    print('>>>', S_index, '---', name_index, ' Image Auto Binary :::')
    to_file = os.path.join(result_path, SSS_folder, S_index, img_name)
    if not os.path.exists(os.path.join(result_path, SSS_folder)):
        os.makedirs(os.path.join(result_path, SSS_folder))
    if not os.path.exists(os.path.join(result_path, SSS_folder, S_index)):
        os.makedirs(os.path.join(result_path, SSS_folder, S_index))

    img = image_to_gray(well_image)
    h, w = img.shape[:2]
    if blockSize is None:
        blockSize = int(np.min([h, w]) / 10)
        if blockSize % 2 != 1:
            blockSize = blockSize - 1

    if C is None:
        C = int(abs(np.mean(img) - np.median(img)))
        if C == 0:
            C = 16

    print('blockSize is: ', blockSize, 'C is: ', C)
    if inv:
        result_th = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, blockSize, C)
        # result_th = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, blockSize, C)
    else:
        result_th = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, blockSize, C)
        # result_th = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, blockSize, C)

    cv2.imwrite(to_file, result_th)

    if show:
        cv2.imshow('adaptiveThreshold_image', result_th)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

    return True
Ejemplo n.º 9
0
def return_mask(main_path, sigle_image, kernel=(69, 69), pix=(23, 23), getF=getMean, getC=get_Bright_Dark,
                n_clusters=3, result_path='pix_features', save=True, save_name=None, show=False):
    # this program is design for core analysis; one time one well one time point one image
    # for block feature extraction and cluster
    # kernel is feature extraction unit, the window
    # pix is compressed image block, the Super pixel
    # kernel >= pix
    # now it is doing feature extraction
    # input sigle_image has well edge, is 5*5
    # sigle_image is a image path
    # kernel or pix is (w,h)(x,y)

    if save:
        result_path = os.path.join(main_path, result_path)
        if os.path.exists(result_path):
            # shutil.rmtree(output_folder)
            pass
        else:
            os.makedirs(result_path)  # make the output folder

    if type(sigle_image) is str:
        sigle_image = os.path.join(main_path, sigle_image)
        if save_name is None:
            save_name = os.path.split(sigle_image)[1]

    img = image_to_gray(sigle_image)

    if show:
        print(img.shape)
        print(img[int(img.shape[0] / 2):int(img.shape[0] / 2 + img.shape[0] / 10),
              int(img.shape[1] / 2):int(img.shape[1] / 2 + img.shape[1] / 10)])
        cv2.imshow('image_to_gray()', img)
        cv2.waitKey()
        cv2.destroyAllWindows()

    if img is None:
        return None
    pix_h = img.shape[0]  # y:height,row
    pix_w = img.shape[1]  # x:width,col
    compress_pix_w = int((pix_w - kernel[0] + pix[0]) / pix[0])  # x:width,col
    compress_pix_h = int((pix_h - kernel[1] + pix[1]) / pix[1])  # y:height,row
    offset_x = int((kernel[0] - pix[0]) / 2)  # x:width,col
    offset_y = int((kernel[1] - pix[1]) / 2)  # y:height,row
    # used_pix_w = compress_pix_w * pix[0] - pix[0] + kernel[0]
    # used_pix_h = compress_pix_h * pix[1] - pix[1] + kernel[1]
    # used_img = img[:used_pix_h, :used_pix_w, :]

    if show:
        print('compress_pix_w = ', compress_pix_w)
        print('compress_pix_h = ', compress_pix_h)

    pix_features_list = []  # is a col of
    for row in range(compress_pix_h):
        for col in range(compress_pix_w):
            stamp = img[row * pix[1]:row * pix[1] + kernel[1], col * pix[0]:col * pix[0] + kernel[0]]
            # if show:
            #     cv2.imshow('steps(' + str(row) + ',' + str(col) + ')', stamp)
            #     cv2.waitKey()
            #     cv2.destroyAllWindows()
            pix_features_list.append(getF(stamp))

    pix_features_list = np.array(pix_features_list)
    pix_features_list = np.nan_to_num(pix_features_list)
    # print(pix_features_list)
    # print(pix_features_list.shape)
    label = getC(pix_features_list, n_clusters=n_clusters)
    # label = pix_features_list[:,0]
    label_map = label.reshape((compress_pix_h, compress_pix_w))
    # label_map = np.array(255 / (label_map + 1), dtype=np.uint8)
    # label_map_colored = np.zeros((compress_pix_h, compress_pix_w, 3), dtype=np.uint8)
    # color_key = return_color()
    # for i in range(compress_pix_h):
    #     for j in range(compress_pix_w):
    #         label_map_colored[i, j] = color_key[int(label_map[i, j])]

    # cv2.resize(img_cpy, (compress_pix_w*pix[0], compress_pix_h*pix[1]), interpolation=cv2.INTER_NEAREST)
    label_map_big = cv2.resize(label_map, (0, 0), fx=pix[0], fy=pix[1], interpolation=cv2.INTER_NEAREST)
    print(label_map_big.shape)
    label_result = np.ones((pix_h, pix_w), dtype=np.uint8) * 128
    print(label_result.shape)
    label_result[offset_y:offset_y + compress_pix_h * pix[1],
    offset_x:offset_x + compress_pix_w * pix[0]] = label_map_big

    if save:
        if save_name is None:
            save_name = 'result.png'
        result_file = os.path.join(result_path, save_name)
        cv2.imwrite(result_file, label_result)

    if show:
        cv2.imshow('get_img_pix_features_list()', label_result)
        cv2.waitKey()
        cv2.destroyAllWindows()

    return False