def make_dataset(category=None,
                 dirlist=None,
                 height=32,
                 width=32,
                 channel=3,
                 extensions=None):

    print("\n** Make " + category)

    class_len = len(dirlist)
    io_mode = "w"
    label_number = 0

    if (not (util.check_path(path=PACK_PATH + "/images/"))):
        util.make_path(path=PACK_PATH + "/images/")
    util.refresh_directory(PACK_PATH + "/images/dataset/")

    channel = 1
    for di in dirlist:
        tmp_path = PACK_PATH + "/dataset/" + category + "/" + di
        fi_list = util.get_filelist(directory=tmp_path, extensions=extensions)

        cnt = 0
        for fi in fi_list:
            tmp_sub, tmp_file = util.get_dir_and_file_name(path=fi)
            cnt += 1

            image = cv2.imread(fi)
            resized_image = cv2.resize(image, (width, height))

            cvf.save_image(path=PACK_PATH + "/images/dataset/",
                           filename=str(label_number) + "_" + str(cnt) +
                           ".png",
                           image=resized_image)

            if (channel == 1):
                resized_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY)
                height, width = resized_image.shape
            else:
                height, width, channel = resized_image.shape
            resized_image = resized_image.reshape((height * width * channel))

            np.save(file=tmp_path + "/" + tmp_file, arr=resized_image)

        label_number += 1

    if (os.path.exists(PACK_PATH + "/" + category)):  # management storage
        shutil.rmtree(PACK_PATH + "/" + category)

    f = open(PACK_PATH + "/dataset/format.txt", "w")
    f.write(str(label_number))
    f.write("\n")
    f.write(str(height * width * channel))
    f.write("\n")
    f.write(str(height))
    f.write("\n")
    f.write(str(width))
    f.write("\n")
    f.write(str(channel))
    f.close()
Exemplo n.º 2
0
def main():

    extensions = ["BMP", "bmp", "PNG", "png", "JPG", "jpg", "JPEG", "jpeg"]

    util.refresh_directory(PACK_PATH+"/images")

    print("Enter the path")
    # usr_path = input(">> ")
    usr_path = "/media/yeonghyeon/Toshiba/lung/datasets/20171204"

    if(util.check_path(usr_path)):
        files = util.get_filelist(directory=usr_path, extensions=extensions)
        for fi in files:
            print(fi)

            tmp_sub, tmp_file = util.get_dir_and_file_name(path=fi)

            if(not(util.check_path(path=PACK_PATH+"/images/"+str(tmp_file)+"/"))):
                util.make_path(path=PACK_PATH+"/images/"+str(tmp_file)+"/")

            image = cvf.load_image(path=fi)

            if(image.shape[0] > image.shape[1]): # height > width
                resized = cvf.resizing(image=image, width=int(500*(image.shape[1]/image.shape[0])), height=500)
            else:
                resized = cvf.resizing(image=image, width=500, height=int(500*(image.shape[0]/image.shape[1])))
            zeropad = cvf.zero_padding(image=resized, height=500, width=500)
            print(image.shape)
            print(resized.shape)
            print(zeropad.shape)
            cvf.save_image(path=PACK_PATH+"/images/", filename=str(tmp_file)+".png", image=zeropad)
    else:
        print("Invalid path :"+usr_path)
