第七天 opencv 2d直方图和直方图反投影

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

def back_projection_demo():
    sample = cv.imread("C:/Users/weiqiangwen/Desktop/sest/data/pic4.png")
    target = cv.imread("C:/Users/weiqiangwen/Desktop/sest/data/pic5.png")
    roi_hsv =cv.cvtColor(sample,cv.COLOR_BGR2HSV)
    target_hsv = cv.cvtColor(target,cv.COLOR_BGR2HSV)
    cv.imshow("sample",sample)
    cv.imshow("tatget",target)
    roiHist = cv.calcHist([roi_hsv],[0,1],None,[180,256],[0,100,0,256])
    cv.normalize(roiHist, roiHist, 0, 255, cv.NORM_MINMAX) ##归一化
    dst = cv.calcBackProject([target_hsv], [0, 1], roiHist, [0, 180, 0, 256], 1)  #直方图反投影
    cv.imshow("backProjectionDemo", dst)

def hist2d_demo(image):                             #2d值方图
    hsv = cv.cvtColor(image,cv.COLOR_BGR2HSV)
    hist = cv.calcHist([image], [0, 1], None, [30, 64], [0, 180, 0, 256])
    #cv.imshow("hist2d", hist)
    plt.imshow(hist,interpolation='nearest')
    plt.title("2D Histogram")
    plt.show()


# rgbHist = np.zeros([16*16*16, 1], np.float32)
# print(rgbHist)
src = cv.imread("C:/Users/weiqiangwen/Desktop/sest/data/pic3.png")
# cv.namedWindow("input contours",cv.WINDOW_AUTOSIZE)
cv.imshow("contours", src)
back_projection_demo()
hist2d_demo(src)
cv.waitKey(0)

cv.destroyAllWindows()
print("--------- Python OpenCV Tutorial ---------")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值