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)
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
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)
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()
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)
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)