Diff of /scripts/Overlay.py [000000] .. [c52ce0]

Switch to unified view

a b/scripts/Overlay.py
1
from PyQt5.QtCore import QThread, pyqtSignal
2
from skimage import color
3
import numpy as np
4
from natsort import natsorted
5
import os
6
from PIL import Image
7
8
9
10
class Overlay(QThread):
11
    info = pyqtSignal(str)
12
    countChanged = pyqtSignal(int)
13
    figures = pyqtSignal()
14
    maxcuts = pyqtSignal(int)
15
16
    def __init__(self, path, save=False):
17
        super().__init__()
18
        self.inputpath = path
19
        self.save = save
20
        self.threshold = None
21
22
    def run(self, debug=False):
23
        files = [f for f in natsorted(os.listdir(self.inputpath)) if not f.endswith("Overlay.png")]
24
        self.maxcuts.emit(len([f for f in files if f.endswith('.png')]))
25
        i = 0
26
        for file in files:
27
            if file.endswith('.png'):
28
                self.info.emit("Preparing "+file)
29
                print(file)
30
31
                image = np.array(Image.open(self.inputpath+os.sep+file))[:, :, :3]
32
33
                # image = mpimg.imread(self.inputpath+os.sep+file)[:,:,:3]
34
                self.info.emit("Reading " + file)
35
                self.current_image = image[::10, ::10]
36
                self.figures.emit()
37
                self.overlay(image,filename=file, debug=debug, save=self.save)
38
                self.countChanged.emit(int(i))  # EMIT the loading bar
39
            self.info.emit(file + " Overlay Done")
40
            i += 1
41
        self.info.emit("All Overlays Saved - ready")
42
43
    def overlay(self, image, filename, debug=False, save=False):
44
45
        img_hsv = color.rgb2hsv(image)
46
        img_hue = img_hsv[:, :, 0]
47
        image_sat = img_hsv[:, :, 1]
48
        hue = np.logical_and(img_hue > 0.02, img_hue < 0.10)  # BROWN PIXELS BETWEEN 0.02 and 0.10
49
        self.info.emit("Preparing thresholds for " + filename)
50
        if self.threshold:
51
            mask = np.logical_and(hue, image_sat > self.threshold)
52
        else:
53
            print("normal threshold")
54
            mask = np.logical_and(hue, image_sat > 0.79)
55
        mask = mask.astype(int)
56
        self.current_image = mask[::10, ::10]
57
        self.figures.emit()
58
        self.info.emit("Creating overlay for " + filename)
59
        alpha = 0.9
60
        imbw = color.rgb2gray(image)
61
        rows, cols = imbw.shape
62
        # Construct a colour image to superimpose
63
        color_mask = np.zeros((rows, cols, 3))
64
        color_mask[:, :, 1] = mask  # Change to 0,1,2, for rgb
65
        # Construct RGB version of grey-level image
66
        img_color = np.dstack((imbw, imbw, imbw))
67
        img_hsv = color.rgb2hsv(img_color)
68
        color_mask_hsv = color.rgb2hsv(color_mask)
69
        img_hsv[..., 0] = color_mask_hsv[..., 0]
70
        img_hsv[..., 1] = color_mask_hsv[..., 1] * alpha
71
        self.info.emit("Converting to RGB " + filename)
72
        img_masked = color.hsv2rgb(img_hsv)
73
        # TODO : emit this fig nicely
74
        # self.current_image = img_masked[img_masked.shape[0]-200:img_masked.shape[0]+200,
75
        #                      img_masked.shape[0]-200:img_masked.shape[0]+200]
76
        # self.figures.emit()
77
        print("displaying output")
78
        self.info.emit("Saving " + filename)
79
80
        imagesave = Image.fromarray((img_masked * 255).astype(np.uint8))
81
        imagesave.save(self.inputpath+os.sep+filename+'_Overlay.png')
82
83
        # mpimg.imsave(self.inputpath+os.sep+filename+'_Overlay.png', img_masked)