Exemplo n.º 3
0
def GetLungROI(filename):

    PACK_PATH = "G:/Work/ChestRadioGraphy/tb/tb-voc/VOC2007/JPEGImages"
    outRoidir = "G:/Work/ChestRadioGraphy/tb/tb-voc/VOC2007/BoxJPEGImages"

    outpath = os.path.join(PACK_PATH, "ROI")
    if not os.path.exists(outpath):
        os.makedirs(outpath)

    if not os.path.exists(outRoidir):
        os.makedirs(outRoidir)

    pyramid = [800, 500, 100]
    boxes = []
    if filename.split(".")[-1] == "jpg":
        # 导入图像
        img = cvf.load_image(path=os.path.join(PACK_PATH, filename))
        height, width = img.shape[0], img.shape[1]

        #  在不同下采样程度上的图像上计算ROI框
        for imgwidth in pyramid:
            boxespart = GetRoiBoxes(filename, img, imgwidth, outpath)
            boxes += boxespart

        # 抑制重叠的框,并选出最大的两个框
        boxes = cvf.NMS(boxes, (width, height), 0.5)

        # 对选出的两个框进行合理性判断以及 并框
        boxes = cvf.merge(boxes, (width, height))

        # 输出ROI区域
        RoiFilePath = os.path.join(outRoidir, filename + "_ROI.txt")
        with open(RoiFilePath, "w") as fp:
            if boxes != None:
                # 输出ROI区域
                fp.writelines("%d %d %d %d" %
                              (boxes[0][0], boxes[0][1], boxes[0][0] +
                               boxes[0][2], boxes[0][1] + boxes[0][3]))

                for box in boxes:
                    rx, ry, rw, rh = box
                    cv2.rectangle(img, (rx, ry), (rx + rw, ry + rh),
                                  (0, 255, 0), 2)
            else:
                # 没有正确找到ROI区域
                fp.writelines("None" + "\n")

        cvf.save_image(True,
                       path=outpath,
                       filename="boxes_" + filename,
                       image=img)
Exemplo n.º 4
0
def GetRoiBoxes(filename, img, imgwidth, outpath):
    origin = img.copy()
    # rgb 转 gray
    try:
        gray = cvf.rgb2gray(rgb=origin)
    except:  # if origin image is grayscale
        gray = origin

    #  原图图像尺寸
    h, w = gray.shape

    #尺寸调整
    resized = cvf.resizing(image=gray, width=imgwidth)
    # 尺寸调整后的尺寸
    hr, wr = resized.shape
    cvf.save_image(saveflag,
                   path=outpath,
                   filename="resize_" + str(imgwidth) + "_" + filename,
                   image=resized)

    resized = cvf.bluring(binary_img=resized, k_size=11)

    mulmul = resized.copy()
    for i in range(20):

        # 将图像 按[0.3*均值,255]范围二值化
        ret, thresh = cv2.threshold(mulmul,
                                    np.average(mulmul) * 0.3, 255,
                                    cv2.THRESH_BINARY)
        cvf.save_image(saveflag,
                       path=outpath,
                       filename="thresh_" + str(imgwidth) + "_" + str(i) +
                       "_" + filename,
                       image=thresh)
        # 将图像 规范化到均值为127
        mulmul = cvf.normalizing(binary_img=resized * (thresh / 255))
        cvf.save_image(saveflag,
                       path=outpath,
                       filename="normal_" + str(imgwidth) + "_" + str(i) +
                       "_" + filename,
                       image=mulmul)

    movavg = cvf.moving_avg_filter(binary_img=mulmul, k_size=10)
    adap = cvf.adaptiveThresholding(binary_img=movavg,
                                    neighbor=111,
                                    blur=True,
                                    blur_size=3)
    cvf.save_image(saveflag,
                   path=outpath,
                   filename="adaptive_" + str(imgwidth) + "_" + filename,
                   image=255 - adap)

    masking = resized * ((255 - adap) / 255)
    cvf.save_image(saveflag,
                   path=outpath,
                   filename="mask_" + str(imgwidth) + "_" + filename,
                   image=masking)

    movavg = cvf.moving_avg_filter(binary_img=masking, k_size=5)
    cvf.save_image(saveflag,
                   path=outpath,
                   filename="movavg_" + str(imgwidth) + "_" + filename,
                   image=movavg)

    ret, thresh = cv2.threshold(movavg,
                                np.average(movavg) * 0.5, 255,
                                cv2.THRESH_BINARY_INV)
    cvf.save_image(saveflag,
                   path=outpath,
                   filename="thresh_" + str(imgwidth) + "_" + str(imgwidth) +
                   "_" + filename,
                   image=thresh)

    contours = cvf.contouring(binary_img=thresh)

    cv2.drawContours(resized, contours, -1, 0)
    cvf.save_image(saveflag,
                   path=outpath,
                   filename="Contours_" + str(imgwidth) + "_" + str(imgwidth) +
                   "_" + filename,
                   image=resized)

    boxes_tmp = cvf.contour2box(contours=contours, padding=20)
    boxes = cvf.rid_repetition(boxes=boxes_tmp, binary_img=thresh)

    newboxes = []
    for box in boxes:
        box[0] = int(float(w) / float(wr) * box[0])
        box[1] = int(float(h) / float(hr) * box[1])
        box[2] = int(float(w) / float(wr) * box[2])
        box[3] = int(float(h) / float(hr) * box[3])
        newboxes.append(box)

    return newboxes
