import time import os import math import sys import os,os.path,shutil import numpy as np import cv2 img_in_path = 'F:/project/Breast/InBreast/INBreast/removeother/' # img_in_path = 'F:/project/Breast/InBreast/INBreast/imgtest/' outpathimgyes = 'F:/project/Breast/InBreast/INBreast/outimgpatch/calcification/' outpathimgno = 'F:/project/Breast/InBreast/INBreast/outimgpatch/no/' txtPath = 'F:/project/Breast/InBreast/INBreast/AllTXTall/' # txtPath = 'F:/project/Breast/InBreast/INBreast/ALLtest/' outpathtxt = 'F:/project/Breast/InBreast/INBreast/outtxtpatch/' txtType = 'txt' txtLists = os.listdir(txtPath) #列出文件夹下所有的目录与文件 # Read all points into a list(before 21:30,715) for filename in txtLists: pp = 0 ppp = 0 with open(txtPath + filename, 'r') as file: print(filename) lines = file.readlines() dataset = [[] for i in range(len(lines))] for i in range(len(dataset)): dataset[i][:] = (item for item in lines[i].strip().split(',')) # 逐行读取数据 #print("dateset:", dataset) # print(type(dataset[0][0])) # print(dataset.__sizeof__()) u = np.array(dataset) list = np.zeros((u.shape[0], 2)) for i in range(u.shape[0]): # print(u[i,0][0]) findNumber = u[i,0].find(" ") # print(findNumber) list[i,0] = round(float(u[i, 0][0:findNumber])) list[i,1] = round(float(u[i, 0][findNumber+1:])) # Read the same name image(before 21:30,715) img = img_in_path + filename[:-4] + '.png' # out_path img = cv2.imread(img) black = np.zeros((img.shape[0], img.shape[1])) for i in range(img.shape[0]): for j in range(img.shape[1]): if img[i,j][0] == 0 and img[i,j][1] == 0 and img[i,j][2] == 0: black[i][j] = 1 print(img.shape[0],img.shape[1]) # Cutting distance has been determined, Handling cutting details(before 22.30, 715) row = img.shape[0]/56; col = img.shape[1]/56; print(row,col) # 待考虑问题:黑色像素 for i in range(int(row-1)): # print(i) for j in range(int(col-1)): tt = 0 pb = np.sum(black[i * 56:i * 56 + 112,j * 56:j * 56 + 112]) / 12544 # print("top_left:", i*56, j*56) # print("bottom_right:",i*56+112,j*56+112) for k in range(u.shape[0]): if j*56<list[k,0]<j*56+112 and i*56<list[k,1]<i*56+112: tt = tt + 1 patch_label = np.zeros((tt, 4)) tt1 = 0 for k in range(u.shape[0]): if j*56<list[k,0]<j*56+112 and i*56<list[k,1]<i*56+112: patch_label[tt1,0] = list[k,0] - j*56 patch_label[tt1,1] = list[k,1] - i*56 patch_label[tt1,2] = list[k,0] patch_label[tt1,3] = list[k,1] tt1 = tt1 + 1 if tt>0 and pb == 0: patch = img[i * 56:i * 56 + 112,j * 56:j * 56 + 112] pp = pp + 1 # print(patch.shape) outputimg = outpathimgyes + '/' + filename[:-4] + '/' + filename[:-4] + '_'+ str(pp) + '.png' cv2.imwrite(outputimg, patch) outputtxt1 = outpathtxt + '/' + filename[:-4] + '/' + filename[:-4] + '_' + str(pp) + '.txt' np.savetxt(outputtxt1, patch_label, fmt='%f') if tt==0 and pb == 0: patch = img[i * 56:i * 56 + 112,j * 56:j * 56 + 112] ppp = ppp + 1 # print(patch.shape) outputimg1 = outpathimgno + '/' + filename[:-4] + '/' + filename[:-4] + '_' + str(ppp) + '.png' cv2.imwrite(outputimg1, patch) print(img.shape[0], img.shape[1]) print(pp,ppp) for i in range(int(row-1)): tt = 0 pb = np.sum(black[i * 56:i * 56 + 112, img.shape[1] - 113:img.shape[1]-1]) / 12544 # print("top_right:",i*56,img.shape[1]-1) # print("bottom_left:", i * 56+112, img.shape[1] - 113) for k in range(u.shape[0]): if img.shape[1] - 113 < list[k, 0] < img.shape[1]-1 and i * 56 < list[k, 1] < i * 56 + 112: tt = tt + 1 patch_label = np.zeros((tt, 4)) tt1 = 0 # print(tt) for k in range(u.shape[0]): if img.shape[1] - 113 < list[k, 0] < img.shape[1]-1 and i * 56 < list[k, 1] < i * 56 + 112: patch_label[tt1, 0] = list[k, 0] - j * 56 patch_label[tt1, 1] = list[k, 1] - i * 56 patch_label[tt1, 2] = list[k, 0] patch_label[tt1, 3] = list[k, 1] tt1 = tt1 + 1 if tt > 0 and pb == 0.1: patch = img[i * 56:i * 56 + 112, img.shape[1] - 113:img.shape[1]-1] pp = pp + 1 # print(patch.shape) outputimg = outpathimgyes + '/' + filename[:-4] + '/' + filename[:-4] + '_' + str(pp) + '.png' cv2.imwrite(outputimg, patch) outputtxt1 = outpathtxt + '/' + filename[:-4] + '/' + filename[:-4] + '_' + str(pp) + '.txt' np.savetxt(outputtxt1, patch_label, fmt='%f') if tt == 0 and pb == 0: patch = img[i * 56:i * 56 + 112, img.shape[1] - 113:img.shape[1]-1] ppp = ppp + 1 # print(patch.shape) outputimg1 = outpathimgno + '/' + filename[:-4] + '/' + filename[:-4] + '_' + str(ppp) + '.png' cv2.imwrite(outputimg1, patch) print(img.shape[0], img.shape[1]) # # # for j in range(int(col-1)): # print("bottom_left:", img.shape[0]-1, j*56) # print("top_right:", img.shape[0] - 113, j * 56+112, ) tt = 0 pb = np.sum(black[img.shape[0]-113:img.shape[0] - 1, j * 56:j * 56+112]) / 12544 # print("top_right:",i*56,img.shape[1]-1) # print("bottom_left:", i * 56+112, img.shape[1] - 113) for k in range(u.shape[0]): if j * 56 < list[k, 0] < j * 56+112 and img.shape[0]-113 < list[k, 1] < img.shape[0]-1: tt = tt + 1 patch_label = np.zeros((tt, 4)) tt1 = 0 # print(tt) for k in range(u.shape[0]): if j * 56 < list[k, 0] < j * 56+112 and img.shape[0]-113 < list[k, 1] < img.shape[0]-1: patch_label[tt1, 0] = list[k, 0] - j * 56 patch_label[tt1, 1] = list[k, 1] - i * 56 patch_label[tt1, 2] = list[k, 0] patch_label[tt1, 3] = list[k, 1] tt1 = tt1 + 1 if tt > 0 and pb == 0: patch = img[img.shape[0]-113:img.shape[0] - 1, j * 56:j * 56+112] pp = pp + 1 # print(patch.shape) outputimg = outpathimgyes + '/' + filename[:-4] + '/' + filename[:-4] + '_' + str(pp) + '.png' cv2.imwrite(outputimg, patch) outputtxt1 = outpathtxt + '/' + filename[:-4] + '/' + filename[:-4] + '_' + str(pp) + '.txt' np.savetxt(outputtxt1, patch_label, fmt='%f') if tt == 0 and pb == 0: patch = img[img.shape[0]-113:img.shape[0] - 1, j * 56:j * 56+112] ppp = ppp + 1 # print(patch.shape) outputimg1 = outpathimgno + '/' + filename[:-4] + '/' + filename[:-4] + '_' + str(ppp) + '.png' cv2.imwrite(outputimg1, patch) # print(img.shape[0], img.shape[1]) # A = np.array([[1, 2], [3, 4], [5, 6]]) # print(np.sum(A)) print(pp)