makeMRCthumbnails.py 2.92 KB
Newer Older
Adrian Nievergelt's avatar
Adrian Nievergelt committed
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed May 15 09:22:40 2019

@author: adrian
"""

import configparser
import os, sys
from pprint import pprint
import re
import argparse
import mrcfile
import cv2
import numpy as np

parser = argparse.ArgumentParser(description = "Generates thumbnails for MRC microscope data.")

# parser.add_argument("-f", "--file", help="create overlay for a single file",
parser.add_argument('-i', '--input', nargs='?', default='.', help='Input MDOC file to process.')
parser.add_argument('-d', '--inDir', nargs='?', default='.', help='If specified, automatically process entire directory of mrc files.')
parser.add_argument('-mi', '--maxImages', nargs='?', type=float, default=30, help='If more images than specified are in a file, make amontage with a subset of the frames present instead of separate files. Detault is 30 images, minimum 6.')
# parser.add_argument('-g', '--graph', action='store_true', help='Store a graph of the corrected and uncorrected line intensities for debug purposes.')

args = parser.parse_args()

def FakeSecHead(fp): yield '[general]\n'; yield from fp

if args.input == '.':
    print("Missing input file");
    print(parser.print_help())
    sys.exit()

if args.maxImages < 6:
    print("Montage threshold needs to be at least 6.");
    print(parser.print_help())
    sys.exit()

parser = configparser.ConfigParser()

inFile = args.input.strip()
mrc = mrcfile.mmap(inFile)

inFileParts = os.path.splitext(inFile)

if len(mrc.data.shape) == 2:
    zSlices = 1
else:
    zSlices = mrc.data.shape[0]

makeMontage = zSlices > args.maxImages

if makeMontage:
    xOutSize = 400;
    montImages = []
57
    # print("Making a montage")
Adrian Nievergelt's avatar
Adrian Nievergelt committed
58
59
60
61
62
63
64
65
66
67
68
else:
    xOutSize = 1000;

if makeMontage:
    outputSlices = np.linspace(0, zSlices-1, 6).astype(int) # select 6 slices throughout the stack
else:
    # likely stack of normal images, process them all into frames
    outputSlices = range(zSlices)

outputFormat = "Writing output file %s."

69
for slice in outputSlices:
Adrian Nievergelt's avatar
Adrian Nievergelt committed
70
71
72
73
74
75
76
    if zSlices == 1:
        image = mrc.data
    else:
        image = mrc.data[slice]
    
    yOutSize = int(xOutSize/image.shape[0]*image.shape[1])
    
77
    # flip horizontally
78
    image = cv2.flip(image,1)
Adrian Nievergelt's avatar
Adrian Nievergelt committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    image = cv2.GaussianBlur(image, (9,9),4)
    image = cv2.normalize(image, None, 0, 255,cv2.NORM_MINMAX)
    image = cv2.resize(image, (xOutSize, yOutSize))
    image = image.astype(np.uint8)
    image = cv2.equalizeHist(image)

    if makeMontage:
        montImages.append(image)
    else:
        filePath = '%s_%i%s.png' % (inFileParts[0], slice+1, inFileParts[1])
        print(outputFormat % (filePath))
        cv2.imwrite(filePath, image)

if makeMontage:
    
Adrian Nievergelt's avatar
Adrian Nievergelt committed
94
    fullMontage = np.vstack((np.hstack((montImages[0],montImages[1],montImages[2])),np.hstack((montImages[3],montImages[4],montImages[5]))))
Adrian Nievergelt's avatar
Adrian Nievergelt committed
95
    
96
    filePath = '%s_montage%s.png' % (inFileParts[0], inFileParts[1])
Adrian Nievergelt's avatar
Adrian Nievergelt committed
97
    print(outputFormat % (filePath))
Adrian Nievergelt's avatar
Adrian Nievergelt committed
98
    cv2.imwrite(filePath, fullMontage)