Exemplo n.º 5
0
        dicom_data = dicom.read_file(fi)
        try:
            dicom_numpy = dicom_data.pixel_array
        except:
            print("TypeError: No pixel data found in this dataset.")
        else:
            sumx = np.sum(dicom_numpy) / (dicom_numpy.shape[0]*dicom_numpy.shape[1])
            dicom_normal = (dicom_numpy / sumx) * (2**7-1)

            # area1 = np.mean(dicom_normal[:int(dicom_numpy.shape[0]/4), :int(dicom_numpy.shape[1]/4)])
            # area2 = np.mean(dicom_normal[int(dicom_numpy.shape[0]/4*3):, :int(dicom_numpy.shape[1]/4)])
            # area3 = np.mean(dicom_normal[:int(dicom_numpy.shape[0]/4), int(dicom_numpy.shape[1]/4*3):])
            # area4 = np.mean(dicom_normal[int(dicom_numpy.shape[0]/4*3):, int(dicom_numpy.shape[1]/4*3):])
            #
            # threshold = np.mean([area1, area2, area3, area4])
            # if(threshold > 127):
            #     dicom_normal = 255 - dicom_normal
            cvf.save_image(path=main_dir, filename=tmp_file+".bmp", image=dicom_normal)

            dist = (2**16-1) / np.max(dicom_numpy)
            dicom_normal = dicom_numpy * dist

            dicom_normal, _ = image_histogram_equalization(image=dicom_numpy, number_bins=int(2**(16/1)))
            print(np.max(dicom_normal))

            tiff.imsave(main_dir+tmp_file+".tiff", (dicom_normal*(2**0)).astype(np.uint16))

            tmp = tiff.imread(main_dir+tmp_file+".tiff")
            print(np.max(tmp))
