Beispiel #1
0
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))
Beispiel #2
0
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)
Beispiel #3
0
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())
Beispiel #4
0
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))
Beispiel #5
0
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))
Beispiel #6
0
from tqdm import tqdm
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))
Beispiel #7
0
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))
Beispiel #8
0
import dippykit as dip
import numpy as np

path = '../medical_data/mp4_probe_overlayed/extracted_frames/vid1/img0001.jpg'
im = dip.imread(path)

print(im.shape)