针对图像融合中阈值矩阵加减的阈值溢出问题的解决方案,本文是自己学习后加入自己深入理解的总结记录,方便自己以后查看。
融合阈值溢出是指阈值矩阵相加或相减后的阈值小于0或大于255是阈值为溢出值,不溢出是保证阈值在溢出时保持为0或255。
1、方法一
先将图片uint8转int32做矩阵变化加减,对转换后矩阵做溢出处理,最后转回uint8图片,缺点耗时较长:4096x3840算法耗时120ms
# 算子类
class ImgThreshMerge(OperatorBase):
def __init__(self):
super(ImgThreshMerge, self).__init__()
# 算⼦类的公开成员变量会被认为是该⼯具的算⼦参数
self.blue_thresh = 0 # 类型为int【-255,255】
self.green_thresh = 0 # 类型为int【-255,255】
self.red_thresh = 0 # 类型为int【-255,255】
def execute(self, b_img, g_img, r_img):
assert isinstance(b_img, GrayImage), "input image type error"
assert isinstance(g_img, GrayImage), "input image type error"
assert isinstance(r_img, GrayImage), "input image type error"
image = cv2.merge([b_img, g_img, r_img])
image = image.astype(np.int32)
dim1 = image.shape[0]
dim2 = image.shape[1]
temp_array = np.ones((dim1, dim2), np.int32)
temp_array0 = temp_array * self.blue_thresh
temp_array1 = temp_array * self.green_thresh
temp_array2 = temp_array * self.red_thresh
constant_image = cv2.merge([temp_array0, temp_array1, temp_array2])
output_image = image + constant_image
output_image[output_image > 255] = 255
output_image[output_image < 0] = 0
output_image = np.uint8(output_image)
result = ImgThreshMergeResult()
result.output_merge_image = ColorImage(output_image)
return result
2、方法二
利用np.uint8生成各通道补偿图片,cv2.add和subtract对图像做补偿后融合图片,耗时:4096x3840算法耗时60ms
# 算子类
class ImgThreshMerge(OperatorBase):
def __init__(self):
super(ImgThreshMerge, self).__init__()
# 算⼦类的公开成员变量会被认为是该⼯具的算⼦参数
self.blue_thresh = 0 # 类型为int【-255,255】
self.green_thresh = 0 # 类型为int【-255,255】
self.red_thresh = 0 # 类型为int【-255,255】
def execute(self, b_img, g_img, r_img):
assert isinstance(b_img, GrayImage), "input image type error"
assert isinstance(g_img, GrayImage), "input image type error"
assert isinstance(r_img, GrayImage), "input image type error"
# cv2.createMergeMertens()
# 解决溢出方法二 unit8生成图片,cv2做补偿,38ms
imgth = np.ones((b_img.shape[0], b_img.shape[1]), np.uint8)
b_imgth = imgth * abs(self.blue_thresh)
g_imgth = imgth * abs(self.green_thresh)
r_imgth = imgth * abs(self.red_thresh)
if(self.blue_thresh>=0):
b_output = cv2.add(b_img, b_imgth)
else:
b_output = cv2.subtract(b_img, b_imgth)
if(self.green_thresh>=0):
g_output = cv2.add(g_img, g_imgth)
else:
g_output = cv2.subtract(g_img, g_imgth)
if(self.red_thresh>=0):
r_output = cv2.add(r_img, r_imgth)
else:
r_output = cv2.subtract(r_img, r_imgth)
output_img = cv2.merge([b_output, g_output, r_output])
result = ImgThreshMergeResult()
result.output_merge_image = ColorImage(output_img)
return result
图像融合和算法界面平台开发请查看链接:图像融合(一)__融合效果调试算法平台开发-优快云博客
图像融合CUDA并行加速链接:图像融合(三)__C++和CUDA做加速处理-优快云博客