def extract_segments(filename):

    tmp_sub, tmp_file = util.get_dir_and_file_name(path=filename)

    if (not (util.check_path(path=PACK_PATH + "/images/" + str(tmp_file) +
                             "/"))):
        util.make_path(path=PACK_PATH + "/images/" + str(tmp_file) + "/")

    origin = cvf.load_image(path=filename)
    gray = cvf.rgb2gray(rgb=origin)
    resized = cvf.resizing(image=gray, width=500)
    cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/",
                   filename=str(tmp_file) + ".png",
                   image=resized)

    mulmul = resized.copy()
    for i in range(20):
        ret, thresh = cv2.threshold(mulmul,
                                    np.average(mulmul) * 0.3, 255,
                                    cv2.THRESH_BINARY)
        cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/",
                       filename=str(tmp_file) + "_thresh1.png",
                       image=thresh)

        mulmul = cvf.normalizing(binary_img=resized * (thresh / 255))

    movavg = cvf.moving_avg_filter(binary_img=mulmul, k_size=10)
    adap = cvf.adaptiveThresholding(binary_img=movavg,
                                    neighbor=111,
                                    blur=False,
                                    blur_size=3)
    cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/",
                   filename=str(tmp_file) + "_adap.png",
                   image=255 - adap)

    result = resized * ((255 - adap) / 255)
    cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/",
                   filename=str(tmp_file) + "_result1.png",
                   image=result)

    movavg = cvf.moving_avg_filter(binary_img=result, k_size=10)
    cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/",
                   filename=str(tmp_file) + "_result2.png",
                   image=movavg)

    ret, thresh = cv2.threshold(movavg,
                                np.average(movavg) * 0.5, 255,
                                cv2.THRESH_BINARY_INV)
    cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/",
                   filename=str(tmp_file) + "_thresh2.png",
                   image=thresh)

    contours = cvf.contouring(binary_img=thresh)
    boxes = cvf.contour2box(contours=contours, padding=20)

    resized = cvf.resizing(image=gray, width=500)

    cnt = 0
    for box in boxes:
        x, y, w, h = box

        if ((x > 0) and (y > 0)):
            if ((x + w < resized.shape[1]) and (y + h < resized.shape[0])):

                cvf.save_image(
                    path=PACK_PATH + "/images/" + str(tmp_file) + "/",
                    filename=str(tmp_file) + "_0_" + str(cnt) + ".png",
                    image=thresh[y:y + h, x:x + w])
                pad = cvf.zero_padding(image=thresh[y:y + h, x:x + w],
                                       height=500,
                                       width=500)
                cvf.save_image(
                    path=PACK_PATH + "/images/" + str(tmp_file) + "/",
                    filename=str(tmp_file) + "_1_" + str(cnt) + ".png",
                    image=pad)
                pad2 = cvf.remain_only_biggest(binary_img=pad)
                cvf.save_image(
                    path=PACK_PATH + "/images/" + str(tmp_file) + "/",
                    filename=str(tmp_file) + "_2_" + str(cnt) + ".png",
                    image=pad2)
                pad_res = cvf.zero_padding(image=resized[y:y + h, x:x + w],
                                           height=500,
                                           width=500)
                cvf.save_image(
                    path=PACK_PATH + "/images/" + str(tmp_file) + "/",
                    filename=str(tmp_file) + "_3_" + str(cnt) + ".png",
                    image=pad_res * (pad2 / 255))
                cvf.save_image(
                    path=PACK_PATH + "/images/" + str(tmp_file) + "/",
                    filename=str(tmp_file) + "_4_" + str(cnt) + ".png",
                    image=resized[y:y + h, x:x + w])
                cnt += 1

    for b in boxes:
        x, y, w, h = b

        if ((x > 0) and (y > 0)):
            if ((x + w < resized.shape[1]) and (y + h < resized.shape[0])):
                cv2.rectangle(resized, (x, y), (x + w, y + h), (255, 255, 255),
                              2)
                cv2.rectangle(thresh, (x, y), (x + w, y + h), (255, 255, 255),
                              2)

    # cvf.save_image(path=PACK_PATH+"/images/"+str(tmp_file)+"/", filename="opened.png", image=dilated)
    cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/",
                   filename="contour.png",
                   image=thresh)
    cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/",
                   filename="resized.png",
                   image=resized)

    cvf.save_image(path=PACK_PATH + "/images/",
                   filename="resized" + str(tmp_file) + ".png",
                   image=resized)
Exemplo n.º 7
0
def concatenate(image=None, boxes=None, ratio=1, file_name=None):

    box_left = []
    box_right = []
    for box in boxes:
        x, y, w, h, result, acc = box
        rx, ry, rw, rh = x * ratio, y * ratio, w * ratio, h * ratio

        if ((rx > 0) and (ry > 0)):
            if ((rx + rw < image.shape[1]) and (ry + rh < image.shape[0])):
                if (result == "lung_left"):
                    box_left.append([rx, ry, rw, rh, result, acc])
                elif (result == "lung_right"):
                    box_right.append([rx, ry, rw, rh, result, acc])

    cnt = 0
    tmp_boxes = []
    for box_r in box_right:
        x_r, y_r, w_r, h_r, result_r, acc_r = box_r

        for box_l in box_left:
            x_l, y_l, w_l, h_l, result_l, acc_l = box_l

            center_rx = x_r + (w_r / 2)
            center_ry = y_r + (h_r / 2)
            center_lx = x_l + (w_l / 2)
            center_ly = y_l + (h_l / 2)

            dist_limit = max(h_r, h_l) / 3 * 2

            if (abs(center_ry - center_ly) >
                    dist_limit):  # concat by y relation.
                continue
            else:
                x_start = min(x_r, x_l)
                y_start = min(y_r, y_l)
                x_end = max(x_r + w_r, x_l + w_l)
                y_end = max(y_r + h_r, y_l + h_l)

                if ((x_start > 0) and (y_start > 0)):
                    if ((x_end < image.shape[1]) and (y_end < image.shape[0])):
                        tmp_boxes.append([
                            x_start, y_start, x_end - x_start, y_end - y_start,
                            "lung", (acc_r + acc_l) / 2
                        ])

    box_concat = []
    try:
        max_idx = 0
        tmp_size = 0
        for idx in range(len(tmp_boxes)):
            x, y, w, h, result, acc = tmp_boxes[idx]

            if ((w * h) > tmp_size):
                tmp_size = w * h
                max_idx = idx

        x, y, w, h, result, acc = tmp_boxes[max_idx]
        box_concat.append([x, y, w, h, result, acc])
        cvf.save_image(path=PACK_PATH + "/results/" + str(file_name) + "/",
                       filename=str(file_name) + "_concat_" + str(cnt) + "_" +
                       str(int((acc_r + acc_l) / 2 * 100)) + ".png",
                       image=image[y:y + h, x:x + w])
        cnt += 1
    except:
        pass
    # try:
    #     max_idx = 0
    #     tmp_acc = 0
    #     for idx in range(len(tmp_boxes)):
    #         x, y, w, h, result, acc = tmp_boxes[idx]
    #
    #         if(acc > tmp_acc):
    #             tmp_acc = acc
    #             max_idx = idx
    #
    #     x, y, w, h, result, acc = tmp_boxes[max_idx]
    #     box_concat.append([x, y, w, h, result, acc])
    # except:
    #     pass

    return box_concat  # return only one box
