人機互動拾取影象上一個點,並顯示水平方向上30個畫素的光譜曲線圖
阿新 • • 發佈:2021-11-03
從影象上拾取一個點,並水平向右顯示以該點為起點的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()
效果圖