diff --git a/README.md b/README.md index 7f8520c..c6915da 100644 --- a/README.md +++ b/README.md @@ -125,3 +125,21 @@ | 识别结果 | ![image-20220721203004305](https://raw.githubusercontent.com/Karllzy/imagebed/main/img/image-20220721203004305.png) | | 模型约束情况 | ![image-20220722095620511](https://raw.githubusercontent.com/Karllzy/imagebed/main/img/image-20220722095620511.png) | +## 图像的对齐 + +引入了RGB和光谱图像的原因,这里牵扯到图像对齐的问题。 + +这里可以看到对齐的结果: + +![Figure_1](https://raw.githubusercontent.com/Karllzy/imagebed/main/img/Figure_1.png) + +根据这张图片的换算结果可以得知光谱图像比RGB图像超前了69个像素,大概2.02厘米的样子。 + +![Figure_2](https://raw.githubusercontent.com/Karllzy/imagebed/main/img/Figure_2.png) + +根据这张图片,可以得知,图像上下偏差是2.3厘米 + + +![Figure_4](https://raw.githubusercontent.com/Karllzy/imagebed/main/img/Figure_4.png) + +这张图片里的上下偏差则达到了2.6厘米左右。 diff --git a/config.py b/config.py index 20fa8bb..9d3a5ff 100644 --- a/config.py +++ b/config.py @@ -30,5 +30,9 @@ class Config: threshold_s = 175 rgb_size_threshold = 4 - # Mask parameter - target_size = (1024, 1024) # (Width, Height) of mask + # mask parameter + target_size = (256, 1024) # (Width, Height) of mask + + # save part + offset_vertical = -70 + diff --git a/main.py b/main.py index f396fd0..23d7fec 100755 --- a/main.py +++ b/main.py @@ -25,12 +25,17 @@ def main(): os.mkfifo(rgb_fifo_path, 0o777) if not os.access(mask_fifo_path, os.F_OK): os.mkfifo(mask_fifo_path, 0o777) - if not os.access(rgb_mask_fifo_path, os.F_OK): - os.mkfifo(rgb_mask_fifo_path, 0o777) + + # 进行补偿buffer的开启 + if Config.offset_vertical < 0: + # 纵向的补偿小于0,那就意味着光谱图要上移才能补上,那么我们应该补偿SPEC相机的全 0 图像 + conserve_part = np.zeros((abs(Config.offset_vertical) // 4, Config.nRows, Config.nBands)) + elif Config.offset_vertical > 0: + # 纵向的补偿小于0,说明光谱图下移才能补上去,那么我们就需要补偿RGB相机的全 0 图像 + conserve_part = np.zeros(abs(Config.offset_vertical), Config.nRgbRows, Config.nRgbBands) while True: fd_img = os.open(img_fifo_path, os.O_RDONLY) fd_rgb = os.open(rgb_fifo_path, os.O_RDONLY) - # spec data read data = os.read(fd_img, total_len) if len(data) < 3: @@ -40,7 +45,6 @@ def main(): else: data_total = data os.close(fd_img) - # rgb data read rgb_data = os.read(fd_rgb, total_rgb) if len(rgb_data) < 3: @@ -51,23 +55,34 @@ def main(): else: rgb_data_total = rgb_data os.close(fd_rgb) - # 识别 t1 = time.time() - img_data = np.frombuffer(data_total, dtype=np.float32).reshape((Config.nRows, Config.nBands, -1)) \ - .transpose(0, 2, 1) + img_data = np.frombuffer(data_total, dtype=np.float32).reshape((Config.nRows, Config.nBands, -1))\ + .transpose(0, 2, 1) rgb_data = np.frombuffer(rgb_data_total, dtype=np.uint8).reshape((Config.nRgbRows, Config.nRgbCols, -1)) + if Config.offset_vertical < 0: + # 纵向的补偿小于0,那就意味着光谱图要上移才能补上,那么我们应该补偿SPEC相机的全 0 图像 + new_conserve_part, real_part = img_data[:abs(Config.offset_vertical) // 4, ...],\ + img_data[abs(Config.offset_vertical) // 4:, ...] + img_data = np.concatenate([real_part, conserve_part], axis=0) + conserve_part = new_conserve_part + elif Config.offset_vertical > 0: + # 纵向的补偿小于0,说明光谱图下移才能补上去,那么我们就需要补偿RGB相机的全 0 图像 + new_conserve_part, real_part = rgb_data[:abs(Config.offset_vertical), ...],\ + rgb_data[abs(Config.offset_vertical):, ...] + rgb_data = np.concatenate([real_part, conserve_part], axis=0) + conserve_part = new_conserve_part # 光谱识别 mask_spec = spec_detector.predict(img_data) # rgb识别 mask_rgb = rgb_detector.predict(rgb_data) # 结果合并 - # mask_result = (mask_spec | mask_rgb).astype(np.uint8) + mask_result = (mask_spec | mask_rgb).astype(np.uint8) # control the size of the output masks masks = [cv2.resize(mask.astype(np.uint8), Config.target_size) for mask in [mask_spec, mask_rgb]] # 写出 - output_fifos = [mask_fifo_path, rgb_mask_fifo_path] + output_fifos = [mask_fifo_path, ] for fifo, mask in zip(output_fifos, masks): fd_mask = os.open(fifo, os.O_WRONLY) os.write(fd_mask, mask.tobytes()) @@ -82,7 +97,6 @@ if __name__ == '__main__': rgb_fifo_path = "/tmp/dkrgb.fifo" mask_fifo_path = "/tmp/dkmask.fifo" - rgb_mask_fifo_path = "/tmp/dkmask_rgb.fifo" # 主函数 main() # read_c_captures('/home/lzy/2022.7.15/tobacco_v1_0/', no_mask=True, nrows=256, ncols=1024, diff --git a/utils.py b/utils.py index 2800eaf..9359bc4 100755 --- a/utils.py +++ b/utils.py @@ -140,7 +140,6 @@ def size_threshold(img, blk_size, threshold): return mask - if __name__ == '__main__': color_dict = {(0, 0, 255): "yangeng", (255, 0, 0): "bejing", (0, 255, 0): "hongdianxian", (255, 0, 255): "chengsebangbangtang", (0, 255, 255): "lvdianxian"}