Exemplo n.º 8
0
def extract_lung(usr_path,
                 extensions=None,
                 height=None,
                 width=None,
                 channel=None,
                 sess=None,
                 x_holder=None,
                 training=None,
                 prediction=None,
                 saver=None):

    if (not (util.check_path(path=PACK_PATH + "/results/"))):
        util.make_path(path=PACK_PATH + "/results/")

    summf = open(PACK_PATH + "/results/summary.csv", "w")
    summf.write("FILENAME")
    summf.write(",")
    summf.write("DETECT")
    summf.write(",")
    summf.write("IOU")
    summf.write("\n")

    files = util.get_filelist(directory=usr_path, extensions=extensions)
    files.sort()
    for filename in files:
        print(filename)

        if (util.check_file(filename=filename)):
            tmp_sub, tmp_file = util.get_dir_and_file_name(path=filename)

            if (not (util.check_path(path=PACK_PATH + "/results/" +
                                     str(tmp_file) + "/"))):
                util.make_path(path=PACK_PATH + "/results/" + str(tmp_file) +
                               "/")

            origin = cvf.load_image(path=filename)
            try:
                gray = cvf.rgb2gray(rgb=origin)
            except:  # if origin image is grayscale
                gray = origin
            resized = cvf.resizing(image=gray, width=500)
            cvf.save_image(path=PACK_PATH + "/results/" + str(tmp_file) + "/",
                           filename=str(tmp_file) + "_pre1_origin.png",
                           image=resized)

            mulmul = resized.copy()
            for i in range(20):
                ret, thresh = cv2.threshold(mulmul,
                                            np.average(mulmul) * 0.3, 255,
                                            cv2.THRESH_BINARY)
                cvf.save_image(path=PACK_PATH + "/results/" + str(tmp_file) +
                               "/",
                               filename=str(tmp_file) + "_pre2_thresh.png",
                               image=thresh)

                mulmul = cvf.normalizing(binary_img=resized * (thresh / 255))
                cvf.save_image(path=PACK_PATH + "/results/" + str(tmp_file) +
                               "/",
                               filename=str(tmp_file) + "_pre3_normalize.png",
                               image=mulmul)

            movavg = cvf.moving_avg_filter(binary_img=mulmul, k_size=10)
            adap = cvf.adaptiveThresholding(binary_img=movavg,
                                            neighbor=111,
                                            blur=False,
                                            blur_size=3)
            cvf.save_image(path=PACK_PATH + "/results/" + str(tmp_file) + "/",
                           filename=str(tmp_file) + "_pre4_adaptrhesh.png",
                           image=255 - adap)

            masking = resized * ((255 - adap) / 255)
            cvf.save_image(path=PACK_PATH + "/results/" + str(tmp_file) + "/",
                           filename=str(tmp_file) + "_pre5_mask1.png",
                           image=masking)

            movavg = cvf.moving_avg_filter(binary_img=masking, k_size=5)
            cvf.save_image(path=PACK_PATH + "/results/" + str(tmp_file) + "/",
                           filename=str(tmp_file) + "_pre6_mask2.png",
                           image=movavg)

            ret, thresh = cv2.threshold(movavg,
                                        np.average(movavg) * 0.5, 255,
                                        cv2.THRESH_BINARY_INV)
            cvf.save_image(path=PACK_PATH + "/results/" + str(tmp_file) + "/",
                           filename=str(tmp_file) + "_pre7_thresh.png",
                           image=thresh)

            contours = cvf.contouring(binary_img=thresh)
            boxes_tmp = cvf.contour2box(contours=contours, padding=20)
            boxes = cvf.rid_repetition(boxes=boxes_tmp, binary_img=thresh)

            if (os.path.exists(PACK_PATH + "/checkpoint/checker.index")):
                saver.restore(sess, PACK_PATH + "/checkpoint/checker")
                f = open(PACK_PATH + "/dataset/labels.txt", 'r')
                content = f.readlines()
                f.close()
                for idx in range(len(content)):
                    content[idx] = content[idx][:len(content[idx]) -
                                                1]  # rid \n

                boxes_pred = []
                cnt = 0
                for b in boxes:
                    x, y, w, h = b
                    if ((x > 0) and (y > 0)):
                        if ((x + w < resized.shape[1])
                                and (y + h < resized.shape[0])):

                            pad = cvf.zero_padding(image=thresh[y:y + h,
                                                                x:x + w],
                                                   height=500,
                                                   width=500)
                            pad2 = cvf.remain_only_biggest(binary_img=pad)
                            pad_res = cvf.zero_padding(image=resized[y:y + h,
                                                                     x:x + w],
                                                       height=500,
                                                       width=500)

                            xdata = pad_res * (pad2 / 255)

                            prob = sess.run(prediction,
                                            feed_dict={
                                                x_holder:
                                                convert_image(image=xdata,
                                                              height=height,
                                                              width=width,
                                                              channel=channel),
                                                training:
                                                False
                                            })
                            result = str(content[int(np.argmax(prob))])
                            acc = np.max(prob)

                            boxes_pred.append([x, y, w, h, result, acc])

                            # cvf.save_image(path=PACK_PATH+"/results/"+str(tmp_file)+"/", filename=str(tmp_file)+"_"+str(result)+"_"+str(int(round(acc, 2)*100))+"_"+str(cnt)+".png", image=xdata)

                            cnt += 1

                boxes_pred = sorted(boxes_pred,
                                    key=lambda l: l[4],
                                    reverse=True)  # sort by result
                boxes_pred = sorted(boxes_pred,
                                    key=lambda l: l[5],
                                    reverse=True)  # sort by acc

                ratio = origin.shape[0] / resized.shape[0]

                save_crops(image=resized,
                           boxes=boxes_pred,
                           ratio=1,
                           file_name=tmp_file)
                concats = concatenate(image=resized,
                                      boxes=boxes_pred,
                                      ratio=1,
                                      file_name=tmp_file)

                iou, bbox = intersection_over_union(filename=filename,
                                                    boxes=concats,
                                                    ratio=ratio)
                summf.write(str(filename))
                summf.write(",")
                summf.write(str(len(concats)))
                summf.write(",")
                summf.write(str(iou))
                summf.write("\n")

                origin_res1 = cvf.resizing(image=origin, width=500)
                origin_res2 = origin_res1.copy()
                origin_res3 = origin_res1.copy()

                origin_res_lr = draw_boxes(image=origin_res1,
                                           boxes=boxes_pred,
                                           ratio=1,
                                           file_name=tmp_file)
                cvf.save_image(path=PACK_PATH + "/results/" + str(tmp_file) +
                               "/",
                               filename=str(tmp_file) + "_origin_lr.png",
                               image=origin_res_lr)
                origin_res_concat1 = draw_boxes(image=origin_res1,
                                                boxes=concats,
                                                ratio=1,
                                                file_name=tmp_file)
                cvf.save_image(
                    path=PACK_PATH + "/results/" + str(tmp_file) + "/",
                    filename=str(tmp_file) + "_origin_lr_and_concat.png",
                    image=origin_res_concat1)
                origin_res_concat2 = draw_boxes(image=origin_res2,
                                                boxes=concats,
                                                ratio=1,
                                                file_name=tmp_file)
                cvf.save_image(path=PACK_PATH + "/results/" + str(tmp_file) +
                               "/",
                               filename=str(tmp_file) + "_origin_concat.png",
                               image=origin_res_concat2)
                if (len(bbox) > 0):
                    origin_res_bbox = draw_boxes(image=origin_res3,
                                                 boxes=bbox,
                                                 ratio=1,
                                                 file_name=tmp_file)
                    cvf.save_image(path=PACK_PATH + "/results/" +
                                   str(tmp_file) + "/",
                                   filename=str(tmp_file) + "_origin_bbox.png",
                                   image=origin_res_bbox)
                    origin_res_concat3 = draw_boxes(image=origin_res3,
                                                    boxes=concats,
                                                    ratio=1,
                                                    file_name=tmp_file)
                    cvf.save_image(
                        path=PACK_PATH + "/results/" + str(tmp_file) + "/",
                        filename=str(tmp_file) + "_origin_concat_bbox.png",
                        image=origin_res_concat3)

            else:
                print("You must training first!")
        else:
            print("Invalid File: " + str(filename))
    summf.close()
