-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathStitcher.py
531 lines (503 loc) · 28.8 KB
/
Stitcher.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
import numpy as np
import cv2
# from scipy.stats import mode
import time
import os
import glob
import copy
# import skimage.measure
# from numba import jit
import ImageUtility as Utility
import ImageFusion
import time
class ImageFeature():
# 用来保存串行全局拼接中的第二张图像的特征点和描述子,为后续加速拼接使用,避免重复计算
isBreak = True # 判断是否上一次中断
kps = None
feature = None
class Stitcher(Utility.Method):
'''
图像拼接类,包括所有跟材料显微组织图像配准相关函数
'''
isColorMode = True
direction = 1 # 1: 第一张图像在上,第二张图像在下; 2: 第一张图像在左,第二张图像在右;
# 3: 第一张图像在下,第二张图像在上; 4: 第一张图像在右,第二张图像在左;
directIncre = 1 # 拼接增长方向,可以为1. 0, -1
fuseMethod = "notFuse"
phaseResponseThreshold = 0.15
tempImageFeature = ImageFeature()
imageFusion = ImageFusion.ImageFusion()
def directionIncrease(self, direction):
"""
功能:改变拼接搜索方向,通过direction和directIncre控制,使得范围保持在[1,4]
:param direction: 当前的方向
:return: 返回更新后的方向
"""
direction += self.directIncre
if direction == 5:
direction = 1
if direction == 0:
direction = 4
return direction
def flowStitch(self, fileList, caculateOffsetMethod):
"""
功能:序列拼接,从list的第一张拼接到最后一张,由于中间可能出现拼接失败,故记录截止文件索引
:param fileList: 图像地址序列
:param caculateOffsetMethod:计算偏移量方法
:return: ((status, endfileIndex), stitchImage),((拼接状态, 截止文件索引), 拼接结果)
"""
self.printAndWrite("Stitching the directory which have " + str(fileList[0]))
fileNum = len(fileList)
offsetList = []
describtion = ""
# calculating the offset for small image
startTime = time.time()
status = True
endfileIndex = 0
for fileIndex in range(0, fileNum - 1):
self.printAndWrite("stitching " + str(fileList[fileIndex]) + " and " + str(fileList[fileIndex + 1]))
# imageA = cv2.imread(fileList[fileIndex], 0)
# imageB = cv2.imread(fileList[fileIndex + 1], 0)
imageA = cv2.imdecode(np.fromfile(fileList[fileIndex], dtype=np.uint8), cv2.IMREAD_GRAYSCALE)
imageB = cv2.imdecode(np.fromfile(fileList[fileIndex + 1], dtype=np.uint8), cv2.IMREAD_GRAYSCALE)
if caculateOffsetMethod == self.calculateOffsetForPhaseCorrleate:
(status, offset) = self.calculateOffsetForPhaseCorrleate([fileList[fileIndex], fileList[fileIndex + 1]])
else:
(status, offset) = caculateOffsetMethod([imageA, imageB])
if status == False:
describtion = " " + str(fileList[fileIndex]) + " and " + str(fileList[fileIndex+1]) + " can not be stitched"
break
else:
offsetList.append(offset)
endfileIndex = fileIndex + 1
endTime = time.time()
self.printAndWrite("The time of registering is " + str(endTime - startTime) + "s")
# stitching and fusing
self.printAndWrite("start stitching")
startTime = time.time()
# offsetList = [[1784, 2], [1805, 2], [1809, 2], [1775, 2], [1760, 2], [1846, 2], [1809, 1], [1812, 2], [1786, 1], [1818, 3], [1786, 2], [1802, 2], [1722, 1], [1211, 1], [-10, 2411], [-1734, -1], [-1808, -1], [-1788, -3], [-1754, -1], [-1727, -2], [-1790, -3], [-1785, -2], [-1778, -1], [-1807, -2], [-1767, -2], [-1822, -3], [-1677, -2], [-1778, -2], [-1440, -1], [-2, 2410], [1758, 2], [1792, 2], [1794, 2], [1840, 3], [1782, 2], [1802, 3], [1782, 2], [1763, 3], [1738, 2], [1837, 3], [1781, 2], [1788, 18], [1712, 0], [1271, -11], [-3, 2478], [-1787, -1], [-1812, -2], [-1822, -2], [-1762, -1], [-1725, -2], [-1884, -2], [-1754, -2], [-1747, -1], [-1666, -1], [-1874, -3], [-1695, -2], [-1672, -1], [-1816, -2], [-1411, -1], [-4, 2431], [1874, 3], [1706, -3], [1782, 2], [1794, 2], [1732, 3], [1838, 3], [1721, 1], [1783, 3], [1805, 2], [1725, 3], [1828, 1], [1774, 3], [1776, 1], [1201, 1], [-16, 2405], [-1821, 0], [-1843, -2], [-1758, -2], [-1742, -3], [-1814, -2], [-1817, -2], [-1848, -2], [-1768, -2], [-1749, -2], [-1765, -2], [-1659, -2], [-1832, -2], [-1791, -2], [-1197, -1]]
stitchImage = self.getStitchByOffset(fileList, offsetList)
endTime = time.time()
self.printAndWrite("The time of fusing is " + str(endTime - startTime) + "s")
if status == False:
self.printAndWrite(describtion)
return ((status, endfileIndex), stitchImage)
def flowStitchWithMutiple(self, fileList, caculateOffsetMethod):
"""
功能:多段序列拼接,从list的第一张拼接到最后一张,由于中间可能出现拼接失败,将分段拼接结果共同返回
:param fileList: 图像地址序列
:param caculateOffsetMethod:计算偏移量方法
:return: 拼接的图像list
"""
result = []
totalNum = len(fileList)
startNum = 0
while 1:
(status, stitchResult) = self.flowStitch(fileList[startNum: totalNum], caculateOffsetMethod)
result.append(stitchResult)
self.tempImageFeature.isBreak = True
if status[1] == 1:
startNum = startNum + status[1] + 1
else:
startNum = startNum + status[1] + 1
# self.printAndWrite("status[1] = " + str(status[1]))
# self.printAndWrite("startNum = "+str(startNum))
if startNum == totalNum:
break
if startNum == (totalNum - 1):
# result.append(cv2.imread(fileList[startNum], 0))
if self.isColorMode:
result.append(cv2.imdecode(np.fromfile(fileList[startNum], dtype=np.uint8), cv2.IMREAD_COLOR))
else:
result.append(cv2.imdecode(np.fromfile(fileList[startNum], dtype=np.uint8), cv2.IMREAD_GRAYSCALE))
break
self.printAndWrite("stitching Break, start from " + str(fileList[startNum]) + " again")
return result
def imageSetStitch(self, projectAddress, outputAddress, fileNum, caculateOffsetMethod, startNum = 1, fileExtension = "jpg", outputfileExtension = "jpg"):
"""
功能:图像集拼接方法
:param projectAddress: 项目地址
:param outputAddress: 输出地址
:param fileNum: 共多少个文件
:param caculateOffsetMethod: 计算偏移量方法
:param startNum: 从第几个文件开始拼
:param fileExtension: 输入文件扩展名
:param outputfileExtension:输出文件扩展名
:return:
"""
for i in range(startNum, fileNum+1):
fileAddress = projectAddress + "\\" + str(i) + "\\"
fileList = glob.glob(fileAddress + "*." + fileExtension)
if not os.path.exists(outputAddress):
os.makedirs(outputAddress)
Stitcher.outputAddress = outputAddress
(status, result) = self.flowStitch(fileList, caculateOffsetMethod)
self.tempImageFeature.isBreak = True
cv2.imwrite(outputAddress + "\\stitching_result_" + str(i) + "." + outputfileExtension, result)
if status == False:
self.printAndWrite("stitching Failed")
def imageSetStitchWithMutiple(self, projectAddress, outputAddress, fileNum, caculateOffsetMethod, startNum = 1, fileExtension = "jpg", outputfileExtension = "jpg"):
"""
功能:图像集多段拼接方法
:param projectAddress: 项目地址
:param outputAddress: 输出地址
:param fileNum: 共多少个文件
:param caculateOffsetMethod: 计算偏移量方法
:param startNum: 从第几个文件开始拼
:param fileExtension: 输入文件扩展名
:param outputfileExtension:输出文件扩展名
:return:
"""
for i in range(startNum, fileNum+1):
startTime = time.time()
fileAddress = projectAddress + "\\" + str(i) + "\\"
fileList = glob.glob(fileAddress + "*." + fileExtension)
if not os.path.exists(outputAddress):
os.makedirs(outputAddress)
Stitcher.outputAddress = outputAddress
result = self.flowStitchWithMutiple(fileList, caculateOffsetMethod)
self.tempImageFeature.isBreak = True
if len(result) == 1:
cv2.imwrite(outputAddress + "\\stitching_result_" + str(i) + "." + outputfileExtension, result[0])
# cv2.imwrite(outputAddress + "\\" + outputName + "." + outputfileExtension, result[0])
else:
for j in range(0, len(result)):
cv2.imwrite(outputAddress + "\\stitching_result_" + str(i) + "_" + str(j+1) + "." + outputfileExtension, result[j])
# cv2.imwrite(outputAddress + "\\" + outputName + "_" + str(j + 1) + "." + outputfileExtension,result[j])
endTime = time.time()
print("Time Consuming for " + fileAddress + " is " + str(endTime - startTime))
def calculateOffsetForPhaseCorrleate(self, dirAddress):
"""
功能:采用相位相关法计算偏移量(不完善)
:param dirAddress:
:return:
"""
(dir1, dir2) = dirAddress
offset = [0, 0]
status = True
# phase = phaseCorrelation()
offsetList = self.phase.phaseCorrelation(dir1, dir2)
# print(offset)
# phase.shutdown()
offset = []
offset.append(np.int(np.round(offsetList[1])))
offset.append(np.int(np.round(offsetList[0])))
# offset[0] = np.round(offsetList[0])
# offset[1] = np.round(offsetList[1])
self.printAndWrite(" The offset of stitching: dx is " + str(offset[0]) + " dy is " + str(offset[1]))
return (status, offset)
def calculateOffsetForPhaseCorrleateIncre(self, images):
'''
功能:采用相位相关法计算偏移量-考虑增长搜索区域(不完善)
:param images: [imageA, imageB]
:param registrateMethod: list:
:param fuseMethod:
:param direction: stitching direction
:return:
'''
(imageA, imageB) = images
offset = [0, 0]
status = False
maxI = (np.floor(0.5 / self.roiRatio) + 1).astype(int)+ 1
iniDirection = self.direction
localDirection = iniDirection
for i in range(1, maxI):
# self.printAndWrite(" i=" + str(i) + " and maxI="+str(maxI))
while(True):
# get the roi region of images
# self.printAndWrite(" localDirection=" + str(localDirection))
roiImageA = self.getROIRegionForIncreMethod(imageA, direction=localDirection, order="first", searchRatio = i * self.roiRatio)
roiImageB = self.getROIRegionForIncreMethod(imageB, direction=localDirection, order="second", searchRatio = i * self.roiRatio)
# hann = cv2.createHanningWindow(winSize=(roiImageA.shape[1], roiImageA.shape[0]), type=5)
# (offsetTemp, response) = cv2.phaseCorrelate(np.float32(roiImageA), np.float32(roiImageB), window=hann)
(offsetTemp, response) = cv2.phaseCorrelate(np.float64(roiImageA), np.float64(roiImageB))
offset[0] = np.int(offsetTemp[1])
offset[1] = np.int(offsetTemp[0])
# self.printAndWrite("offset: " + str(offset))
# self.printAndWrite("respnse: " + str(response))
if response > self.phaseResponseThreshold:
status = True
if status == True:
break
else:
localDirection = self.directionIncrease(localDirection)
if localDirection == iniDirection:
break
if status == True:
if localDirection == 1:
offset[0] = offset[0] + imageA.shape[0] - int(i * self.roiRatio * imageA.shape[0])
elif localDirection == 2:
offset[1] = offset[1] + imageA.shape[1] - int(i * self.roiRatio * imageA.shape[1])
elif localDirection == 3:
offset[0] = offset[0] - (imageB.shape[0] - int(i * self.roiRatio * imageB.shape[0]))
elif localDirection == 4:
offset[1] = offset[1] - (imageB.shape[1] - int(i * self.roiRatio * imageB.shape[1]))
self.direction = localDirection
break
if status == False:
return (status, " The two images can not match")
elif status == True:
self.printAndWrite(" The offset of stitching: dx is " + str(offset[0]) + " dy is " + str(offset[1]))
return (status, offset)
def calculateOffsetForFeatureSearch(self, images):
'''
功能:采用特征搜索计算偏移量
:param images: [imageA, imageB]
:return:(status, offset)
'''
(imageA, imageB) = images
offset = [0, 0]
status = False
if self.isEnhance == True:
if self.isClahe == True:
clahe = cv2.createCLAHE(clipLimit=self.clipLimit, tileGridSize=(self.tileSize, self.tileSize))
imageA = clahe.apply(imageA)
imageB = clahe.apply(imageB)
elif self.isClahe == False:
imageA = cv2.equalizeHist(imageA)
imageB = cv2.equalizeHist(imageB)
# get the feature points
if self.tempImageFeature.isBreak == True:
(kpsA, featuresA) = self.detectAndDescribe(imageA, featureMethod=self.featureMethod)
(kpsB, featuresB) = self.detectAndDescribe(imageB, featureMethod=self.featureMethod)
self.tempImageFeature.isBreak = False
self.tempImageFeature.kps = kpsB
self.tempImageFeature.feature = featuresB
else:
kpsA = self.tempImageFeature.kps
featuresA = self.tempImageFeature.feature
(kpsB, featuresB) = self.detectAndDescribe(imageB, featureMethod=self.featureMethod)
self.tempImageFeature.isBreak = False
self.tempImageFeature.kps = kpsB
self.tempImageFeature.feature = featuresB
if featuresA is not None and featuresB is not None:
matches = self.matchDescriptors(featuresA, featuresB)
# match all the feature points
if self.offsetCaculate == "mode":
(status, offset) = self.getOffsetByMode(kpsA, kpsB, matches, offsetEvaluate = self.offsetEvaluate)
elif self.offsetCaculate == "ransac":
(status, offset, adjustH) = self.getOffsetByRansac(kpsA, kpsB, matches, offsetEvaluate = self.offsetEvaluate)
if status == False:
self.tempImageFeature.isBreak = True
return (status, " The two images can not match")
elif status == True:
self.tempImageFeature.isBreak = False
self.printAndWrite(" The offset of stitching: dx is " + str(offset[0]) + " dy is " + str(offset[1]))
return (status, offset)
def calculateOffsetForFeatureSearchIncre(self, images):
'''
功能:采用特征搜索计算偏移量-考虑增长搜索区域
:param images: [imageA, imageB]
:return:(status, offset)
'''
(imageA, imageB) = images
offset = [0, 0]
status = False
maxI = (np.floor(0.5 / self.roiRatio) + 1).astype(int)+ 1
iniDirection = self.direction
localDirection = iniDirection
for i in range(1, maxI):
# self.printAndWrite(" i=" + str(i) + " and maxI="+str(maxI))
while(True):
# get the roi region of images
# self.printAndWrite(" localDirection=" + str(localDirection))
roiImageA = self.getROIRegionForIncreMethod(imageA, direction=localDirection, order="first", searchRatio = i * self.roiRatio)
roiImageB = self.getROIRegionForIncreMethod(imageB, direction=localDirection, order="second", searchRatio = i * self.roiRatio)
if self.isEnhance == True:
if self.isClahe == True:
clahe = cv2.createCLAHE(clipLimit=self.clipLimit,tileGridSize=(self.tileSize, self.tileSize))
roiImageA = clahe.apply(roiImageA)
roiImageB = clahe.apply(roiImageB)
elif self.isClahe == False:
roiImageA = cv2.equalizeHist(roiImageA)
roiImageB = cv2.equalizeHist(roiImageB)
# get the feature points
kpsA, featuresA = self.detectAndDescribe(roiImageA, featureMethod=self.featureMethod)
kpsB, featuresB = self.detectAndDescribe(roiImageB, featureMethod=self.featureMethod)
if featuresA is not None and featuresB is not None:
matches = self.matchDescriptors(featuresA, featuresB)
# self.printAndWrite(" The number of raw matches is " + str(len(matches)))
# match all the feature points
if self.offsetCaculate == "mode":
(status, offset) = self.getOffsetByMode(kpsA, kpsB, matches, offsetEvaluate = self.offsetEvaluate)
elif self.offsetCaculate == "ransac":
(status, offset, adjustH) = self.getOffsetByRansac(kpsA, kpsB, matches, offsetEvaluate = self.offsetEvaluate)
if status:
break
else:
localDirection = self.directionIncrease(localDirection)
if localDirection == iniDirection:
break
if status:
if localDirection == 1:
offset[0] = offset[0] + imageA.shape[0] - int(i * self.roiRatio * imageA.shape[0])
elif localDirection == 2:
offset[1] = offset[1] + imageA.shape[1] - int(i * self.roiRatio * imageA.shape[1])
elif localDirection == 3:
offset[0] = offset[0] - (imageB.shape[0] - int(i * self.roiRatio * imageB.shape[0]))
elif localDirection == 4:
offset[1] = offset[1] - (imageB.shape[1] - int(i * self.roiRatio * imageB.shape[1]))
self.direction = localDirection
break
if status == False:
return (status, " The two images can not match")
elif status == True:
self.printAndWrite(" The offset of stitching: dx is " + str(offset[0]) + " dy is " + str(offset[1]))
return (status, offset)
def getStitchByOffset(self, fileList, originOffsetList):
'''
功能:通过偏移量列表和文件列表得到最终的拼接结果
:param fileList: 图像列表
:param originOffsetList: 偏移量列表
:return: ndarry,图像
'''
# 如果你不细心,不要碰这段代码
# 已优化到根据指针来控制拼接,CPU下最快了
dxSum = dySum = 0
imageList = []
# imageList.append(cv2.imread(fileList[0], 0))
if self.isColorMode:
imageList.append(cv2.imdecode(np.fromfile(fileList[0], dtype=np.uint8), cv2.IMREAD_COLOR))
else:
imageList.append(cv2.imdecode(np.fromfile(fileList[0], dtype=np.uint8), cv2.IMREAD_GRAYSCALE))
resultRow = imageList[0].shape[0] # 拼接最终结果的横轴长度,先赋值第一个图像的横轴
resultCol = imageList[0].shape[1] # 拼接最终结果的纵轴长度,先赋值第一个图像的纵轴
originOffsetList.insert(0, [0, 0]) # 增加第一张图像相对于最终结果的原点的偏移量
rangeX = [[0,0] for x in range(len(originOffsetList))] # 主要用于记录X方向最大最小边界
rangeY = [[0, 0] for x in range(len(originOffsetList))] # 主要用于记录Y方向最大最小边界
# print("originOffsetList=",originOffsetList)
offsetList = copy.deepcopy(originOffsetList)
rangeX[0][1] = imageList[0].shape[0]
rangeY[0][1] = imageList[0].shape[1]
for i in range(1, len(offsetList)):
# self.printAndWrite(" stitching " + str(fileList[i]))
# 适用于流形拼接的校正,并更新最终图像大小
# tempImage = cv2.imread(fileList[i], 0)
if Stitcher.isColorMode:
tempImage = cv2.imdecode(np.fromfile(fileList[i], dtype=np.uint8), cv2.IMREAD_COLOR)
else:
tempImage = cv2.imdecode(np.fromfile(fileList[i], dtype=np.uint8), cv2.IMREAD_GRAYSCALE)
dxSum = dxSum + offsetList[i][0]
dySum = dySum + offsetList[i][1]
# self.printAndWrite(" The dxSum is " + str(dxSum) + " and the dySum is " + str(dySum))
if dxSum <= 0:
for j in range(0, i):
offsetList[j][0] = offsetList[j][0] + abs(dxSum)
rangeX[j][0] = rangeX[j][0] + abs(dxSum)
rangeX[j][1] = rangeX[j][1] + abs(dxSum)
resultRow = resultRow + abs(dxSum)
rangeX[i][1] = resultRow
dxSum = rangeX[i][0] = offsetList[i][0] = 0
else:
offsetList[i][0] = dxSum
resultRow = max(resultRow, dxSum + tempImage.shape[0])
rangeX[i][1] = resultRow
if dySum <= 0:
for j in range(0, i):
offsetList[j][1] = offsetList[j][1] + abs(dySum)
rangeY[j][0] = rangeY[j][0] + abs(dySum)
rangeY[j][1] = rangeY[j][1] + abs(dySum)
resultCol = resultCol + abs(dySum)
rangeY[i][1] = resultCol
dySum = rangeY[i][0] = offsetList[i][1] = 0
else:
offsetList[i][1] = dySum
resultCol = max(resultCol, dySum + tempImage.shape[1])
rangeY[i][1] = resultCol
imageList.append(tempImage)
stitchResult = None
if self.isColorMode:
stitchResult = np.zeros((resultRow, resultCol, 3), np.int) - 1
else:
stitchResult = np.zeros((resultRow, resultCol), np.int) - 1
# stitchResult = np.zeros((resultRow, resultCol), np.int)
self.printAndWrite(" The rectified offsetList is " + str(offsetList))
# 如上算出各个图像相对于原点偏移量,并最终计算出输出图像大小,并构造矩阵,如下开始赋值
for i in range(0, len(offsetList)):
self.printAndWrite(" stitching " + str(fileList[i]))
if i == 0:
if self.isColorMode:
stitchResult[offsetList[0][0]: offsetList[0][0] + imageList[0].shape[0], offsetList[0][1]: offsetList[0][1] + imageList[0].shape[1], :] = imageList[0]
else:
stitchResult[offsetList[0][0]: offsetList[0][0] + imageList[0].shape[0], offsetList[0][1]: offsetList[0][1] + imageList[0].shape[1]] = imageList[0]
else:
if self.fuseMethod == "notFuse":
# 适用于无图像融合,直接覆盖
# self.printAndWrite("Stitch " + str(i+1) + "th, the roi_ltx is " + str(offsetList[i][0]) + " and the roi_lty is " + str(offsetList[i][1]))
if self.isColorMode:
stitchResult[offsetList[i][0]: offsetList[i][0] + imageList[i].shape[0], offsetList[i][1]: offsetList[i][1] + imageList[i].shape[1], :] = imageList[i]
else:
stitchResult[offsetList[i][0]: offsetList[i][0] + imageList[i].shape[0], offsetList[i][1]: offsetList[i][1] + imageList[i].shape[1]] = imageList[i]
else:
# 适用于图像融合算法,切出 roiA 和 roiB 供图像融合
minOccupyX = rangeX[i-1][0]
maxOccupyX = rangeX[i-1][1]
minOccupyY = rangeY[i-1][0]
maxOccupyY = rangeY[i-1][1]
# self.printAndWrite("Stitch " + str(i + 1) + "th, the offsetList[i][0] is " + str(
# offsetList[i][0]) + " and the offsetList[i][1] is " + str(offsetList[i][1]))
# self.printAndWrite("Stitch " + str(i + 1) + "th, the minOccupyX is " + str(
# minOccupyX) + " and the maxOccupyX is " + str(maxOccupyX) + " and the minOccupyY is " + str(
# minOccupyY) + " and the maxOccupyY is " + str(maxOccupyY))
roi_ltx = max(offsetList[i][0], minOccupyX)
roi_lty = max(offsetList[i][1], minOccupyY)
roi_rbx = min(offsetList[i][0] + imageList[i].shape[0], maxOccupyX)
roi_rby = min(offsetList[i][1] + imageList[i].shape[1], maxOccupyY)
# self.printAndWrite("Stitch " + str(i + 1) + "th, the roi_ltx is " + str(
# roi_ltx) + " and the roi_lty is " + str(roi_lty) + " and the roi_rbx is " + str(
# roi_rbx) + " and the roi_rby is " + str(roi_rby))
if self.isColorMode:
roiImageRegionA = stitchResult[roi_ltx:roi_rbx, roi_lty:roi_rby, :].copy()
stitchResult[offsetList[i][0]: offsetList[i][0] + imageList[i].shape[0], offsetList[i][1]: offsetList[i][1] + imageList[i].shape[1], :] = imageList[i]
roiImageRegionB = stitchResult[roi_ltx:roi_rbx, roi_lty:roi_rby, :].copy()
stitchResult[roi_ltx:roi_rbx, roi_lty:roi_rby, :] = self.fuseImage([roiImageRegionA, roiImageRegionB], originOffsetList[i][0], originOffsetList[i][1])
else:
roiImageRegionA = stitchResult[roi_ltx:roi_rbx, roi_lty:roi_rby].copy()
stitchResult[offsetList[i][0]: offsetList[i][0] + imageList[i].shape[0], offsetList[i][1]: offsetList[i][1] + imageList[i].shape[1]] = imageList[i]
roiImageRegionB = stitchResult[roi_ltx:roi_rbx, roi_lty:roi_rby].copy()
stitchResult[roi_ltx:roi_rbx, roi_lty:roi_rby] = self.fuseImage([roiImageRegionA, roiImageRegionB], originOffsetList[i][0], originOffsetList[i][1])
# print("originOffsetList=", originOffsetList)
stitchResult[stitchResult == -1] = 0
return stitchResult.astype(np.uint8)
def fuseImage(self, images, dx, dy):
"""
功能:融合图像
:param images: [imageA, imageB]
:param dx: x方向偏移量
:param dy: y方向偏移量
:return:
"""
self.imageFusion.isColorMode = self.isColorMode
(imageA, imageB) = images
if self.fuseMethod != "fadeInAndFadeOut" and self.fuseMethod != "trigonometric":
# 将各自区域中为背景的部分用另一区域填充,目的是消除背景
# 权值为-1是为了方便渐入检出融合和三角融合计算
imageA[imageA == -1] = 0
imageB[imageB == -1] = 0
imageA[imageA == 0] = imageB[imageA == 0]
imageB[imageB == 0] = imageA[imageB == 0]
fuseRegion = np.zeros(imageA.shape, np.uint8)
if self.fuseMethod == "notFuse":
fuseRegion = imageB
elif self.fuseMethod == "average":
fuseRegion = self.imageFusion.fuseByAverage([imageA, imageB])
elif self.fuseMethod == "maximum":
fuseRegion = self.imageFusion.fuseByMaximum([imageA, imageB])
elif self.fuseMethod == "minimum":
fuseRegion = self.imageFusion.fuseByMinimum([imageA, imageB])
elif self.fuseMethod == "fadeInAndFadeOut":
fuseRegion = self.imageFusion.fuseByFadeInAndFadeOut(images, dx, dy)
elif self.fuseMethod == "trigonometric":
fuseRegion = self.imageFusion.fuseByTrigonometric(images, dx, dy)
elif self.fuseMethod == "multiBandBlending":
assert self.isColorMode is False, "The multi Band Blending is not support for color mode in this code"
fuseRegion = self.imageFusion.fuseByMultiBandBlending([imageA, imageB])
elif self.fuseMethod == "optimalSeamLine":
assert self.isColorMode is False, "The optimal seam line is not support for color mode in this code"
fuseRegion = self.imageFusion.fuseByOptimalSeamLine(images, self.direction)
return fuseRegion
if __name__=="__main__":
stitcher = Stitcher()
imageA = cv2.imread(".\\images\\dendriticCrystal\\1\\1-044.jpg", 0)
imageB = cv2.imread(".\\images\\dendriticCrystal\\1\\1-045.jpg", 0)
offset = stitcher.calculateOffsetForFeatureSearchIncre([imageA, imageB])