import cv2
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
X1,Y1 = np.meshgrid(np.linspace(0,256,256),np.linspace(0,180,180))
X,Y = np.meshgrid(np.linspace(0,300,300),np.linspace(0,300,300))
roi = cv2.imread('3.png')
hsv = cv2.cvtColor(roi,cv2.COLOR_BGR2HSV)
##
target = cv2.imread('33.png')
hsvt = cv2.cvtColor(target,cv2.COLOR_BGR2HSV)
print(hsvt.shape)
#计算直方图
roihist = cv2.calcHist([hsv],[0,1],None,[180,256],[0,180,0,256])
np.savetxt("data.txt",roihist)
#归一化，使得值全部在0到255之间
fig3d = plt.figure()
ax = fig3d.gca(projection = '3d')
ax.plot_surface(X1,Y1,roihist,cmap='hot')
cv2.imshow("roihist",roihist)
cv2.normalize(roihist,roihist,0,255,cv2.NORM_MINMAX)
cv2.imshow("roihist_norminalize",roihist)
print("-------")
print(roihist.shape)
plt.figure(2)
plt.plot(roihist[0],'r')
plt.plot(roihist[1],'b')
dst = cv2.calcBackProject([hsvt],[0,1],roihist,[0,180,0,256],1)
print("dst shape",dst.shape)
ret,thresh = cv2.threshold(dst,50,255,0)
cv2.imshow("dst",thresh)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
opening = cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel)
cv2.imshow("opening",opening)
fig = plt.figure()
ax = fig.gca(projection = '3d')
#Z = np.sinc(np.sqrt(X ** 2 + Y ** 2))
#print(Z.shape)
ax.plot_surface(X,Y,dst,cmap='hot')
ret,dst1 = cv2.threshold(dst,100,0,cv2.THRESH_TOZERO_INV)
print(dst1.shape)
fig1 = plt.figure()
ax1 = fig1.gca(projection = '3d')
ax1.plot_surface(X,Y,dst1,cmap='hot')
plt.show()
cv2.waitKey()

c7e599b2e06f90e7d6b238973d54f2eea38ab5c0
<mask token>

<mask token>
print(hsvt.shape)
<mask token>
np.savetxt('data.txt', roihist)
<mask token>
ax.plot_surface(X1, Y1, roihist, cmap='hot')
cv2.imshow('roihist', roihist)
cv2.normalize(roihist, roihist, 0, 255, cv2.NORM_MINMAX)
cv2.imshow('roihist_norminalize', roihist)
print('-------')
print(roihist.shape)
plt.figure(2)
plt.plot(roihist[0], 'r')
plt.plot(roihist[1], 'b')
<mask token>
print('dst shape', dst.shape)
<mask token>
cv2.imshow('dst', thresh)
<mask token>
cv2.imshow('opening', opening)
<mask token>
ax.plot_surface(X, Y, dst, cmap='hot')
<mask token>
print(dst1.shape)
<mask token>
ax1.plot_surface(X, Y, dst1, cmap='hot')
plt.show()
cv2.waitKey()

<mask token>
X1, Y1 = np.meshgrid(np.linspace(0, 256, 256), np.linspace(0, 180, 180))
X, Y = np.meshgrid(np.linspace(0, 300, 300), np.linspace(0, 300, 300))
roi = cv2.imread('3.png')
hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
target = cv2.imread('33.png')
hsvt = cv2.cvtColor(target, cv2.COLOR_BGR2HSV)
print(hsvt.shape)
roihist = cv2.calcHist([hsv], [0, 1], None, [180, 256], [0, 180, 0, 256])
np.savetxt('data.txt', roihist)
fig3d = plt.figure()
ax = fig3d.gca(projection='3d')
ax.plot_surface(X1, Y1, roihist, cmap='hot')
cv2.imshow('roihist', roihist)
cv2.normalize(roihist, roihist, 0, 255, cv2.NORM_MINMAX)
cv2.imshow('roihist_norminalize', roihist)
print('-------')
print(roihist.shape)
plt.figure(2)
plt.plot(roihist[0], 'r')
plt.plot(roihist[1], 'b')
dst = cv2.calcBackProject([hsvt], [0, 1], roihist, [0, 180, 0, 256], 1)
print('dst shape', dst.shape)
ret, thresh = cv2.threshold(dst, 50, 255, 0)
cv2.imshow('dst', thresh)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
opening = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
cv2.imshow('opening', opening)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, dst, cmap='hot')
ret, dst1 = cv2.threshold(dst, 100, 0, cv2.THRESH_TOZERO_INV)
print(dst1.shape)
fig1 = plt.figure()
ax1 = fig1.gca(projection='3d')
ax1.plot_surface(X, Y, dst1, cmap='hot')
plt.show()
cv2.waitKey()