Exemplo n.º 9
0
def tmp_main():

    util.refresh_directory(PACK_PATH + "/images")

    img = cvf.load_image(
        path="/home/yeonghyeon/Desktop/total/pul edema_post.bmp")
    print(img.shape)

    gray = cvf.rgb2gray(rgb=img)
    print(gray.shape)

    res = cvf.resizing(image=gray, width=500)
    print(res.shape)
    print("AVG: " + str(np.average(res)))

    print(np.average(res), np.average(res * 2))
    cvf.save_image(path=PACK_PATH + "/images/",
                   filename="resx2.png",
                   image=res * res + res)

    feed = cvf.feeding_outside_filter(binary_img=res, thresh=100)
    cvf.save_image(path=PACK_PATH + "/images/",
                   filename="feed.png",
                   image=feed)

    movavg = cvf.moving_avg_filter(binary_img=feed, k_size=10)
    cvf.save_image(path=PACK_PATH + "/images/",
                   filename="aveage.png",
                   image=movavg)

    ret, thresh = cv2.threshold(movavg,
                                np.average(movavg) * 0.8, 255,
                                cv2.THRESH_BINARY_INV)
    cvf.save_image(path=PACK_PATH + "/images/",
                   filename="thresh.png",
                   image=thresh)
    # movavg = cvf.moving_avg_filter(binary_img=thresh, k_size=3)
    # cvf.save_image(path=PACK_PATH+"/images/", filename="aveage2.png", image=movavg)

    contours = cvf.contouring(binary_img=thresh)

    boxes = cvf.contour2box(contours=contours, padding=15)

    res = cvf.resizing(image=gray, width=500)
    cnt = 0
    for b in boxes:
        x, y, w, h = b

        if ((x > 0) and (y > 0)):
            if ((x + w < res.shape[1]) and (y + h < res.shape[0])):
                cvf.save_image(path=PACK_PATH + "/images/",
                               filename="box_0_" + str(cnt) + ".png",
                               image=res[y:y + h, x:x + w])
                cnt += 1

    cnt = 0
    for box1 in boxes:
        x1, y1, w1, h1 = box1

        for box2 in boxes:
            x2, y2, w2, h2 = box2

            x_crop = min(x1, x2)
            y_crop = min(y1, y2)
            w_crop = max(x1 + w1, x2 + w2)
            h_crop = max(y1 + h1, y2 + h2)

            if ((x_crop > 0) and (y_crop > 0)):
                if ((x_crop + w_crop < res.shape[1])
                        and (y_crop + h_crop < res.shape[0])):
                    cvf.save_image(path=PACK_PATH + "/images/",
                                   filename="box_1_" + str(cnt) + ".png",
                                   image=res[y_crop:h_crop, x_crop:w_crop])
                    cnt += 1

    for box1 in boxes:
        x1, y1, w1, h1 = box1

        for box2 in boxes:
            x2, y2, w2, h2 = box2

            x_crop = min(x1, x2)
            y_crop = min(y1, y2)
            w_crop = max(x1 + w1, x2 + w2)
            h_crop = max(y1 + h1, y2 + h2)

            if ((x_crop > 0) and (y_crop > 0)):
                if ((x_crop + w_crop < res.shape[1])
                        and (y_crop + h_crop < res.shape[0])):
                    cv2.rectangle(res, (x_crop, y_crop),
                                  (x_crop + w_crop, y_crop + h_crop),
                                  (255, 255, 255), 2)
                    cv2.rectangle(thresh, (x_crop, y_crop),
                                  (x_crop + w_crop, y_crop + h_crop),
                                  (255, 255, 255), 2)
    # for b in boxes:
    #     x, y, w, h = b
    #
    #     if((x > 0) and (y > 0)):
    #         if((x+w < res.shape[1]) and (y+h < res.shape[0])):
    #             cv2.rectangle(res,(x,y),(x+w,y+h),(255, 255, 255),2)
    #             cv2.rectangle(thresh,(x,y),(x+w,y+h),(255, 255, 255),2)

    cvf.save_image(path=PACK_PATH + "/images/",
                   filename="withbox.png",
                   image=res)
    cvf.save_image(path=PACK_PATH + "/images/",
                   filename="withbox_thre.png",
                   image=thresh)
