-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_PreClipImagesOrRaw.py
193 lines (139 loc) · 6.61 KB
/
_PreClipImagesOrRaw.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
##################################################################################
# This script is used to clip the reconverted 16bit grayscale PNGs to the area #
# of interest. This can greatly reduce the consumed time for the data-extraction #
# of the images. #
# #
# How to use (variable explanation): #
# parentDir: This folder is scanned recursevly for possible candidates of #
# a possible measurement-folder. #
# picDir: Name of the subfolder where the PyCam2 pictures are stored #
# in. #
# clipInBayerSpace: Support was added for preclipping not only images, but also #
# the ".raw" pickle files in bayer-space. #
# SkipBadSubdirs: If this is enabled, the subfolders of any _XX marked parent #
# are skipped in addition. #
# imgWin: Is used to define the area of interest: #
# 1.) [w, h]: Defines width and height of the image #
# around the image center #
# 1.) [x, y, w, h]: Defines the left upper corner (x, y) and #
# the image size (width, height). #
# Note: The imgWin is ALWAYS the direct indicies at which the #
# images/raw are clipped!
# #
# 2023 © haum (OTH-Regensburg) #
##################################################################################
import os
import time
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
import natsort
import shutil
import pickle
from misc import bcolors
from misc import DiffTime
from misc import DiffToNow
from misc import Time2Human
from misc import Logger
from misc import LogLine
from misc import LogLineOK
###### USER AREA ######
parentDirs = [
# Your (parent)-folderpaths go here
r"<Drive>\<Input Pics folderpath here>", # Topmost Parent --> Scans the child-folders iteratively
]
picDir = "Pics"
skipBadSubDir = True
### Clip-Window
## Note:
# When clipping in bayer-space, only the x-direction is encoded in Bayer-Pattern.
# x and w therefore, needs to be multiplied by 1.5 (= 12bit / 8bit)
# Thereby, only even values are allowed !!! EXCEPT !!!, when you need to re-align the RAW-pickles
#
# When clipping an already demosaicked image, there are no such limitations, as there is no index-scaling necessary
#
## Coordinates of the left upper corner (startpoint)
x = int(2)
y = int(0)
## Size of the image in pixels
w = int(848 * 1.5) # Image Width (use *1.5 for bayer-space)
h = int(848) # Image Height
###### DO NOT TOUCH AREA ######
### Determine the cut-indicies first
cx1 = x
cy1 = y
cx2 = x + w
cy2 = y + h
t0 = time.time()
LogLine(t0, "Starting", bcolors.BOLD + "----- PreClipImages -----" + bcolors.ENDC, yFill=15, wFill=0, end="\n")
LogLine(t0, "Toolname:", "Subtool of Raspberry " + bcolors.OKBLUE + "Pi" + bcolors.ENDC + "-i" + bcolors.OKBLUE + "Mage Pro"+ bcolors.ENDC + "cessor", yFill=15, wFill=30, end="\n")
LogLine(t0, "Programmer:", "haum", yFill=15, wFill=30, end="\n")
LogLine(t0, "Affiliation:", "OTH-Regensburg", yFill=15, wFill=30, end="\n")
LogLine(t0, "Year:", "2022", yFill=15, wFill=30, end="\n")
_XXBadDirs = list()
for parentDir in parentDirs: # Iterate through parentDirs
for root, dirs, files in os.walk(parentDir): # Iterate recursively through the parentDirs
# Firstly check if path contains one of the already marked bad measurement-folders
if any(root.__contains__(_bDir) for _bDir in _XXBadDirs):
LogLine(None, "Bad parent - skipped: ", root, wFill=0, end="\n")
continue
# Folder marked as bad measurement -> Skip
if root.endswith("_XX"):
if skipBadSubDir == True:
_XXBadDirs.append(root)
LogLine(None, "Marked as bad - skipped: ", root, wFill=0, end="\n")
continue
if root.endswith("AO"):
if skipBadSubDir == True:
_XXBadDirs.append(root)
LogLine(None, "Skipping AO-Folder: ", root, wFill=0, end="\n")
continue
print("")
LogLine(None, "Entering: ", root, wFill=0, end="\n")
#Check if current directory contains measurement-files
# Directory found when it contains a Pics directory and *.dat files
if not dirs.__contains__(picDir) or not any(f.endswith(".dat") for f in files):
LogLine(None, "Nothing interesting here...", wFill=0, end="\n")
continue
else:
LogLine(t0, "Possible directory found: ", root, end="\n")
loadPicDir = os.path.join(root, picDir)
savePicDir = loadPicDir
picFiles = os.listdir(loadPicDir)
picFiles = natsort.natsorted(picFiles, alg=natsort.ns.IGNORECASE) # Be sure that all pictures sorted correctly!
for _iFile in range(len(picFiles)):
pFile = picFiles[_iFile]
if pFile.endswith(".jpg"):
fileIsType = "image"
elif pFile.endswith(".raw"):
fileIsType = "RAW"
else:
fileIsType = ".png" # by default, png ist assumed
# continue
lPath = os.path.join(loadPicDir, pFile)
sPath = lPath
if fileIsType == ".jpg": # JPGs are converted into PNGs
sPath = sPath.replace(".jpg", ".png")
# else: # It's a raw or png (sPath = sPath)
# sPath = sPath.replace(".raw", ".raw")
LogLine(None, yellowMsg=pFile, whiteMessage="Processing Image ", yFill=50, wFill=0, end="\r")
if fileIsType == ".jpg" or fileIsType == ".png":
img = cv.imread(lPath, cv.IMREAD_ANYDEPTH | cv.IMREAD_GRAYSCALE) # Load image
img = img[cy1:cy2, cx1:cx2] # Clip image
# Override the image!
cv.imwrite(sPath, img)
else: # It's a .raw --> Clip in bayer-space
# Load in RAW-pickle
fImg = open(lPath, "rb")
img = pickle.load(fImg)
fImg.close()
img = img[cy1:cy2, cx1:cx2] # Actual clip
# Store back
fImg = open(lPath, "wb")
pickle.dump(img, fImg)
fImg.close()
if fileIsType == ".jpg":
os.remove(lPath) # Do not remove. PNG and RAW overwrites itself
LogLineOK()
print("\n")
LogLine(t0, "Finished", bcolors.BOLD + "----- PreClipImages -----" + bcolors.ENDC, yFill=15, wFill=0, end="\n")