imagestitch.py 2.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
# -*- coding: utf-8 -*-
"""
GEPARD - Gepard-Enabled PARticle Detection
Copyright (C) 2018  Lars Bittrich and Josef Brandt, Leibniz-Institut für 
Polymerforschung Dresden e. V. <bittrich-lars@ipfdd.de>    

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program, see COPYING.  
If not, see <https://www.gnu.org/licenses/>.
"""

import cv2
import numpy as np

def imageStacking(colimgs):
    full = []
    images = []
    laplacians = []
    for img in colimgs:
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        
        lap = cv2.Laplacian(gray, cv2.CV_64F)
        full.append(img)
        images.append(gray)
        blurlap = (cv2.GaussianBlur((lap)**2,(25,25),0))
        laplacians.append(blurlap)
    images = np.array(images)
    laplacians = np.array(laplacians)
    full = np.array(full)
    full = np.uint8(full)
    
    lap = laplacians+.1
    zval = lap.argmax(axis=0)*(255./(lap.shape[0] - (1 if lap.shape[0]>1 else 0)))
    im = np.sum(full * lap[:,:,:,np.newaxis], axis=0)/(lap[:,:,:,np.newaxis].sum(axis=0))
    
    zval = np.uint8(zval)
    im = np.uint8(im)
    return im, zval

def combineImages(path, nx, ny, nk, width, height, angle):
    imgs = []
    full = None
    for i in range(nx):
        for j in range(ny):
            colimgs = []
            for k in range(nk):
                colimgs.append(cv2.imread(path + f'test_{i}_{j}_{k}.bmp'))
            img = imageStacking(colimgs)
            imgs.append(img)
            dx = i*.9*img.shape[1]
            dy = j*.8*img.shape[0]
            c, s = np.cos(np.radians(angle)), np.sin(np.radians(angle))
            M = np.float32([[c,s,dx],[-s,c,dy]])
            dst = cv2.warpAffine(img, M, (int(img.shape[1]*((nx-1)*.9 +1)), int(img.shape[0]*((ny-1)*.8 +1))))
            
            if full is None:
                full = dst
            else:
                full = cv2.max(full,dst)
    cv2.imwrite("full_dunkel.png", full)
    
    
if __name__ == "__main__":
    path = "../Bildserie-Scan/dunkelfeld/"
    
    Nx, Ny, Nk = 10, 10, 4
    width, height, angle = 463.78607177734375, 296.0336608886719, -0.04330849274992943
    
    combineImages(path, Nx, Ny, Nk, width, height, angle)