首页 > 编程语言 > 详细

python opencv 两个图像相减取绝对值

时间:2021-08-23 20:23:12      阅读:17      评论:0      收藏:0      [点我收藏+]

 

 

import cv2
import os
import shutil

bgr_imgs = os.listdir("./imgs_sub/bgr/")
bgr_dir = "./imgs_sub/bgr/"
human_dir = "./imgs_sub/human/"
diff_dir = "./imgs_sub/absdiff/"
# human_img = os.listdir("./imgs_sub/human")
width = 3840
height = 2160

if os.path.exists(diff_dir):
    shutil.rmtree(diff_dir)
    os.mkdir(diff_dir)
    print(diff_dir, " create!")
else:
    os.mkdir(diff_dir)
    print(diff_dir, " create!")


for img in bgr_imgs:
    print(img)
    bgr_mat = cv2.imread(bgr_dir+img,0)
    img_name = img.split(.)[0]
    human_mat = cv2.imread(human_dir+img_name+".png",0)

    # img_sub = bgr_mat - human_mat
    # cv2.namedWindow("sub", cv2.WINDOW_NORMAL)
    # cv2.resizeWindow("sub", int(width / 3), int(height / 3))
    # bgr_pixel = bgr_mat[0]
    # human_pixel = human_mat[0]
    #
    # sub_pixel = bgr_pixel - human_pixel

    img_abs_sub = cv2.absdiff(bgr_mat, human_mat)

    # print("bgr_pixel: ",bgr_pixel," human_pixel: ",human_pixel, " sub_pixel: ",sub_pixel, " abs_sub_pixel: ", img_abs_sub[0])
    print("bgr_pixel[0][0]: ", bgr_mat[0][0], " human_pixel[0][0]: ", human_mat[0][0], " abs_sub_pixel[0][0]: ",
          img_abs_sub[0][0])

    cv2.imwrite(diff_dir+img, img_abs_sub)

    # cv2.namedWindow("bgr", cv2.WINDOW_NORMAL)
    # cv2.resizeWindow("bgr", int(width / 3), int(height / 3))
    # cv2.namedWindow("human", cv2.WINDOW_NORMAL)
    # cv2.resizeWindow("human", int(width / 3), int(height / 3))
    # cv2.namedWindow("abs_sub", cv2.WINDOW_NORMAL)
    # cv2.resizeWindow("abs_sub", int(width / 3), int(height / 3))

    # cv2.imshow("bgr", bgr_mat)
    # cv2.imshow("human", human_mat)
    # # cv2.imshow("sub", img_sub)
    # cv2.imshow("abs_sub", img_abs_sub)
    # cv2.waitKey(0)

 

python opencv 两个图像相减取绝对值

原文:https://www.cnblogs.com/xiaohuidi/p/15177217.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!