import cv2
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
X1, Y1 = np.meshgrid(np.linspace(0, 256, 256), np.linspace(0, 180, 180))
X, Y = np.meshgrid(np.linspace(0, 300, 300), np.linspace(0, 300, 300))
roi = cv2.imread('3.png')
hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
target = cv2.imread('33.png')
hsvt = cv2.cvtColor(target, cv2.COLOR_BGR2HSV)
print(hsvt.shape)
roihist = cv2.calcHist([hsv], [0, 1], None, [180, 256], [0, 180, 0, 256])
np.savetxt('data.txt', roihist)
fig3d = plt.figure()
ax = fig3d.gca(projection='3d')
ax.plot_surface(X1, Y1, roihist, cmap='hot')
cv2.imshow('roihist', roihist)
cv2.normalize(roihist, roihist, 0, 255, cv2.NORM_MINMAX)
cv2.imshow('roihist_norminalize', roihist)
print('-------')
print(roihist.shape)
plt.figure(2)
plt.plot(roihist[0], 'r')
plt.plot(roihist[1], 'b')
dst = cv2.calcBackProject([hsvt], [0, 1], roihist, [0, 180, 0, 256], 1)
print('dst shape', dst.shape)
ret, thresh = cv2.threshold(dst, 50, 255, 0)
cv2.imshow('dst', thresh)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
opening = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
cv2.imshow('opening', opening)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, dst, cmap='hot')
ret, dst1 = cv2.threshold(dst, 100, 0, cv2.THRESH_TOZERO_INV)
print(dst1.shape)
fig1 = plt.figure()
ax1 = fig1.gca(projection='3d')
ax1.plot_surface(X, Y, dst1, cmap='hot')
plt.show()
cv2.waitKey()

import cv2
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
X1,Y1 = np.meshgrid(np.linspace(0,256,256),np.linspace(0,180,180))
X,Y = np.meshgrid(np.linspace(0,300,300),np.linspace(0,300,300))
roi = cv2.imread('3.png')
hsv = cv2.cvtColor(roi,cv2.COLOR_BGR2HSV)
##
target = cv2.imread('33.png')
hsvt = cv2.cvtColor(target,cv2.COLOR_BGR2HSV)
print(hsvt.shape)
#计算直方图
roihist = cv2.calcHist([hsv],[0,1],None,[180,256],[0,180,0,256])
np.savetxt("data.txt",roihist)
#归一化，使得值全部在0到255之间
fig3d = plt.figure()
ax = fig3d.gca(projection = '3d')
ax.plot_surface(X1,Y1,roihist,cmap='hot')
cv2.imshow("roihist",roihist)
cv2.normalize(roihist,roihist,0,255,cv2.NORM_MINMAX)
cv2.imshow("roihist_norminalize",roihist)
print("-------")
print(roihist.shape)
plt.figure(2)
plt.plot(roihist[0],'r')
plt.plot(roihist[1],'b')
dst = cv2.calcBackProject([hsvt],[0,1],roihist,[0,180,0,256],1)
print("dst shape",dst.shape)
ret,thresh = cv2.threshold(dst,50,255,0)
cv2.imshow("dst",thresh)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
opening = cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel)
cv2.imshow("opening",opening)
fig = plt.figure()
ax = fig.gca(projection = '3d')
#Z = np.sinc(np.sqrt(X ** 2 + Y ** 2))
#print(Z.shape)
ax.plot_surface(X,Y,dst,cmap='hot')
ret,dst1 = cv2.threshold(dst,100,0,cv2.THRESH_TOZERO_INV)
print(dst1.shape)
fig1 = plt.figure()
ax1 = fig1.gca(projection = '3d')
ax1.plot_surface(X,Y,dst1,cmap='hot')
plt.show()
cv2.waitKey()
