从图像上拾取一个点,并水平向右显示以该点为起点的30个像素的光谱曲线
import cv2 import matplotlib.pyplot as plt from matplotlib.pyplot import MultipleLocator #人机交互拾取图像上一个点,并显示水平方向上30个像素的光谱曲线图 def mouse(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: xy = "%d,%d" % (x, y) cv2.circle(img, (x, y), 3, (250, 55, 250), thickness=-1) cv2.putText(img, xy, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.0, (50, 200, 255), thickness=1) cv2.imshow("image", img) column,line=xy.split(',') l,c=int(line), int(column) print(line, column) X=[] Y1=[] Y2=[] Y3=[] n=40 for i in range(n): X.append(c+i) Y1.append(a[l,c+ i, 0]) Y2.append(a[l,c + i, 1]) Y3.append(a[l,c+ i, 2]) cv2.line(img, ( c,l), (X[-1],l ), color=(0, 0, 255), thickness=2) #显示中文 plt.rcParams['font.sans-serif'] = ['SimHei'] plt.figure(figsize=(8, 4)) x_major_locator = MultipleLocator(2) ax = plt.gca() # ax为两条坐标轴的实例 ax.xaxis.set_major_locator(x_major_locator) plt.plot(X, Y1, label="R", color="red", linewidth=2) plt.plot(X, Y2, label="G", color="green", linewidth=2) plt.plot(X, Y3, label="B", color="blue", linewidth=2) plt.xlabel("Time(s)") plt.ylabel("Value值") plt.title("RGB光谱曲线") plt.show() cv2.imshow("image", img) if __name__ == '__main__': img = cv2.imread('mountain.jpg') print(img.shape) cv2.namedWindow("image") cv2.imshow("image", img) a=img.copy() cv2.setMouseCallback("image", mouse) cv2.waitKey(0) cv2.destroyAllWindows()
效果图