opencv算法(通过opencv点击图片获得相应坐标)

这是一个开始

get the coordinates of the image by click the mouse.
eg:
o(x,y)

get the 2 dims so you must first do grayscale processing.
实现的功能是通过鼠标的点击,获得图片的相应的坐标
图片的缩放,或改变图片的坐标位置的大小
原理参见:
opencv图片的缩放的数学原理

# 图片坐标获取的方法:
# -*- coding:utf-8 -*-
import cv2
img=cv2.imread("4.jpg") # 定片位置
img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  # 转化为灰度图
# 图片的缩放
#size=img.shape
#img=cv2.resize(img,(size[1]//2,size[0]//2),cv2.INTER_LINEAR)


def onmouse(event,x,y,flags,param):
    if event==cv2.EVENT_LBUTTONDOWN:
        print(x,y)


def main():
    cv2.namedWindow("img",cv2.WINDOW_NORMAL)  # 构建窗口
    cv2.setMouseCallback("img",onmouse,0) # 回调绑定窗口
    cv2.imshow("img",img) # 显示图像
    if cv2.waitKey()==ord("q"):
        cv2.destroyAllWindows()


if __name__=="__main__":
    main()

result

程序运行正常

猜你喜欢

转载自blog.csdn.net/lc574260570/article/details/82588334