Exemplo n.º 10
0
def extract_segments(filename):

    tmp_sub, tmp_file = util.get_dir_and_file_name(path=filename)

    if (not (util.check_path(path=PACK_PATH + "/images/" + str(tmp_file)))):
        util.make_path(path=PACK_PATH + "/images/" + str(tmp_file))

    origin = cvf.load_image(path=filename)
    gray = cvf.rgb2gray(rgb=origin)
    resized = cvf.resizing(image=gray, width=500)
    avg = np.average(resized)

    # feed = cvf.feeding_outside_filter(binary_img=resized, thresh=100)
    # cvf.save_image(path=PACK_PATH+"/images/"+str(tmp_file)+"/", filename="feed.png", image=feed)
    movavg = cvf.moving_avg_filter(binary_img=resized, k_size=10)
    cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/",
                   filename="movavg.png",
                   image=movavg)

    ret, thresh = cv2.threshold(movavg,
                                np.average(movavg) * 0.5, 255,
                                cv2.THRESH_BINARY_INV)

    cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/",
                   filename="origin.png",
                   image=origin)
    cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/",
                   filename="thresh.png",
                   image=thresh)

    contours = cvf.contouring(binary_img=thresh)
    boxes = cvf.contour2box(contours=contours, padding=50)

    resized = cvf.resizing(image=gray, width=500)

    cnt = 0
    for box in boxes:
        x, y, w, h = box

        if ((x > 0) and (y > 0)):
            if ((x + w < resized.shape[1]) and (y + h < resized.shape[0])):

                cvf.save_image(
                    path=PACK_PATH + "/images/" + str(tmp_file) + "/",
                    filename=str(tmp_file) + "_0_" + str(cnt) + ".png",
                    image=thresh[y:y + h, x:x + w])
                pad = cvf.zero_padding(image=thresh[y:y + h, x:x + w],
                                       height=500,
                                       width=500)
                cvf.save_image(
                    path=PACK_PATH + "/images/" + str(tmp_file) + "/",
                    filename=str(tmp_file) + "_1_" + str(cnt) + ".png",
                    image=pad)
                cvf.save_image(
                    path=PACK_PATH + "/images/" + str(tmp_file) + "/",
                    filename=str(tmp_file) + "_2_" + str(cnt) + ".png",
                    image=resized[y:y + h, x:x + w])
                cnt += 1

    for b in boxes:
        x, y, w, h = b

        if ((x > 0) and (y > 0)):
            if ((x + w < resized.shape[1]) and (y + h < resized.shape[0])):
                cv2.rectangle(resized, (x, y), (x + w, y + h), (255, 255, 255),
                              2)
                cv2.rectangle(thresh, (x, y), (x + w, y + h), (255, 255, 255),
                              2)

    # cvf.save_image(path=PACK_PATH+"/images/"+str(tmp_file)+"/", filename="opened.png", image=dilated)
    cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/",
                   filename="contour.png",
                   image=thresh)
    cvf.save_image(path=PACK_PATH + "/images/" + str(tmp_file) + "/",
                   filename="resized.png",
                   image=resized)

    cvf.save_image(path=PACK_PATH + "/images/",
                   filename="resized" + str(tmp_file) + ".png",
                   image=resized)