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

 

 

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 两个图像相减取绝对值

上一篇:python+requests公共模块部分的封装


下一篇:SpringBoot里的Servlet和实现