def main(): print(len(all_info), 'vases') link_dict = dict() for img_id, img_info in tqdm(all_info.items()): # input(img_info) # if you want to see one line if 'fragment' in img_info['Title'].lower() or \ 'fragment' in img_info['description'].lower() or \ 'Fragments' in img_info['categories']: link_dict[img_id] = img_info['src'] try: img = dip.imread(id_to_img_name(img_id)) except: continue print(img.shape) new_img = dip.resize(img, dsize=outsize, interpolation=INTER_AREA) dip.im_write(new_img, id_to_out_name(img_id)) elif 'terra' in img_info['Title'].lower() or \ 'Medium' not in img_info or \ ('Medium' in img_info and 'terra' in img_info['Medium'].lower()) or \ 'terra' in img_info['description'].lower() or \ 'Terracotta' in img_info['categories']: pass # if outsize: # try: # img = dip.imread(id_to_img_name(img_id)) # except: # continue # print(img.shape) # new_img = dip.resize(img, dsize=outsize, interpolation=INTER_AREA) # dip.im_write(new_img, id_to_out_name(img_id)) # else: # shutil.copyfile(id_to_img_name(img_id), id_to_out_name(img_id)) with open('data/raw/frag_links.pkl', 'wb') as f: pickle.dump(link_dict, f)
def main(outsize=default_outsize): print(len(all_info), 'vases') for img_id, img_info in all_info.items(): # input(img_info) # if you want to see one line if 'fragment' in img_info['Title'].lower() or \ 'fragment' in img_info['description'].lower() or \ 'Fragments' in img_info['categories']: continue # just get Terracotta elif 'terra' in img_info['Title'].lower() or \ 'Medium' not in img_info or \ ('Medium' in img_info and 'terra' in img_info['Medium'].lower()) or \ 'terra' in img_info['description'].lower() or \ 'Terracotta' in img_info['categories']: # just get everything that's not broken # else: if outsize: try: img = dip.imread(id_to_img_name(img_id)) except: continue print(img.shape) new_img = dip.resize(img, dsize=outsize, interpolation=INTER_AREA) dip.im_write(new_img, id_to_out_name(img_id)) else: shutil.copyfile(id_to_img_name(img_id), id_to_out_name(img_id))
def main_pix2pix(): def out_pix2pix_name(): global _pix2pix_counter outname = f'{_pix2pix_dir}/{_pix2pix_counter}.jpg' _pix2pix_counter += 1 return outname for f_img in glob.glob(dir_in + '/*'): try: img = dip.imread(f_img) except: continue img_out = dip.resize(img, _pix2pix_outsize, interpolation=INTER_AREA) print(f_img, img.shape) if len(img.shape) == 3: gray = np.mean(img, axis=-1) else: # gray = img continue grad = dip.transforms.edge_detect(gray) mask, m_min, m_max, n_min, n_max = space_fill(grad) for n in range(n_fragments): # frag_size = max(img.shape[0], img.shape[1]) // 4 frag_size = img.shape[0]//4, img.shape[1]//4 frag, _ = fragment(img, m_min, m_max, n_min, n_max, frag_size=frag_size) if frag is None: break else: frag = dip.resize(frag, _pix2pix_outsize, interpolation=INTER_AREA) both = np.concatenate([img_out, frag], axis=1) # plt.imshow(both) # plt.show() dip.im_write(both, out_pix2pix_name())
def main_biggan(): for f_img in glob.glob(dir_in + '/*'): img = dip.imread(f_img) img_id = int(os.path.split(f_img)[-1].split('.')[0]) print(f_img, img.shape) if len(img.shape) == 3: gray = np.mean(img, axis=-1) else: # gray = img continue grad = dip.transforms.edge_detect(gray) mask, m_min, m_max, n_min, n_max = space_fill(grad) grad = mark_image_box(grad, m_min, m_max, n_min, n_max) frag_failed = False for n in range(n_fragments): frag, _ = fragment(img, m_min, m_max, n_min, n_max) if frag is None: frag_failed = True break dip.im_write(frag, out_frag(img_id, n)) if frag_failed: continue shutil.copyfile(f_img, out_img(img_id))
def main_site_vasegen(): for f_img in tqdm(glob.glob(dir_in + '/*')): try: img = dip.imread(f_img) except: continue img_id = int(os.path.split(f_img)[-1].split('.')[0]) # img_out = dip.resize(img, _pix2pix_outsize, interpolation=INTER_AREA) print(f_img, img.shape) if len(img.shape) == 3: gray = np.mean(img, axis=-1) else: # gray = img continue grad = dip.transforms.edge_detect(gray) mask, m_min, m_max, n_min, n_max = space_fill(grad) trimx = grad.shape[0] // 20 trimy = grad.shape[1] // 20 grad[:trimx, :] = 0 grad[-trimx:, :] = 0 grad[:, :trimy] = 0 grad[:, -trimy:] = 0 markerx = _pix2pix_marker_size * ( grad.shape[0]) // _pix2pix_outsize[0] // 2 markery = _pix2pix_marker_size * ( grad.shape[1]) // _pix2pix_outsize[1] // 2 grad_top = grad > np.percentile(grad, 99) border_inds = np.argwhere(grad_top) hull = ConvexHull(border_inds) _frag_context = np.zeros_like(grad_top) mm = list(np.ndindex(_frag_context.shape[:2])) out_hull = isInHull(mm, hull) mm = np.array(mm)[out_hull] # print(mm) # input(out_hull) _frag_context = np.stack([255 * ~_frag_context] * 3 + [_frag_context], axis=-1).astype(np.uint8) _frag_context[mm[:, 0], mm[:, 1], :3] = img[mm[:, 0], mm[:, 1]] _frag_context[mm[:, 0], mm[:, 1], 3] = 255 dip.im_write(_frag_context, out_img(img_id))
def _test_restore_mode(self, file, deg_type, save_images=False, name=None): fh = ImageFileHandler() im_degrader = ImageDegrader() im = fh.open_image_file_as_matrix(file) degraded_im = im_degrader.degrade(im, degradation_type=deg_type, severity_value=.5) restored_im, clustered_im, h_params = self.multiplicative_clustering_restore( degraded_im) restored_im_2 = self.fast_multiplicative_restore( degraded_im, h_param=np.mean(h_params), search_window_size=21) if save_images: dip.im_write(dip.float_to_im(degraded_im), "./" + name + "_degraded_image.jpg", quality=95) dip.im_write(dip.float_to_im(restored_im), "./" + name + "_restored_image.jpg", quality=95) dip.im_write(dip.float_to_im(clustered_im), "./" + name + "_clustered_image.jpg", quality=95) dip.figure() dip.subplot(141) dip.imshow(im, cmap="gray") dip.subplot(142) dip.imshow(degraded_im, cmap="gray") dip.xlabel("PSNR: {0:.2f}, SSIM: {1:.2f}".format( dip.PSNR(im, degraded_im), dip.SSIM(im, degraded_im)[0])) dip.subplot(143) dip.imshow(restored_im, cmap="gray") dip.xlabel("PSNR: {0:.2f}, SSIM: {1:.2f}".format( dip.PSNR(im, restored_im), dip.SSIM(im, restored_im)[0])) dip.subplot(144) dip.imshow(restored_im_2, cmap="gray") dip.xlabel("PSNR: {0:.2f}, SSIM: {1:.2f}".format( dip.PSNR(im, restored_im_2), dip.SSIM(im, restored_im_2)[0])) dip.show()
#(c) Reading an image X = dip.im_read(picture_link) #(d) Converting the image to normalized floating point space X = dip.im_to_float(X) X *= 255 #(e) Adding Constant to Image Y = X + 75 #(f) Renormalize the image and covert to integer Y = dip.float_to_im(Y/255) #(g) Writing an image to disk dip.im_write(Y, save_link_1) #(h) Square Intenstiy and write image to disk Z = X**2 Z = dip.float_to_im(Z/255) Z = dip.im_write(Z, save_link_2) #(i) Compute FFT of X fX = dip.fft2(X) fX = dip.fftshift(fX) fX = np.log(np.abs(fX)) #(j) Save and show the resulting spectrum dip.imshow(fX) dip.show()
# (3) Find the min-area contour _, cnts, _ = cv2.findContours(threshed, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cntsSorted = sorted(cnts, key=cv2.contourArea) cnt = cntsSorted[-1] # (4) Create mask and do bitwise-op mask = np.zeros(img.shape[:2], np.uint8) cv2.drawContours(mask, [cnt], -1, 255, -1) dst = cv2.bitwise_and(img, img, mask=mask) # (5) Crop image to JUST around the object, resize so object is in center x, y, w, h = util.find_bounding_rect(dst) dstcrop = dst[y:y + h, x:x + w] dstcrop = util.resize_image(dstcrop, M_max, N_max, 'constant') # (6) Logical OR - for accumulation of images accum_object = cv2.bitwise_or(dstcrop, accum_object) accum_total = cv2.bitwise_or(dstcrop, accum_total) x, y, w, h = util.find_bounding_rect(accum_object) accum_object = accum_object[y:y + h, x:x + w] dip.im_write(accum_object, PATH_OUT_TEMPLATE_IMAGES + 'template_' + str(template_id).zfill(2) + ".jpg") template_id = template_id + 1 x, y, w, h = util.find_bounding_rect(accum_total) accum_total = accum_total[y:y + h, x:x + w] dip.im_write(accum_total, PATH_OUT_TEMPLATE_IMAGES + 'template_master.jpg')
########################### # RESIZING IMAGES # ########################### dir_no = "../grayscale_data/no/" dir_yes = "../grayscale_data/yes/" dirs = [dir_no, dir_yes] for directory in dirs: for filename in os.listdir(directory): im = dip.im_read(directory + filename) out = dip.resize(im, (350, 300), interpolation=cv2.INTER_CUBIC) filename_without_extension = os.path.splitext(filename)[0] if directory == dir_no: dip.im_write(out, "../resized_data/no/" + filename_without_extension + ".png", quality=95) # 95 is the best possible image quality elif directory == dir_yes: dip.im_write(out, "../resized_data/yes/" + filename_without_extension + ".png", quality=95) # 95 is the best possible image quality ########################### # VARYING CONTRAST # ########################### dir_no = "../resized_data/no/" dir_yes = "../resized_data/yes/" dirs = [dir_no, dir_yes] for directory in dirs:
def save_matrix_as_image_file(self, matrix, image_path): dip.im_write(dip.float_to_im(matrix), image_path)
## Change the path to where you want to save the resulting images path_mix = "/Users/chuchu/Dropbox/gt_exp/set2/image_mix/" ## o_c_e = [(1, 0, 0), (0, 1, 0), (0, 0, 1), (0.5, 0.25, 0.25), (0.25, 0.5, 0.25), (0.25, 0.25, 0.5), (0.33, 0.33, 0.33)] dir = path_mix if not os.path.exists(dir): os.makedirs(dir) for event in range(3, len(o_c_e)): dir = path_mix + 'event_' + str(o_c_e[event][0]) + '_' + str( o_c_e[event][1]) + '_' + str(o_c_e[event][2]) if not os.path.exists(dir): os.makedirs(dir) for filename in os.listdir(path_orgin): path_image_origin = path_orgin + filename path_image_contrast = path_contrast + filename path_image_edge = path_edge + filename im_origin = dip.im_read(path_image_origin) # read icmage im_contrast = dip.im_read(path_image_contrast) # read icmage im_edge = dip.im_read(path_image_edge) # read icmage im_mix = (im_origin * o_c_e[event][0] + im_contrast * o_c_e[event][1] + im_edge * o_c_e[event][2]).astype(np.uint8) save_name = dir + '/' + filename[:-4] + '.png' dip.im_write(im_mix, save_name)
import numpy as np import matplotlib.pyplot as plt import os path = "/Users/chuchu/Dropbox/gt_exp/ddb1_fundusimages/" # Change the path to where you store the images modes = [2, 3, 4] for m in range(len(modes)): dir = 'upsample_img/'+str(modes[m]); if not os.path.exists(dir): os.makedirs(dir) for m in range(len(modes)): for filename in os.listdir(path): mode = modes[m] file_path = path + filename f = dip.im_read(file_path) # read icmage f = dip.im_to_float(f) M = np.array([[1/mode, 0], [0, 1/mode]]) f_up_0 = dip.sampling.resample(f[:,:,0], M, interp= 'bilinear') f_up_1 = dip.sampling.resample(f[:, :, 1], M, interp='bilinear') f_up_2 = dip.sampling.resample(f[:, :, 2], M, interp='bilinear') h, l = f_up_0.shape f_up = np.zeros((h, l, 3)) f_up[:,:,0] = f_up_0 f_up[:, :, 1] = f_up_1 f_up[:, :, 2] = f_up_2 upsample_img = 'upsample_img/' + str(mode)+ '/' +filename[:-4] + '_up' + str(mode) + '.jpg' save_im = dip.float_to_im(f_up) dip.im_write(save_im, upsample_img)
import dippykit as dip from cv2 import INTER_AREA, INTER_CUBIC try: outsize = (int(sys.argv[1]) * 2, int(sys.argv[1])) except: print('provide a number for the side of the square image to scale to') exit() _pix2pix_indir = 'data/processed/pix2pix_vase_fragments/train/' _pix2pix_outdir = f'data/processed/pix2pix_vase_fragments_{sys.argv[1]}/train/' out_pix2pix = lambda fname: f'{_pix2pix_outdir}/{fname}.jpg' if os.path.exists(_pix2pix_outdir): y_n = input(f'Folder {_pix2pix_outdir} exists, overwrite?') if 'y' not in y_n: exit() os.makedirs(_pix2pix_outdir, exist_ok=True) for f_img in tqdm(glob.glob(_pix2pix_indir + '/*.jpg')): try: img = dip.imread(f_img) except: continue img_name = os.path.split(f_img)[-1] img = dip.resize(img, outsize, interpolation=INTER_CUBIC) dip.im_write(img, out_pix2pix(img_name))
dir = 'enhance/'; if not os.path.exists(dir): os.makedirs(dir) for mode in modes: for filename in os.listdir(path): file_path = path + filename f = dip.im_read(file_path) #read icmage f = dip.im_to_float(f) f_g = rgb2gray(f) f_edge = dip.transforms.edge_detect(f_g, mode = mode, as_bool=False); edgename = 'edge/' + filename[:-4] + '.png'; if mode is not 'canny': f_edge *= 1/f_edge.max() save_im = dip.float_to_im(f_edge) dip.im_write(save_im, edgename) f[:,:,0] += f_edge f[:, :, 1] += f_edge f[:, :, 2] += f_edge enhance_img = 'enhance/' + filename[:-4] + '.png'; save_im = dip.float_to_im(f) dip.im_write(save_im, enhance_img) ####combine all # # for filename in os.listdir(path): # file_path = path + filename # f = dip.im_read(file_path) # read icmage # f = dip.im_to_float(f) # f_g = rgb2gray(f)
def main_pix2pix_context(): def out_pix2pix_name(): global _pix2pix_counter outname = f'{_pix2pix_dir}/{_pix2pix_counter}.jpg' _pix2pix_counter += 1 return outname for f_img in tqdm(glob.glob(dir_in + '/*')): try: img = dip.imread(f_img) except: continue img_id = int(os.path.split(f_img)[-1].split('.')[0]) # img_out = dip.resize(img, _pix2pix_outsize, interpolation=INTER_AREA) print(f_img, img.shape) if len(img.shape) == 3: gray = np.mean(img, axis=-1) else: # gray = img continue grad = dip.transforms.edge_detect(gray) mask, m_min, m_max, n_min, n_max = space_fill(grad) trimx = grad.shape[0] // 20 trimy = grad.shape[1] // 20 grad[:trimx, :] = 0 grad[-trimx:, :] = 0 grad[:, :trimy] = 0 grad[:, -trimy:] = 0 markerx = _pix2pix_marker_size * (grad.shape[0]) // _pix2pix_outsize[0] // 2 markery = _pix2pix_marker_size * (grad.shape[1]) // _pix2pix_outsize[1] // 2 grad_top = grad > np.percentile(grad, 99) border_inds = np.argwhere(grad_top) hull = ConvexHull(border_inds) perim = border_inds[hull.vertices] perim = np.concatenate((perim, border_inds[hull.vertices[:1]]), axis=0) _frag_context = np.zeros_like(grad_top) for a, b in zip(perim[:-1], perim[1:]): a, b = np.array(a), np.array(b) for t in np.linspace(0, 1, max(grad.shape)): p = t * a + (1 - t) * b ind = np.round(p).astype(np.int) mstart = max(ind[0] - markerx, 0) mstop = min(ind[0] + markerx, grad.shape[0] - 1) nstart = max(ind[1] - markery, 0) nstop = min(ind[1] + markery, grad.shape[1] - 1) _frag_context[mstart:mstop, nstart:nstop] = 1 mm = list(np.ndindex(_frag_context.shape[:2])) out_hull = isInHull(mm, hull) mm = np.array(mm)[~out_hull] # print(mm) # input(out_hull) _frag_context = np.stack([255*~_frag_context]*3, axis=-1).astype(np.uint8) _frag_context[mm[:, 0], mm[:, 1]] = img[mm[:, 0], mm[:, 1]] # binary erosion - erodes binary blocks like sand bars # frag_context2 = frag_context & binary_erosion(frag_context) # threshold/contour method # ret, thresh = cv2.threshold(grad, np.percentile(grad, 95), 255, 0) # contours, hier = cv2.findContours(thresh.astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # print(contours, hier.shape) # frag_context = np.zeros_like(grad) # cv2.drawContours(frag_context, contours, -1, 255, 3) # draw contours according to hierarchy # frag_context2 = np.zeros_like(grad) # hier = (hier - np.min(hier)) / (np.max(hier) - np.min(hier)) # for h, contlist in zip(hier[0], contours): # for cont in contlist: # frag_context2[cont[0, 1], cont[0, 0]] = max(h) # otsu thresholding # val = filters.threshold_otsu(gray); frag_context = gray < val for n in range(n_fragments): # frag_size = max(img.shape[0], img.shape[1]) // 4 frag_size = img.shape[0]//4, img.shape[1]//4 frag, frag_pos = fragment(img, m_min, m_max, n_min, n_max, frag_size=frag_size) fragx = slice(frag_pos[0], frag_pos[0]+frag_size[0]) fragy = slice(frag_pos[1], frag_pos[1]+frag_size[1]) if frag is None: break else: frag_context = np.copy(_frag_context) white = (255, 255, 255) frag_context[fragx, fragy][frag != white] = frag[frag != white] # img = np.copy(img) # img[fragx, fragy][frag != white] = frag[frag != white] # frag_context_out = dip.resize(frag_context, _pix2pix_outsize, interpolation=INTER_AREA) # frag_context_out = frag_context_out[:, :, 0] # print(img_out.shape, frag_context_out.shape) # print(img_out.dtype, frag_context_out.dtype) # both = np.concatenate([img_out, frag_context_out], axis=1) both = np.concatenate([img, frag_context], axis=1) # plt.subplot(221) # plt.imshow(img) # plt.subplot(222) # plt.imshow(grad) # plt.subplot(223) # plt.imshow(frag_context) # plt.subplot(224) # plt.imshow(both) # plt.show() dip.im_write(both, out_pix2pix(img_id, n))