1.利用cv和np,提取指定区域内的所有像素坐标
import cv2
import numpy as np
img_path = r'************'
img = cv2.imread(img_path,1)
#1代表读取彩色图,0代表读取灰度图
#1.先在空白图上画一个圆
emptyImage = np.zeros(img.shape,np.uint8)
cv2.circle(emptyImage,(7000,5500),1850,(0,0,255),20)
#当最后一值为-1时,代表填充整个线框。否则为线的粗细
binaryImg = cv2.Canny(emptyImage,50,200)#canny二值化处理
#2.再用contours家族根据边界绘制实心圆,根据RGBD值提取坐标
h = cv2.findContours(binaryImg,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
contours = h[0]
#binary,contours, hierarchy = cv2.findContours(binaryImg,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
temp = np.zeros(binaryImg.shape,np.uint8)
cv2.drawContours(temp,contours,-1,255,-1)
a = np.where(temp==255)[0].reshape(-1,1)#提取temp中像素值为255坐标的第一点,并转成列向量
b = np.where(temp==255)[1].reshape(-1,1)
coordinate = np.concatenate([a,b], axis=1).tolist()#axis=1代表对应行的数组进行拼接
inside = [tuple(x) for x in coordinate]
cv2.namedWindow('image',cv2.WINDOW_NORMAL)
cv2.imshow('image',emptyImage)
cv2.namedWindow('contours',cv2.WINDOW_NORMAL)
cv2.imshow('contours',temp)
cv2.waitKey(0)
cv2.destroyAllWindows()
2. 鼠标选点显示像素坐标值(待更新,鼠标滑轮缩放功能)
#为可视化做准备:画圆,取点显示像素坐标值
import cv2
import numpy as np
img = cv2.imread(r'*************************',1)
def on_EVENT_LBUTTONDOWN(event, x, y, flags, param):
#global zoom, zoom_w, zoom_h, img_zoom, flag_har, flag_var
if event == cv2.EVENT_LBUTTONDOWN:
xy = "%d,%d" % (x, y)
print
xy
cv2.circle(img, (x, y), 10, (255, 0, 0), thickness=10)
cv2.putText(img, xy, (x, y), cv2.FONT_HERSHEY_PLAIN,
10, (255, 255, 255), thickness=15)
cv2.imshow("image", img)
cv2.namedWindow("image", cv2.WINDOW_KEEPRATIO)
cv2.setMouseCallback("image", on_EVENT_LBUTTONDOWN)
cv2.circle(img,(7000,5500),50,(0,0,255),-1)#画圆,50米为1850
cv2.imshow("image", img)
while (True):
try:
cv2.waitKey(100)
except Exception:
cv2.destroyWindow("image")
break
cv2.waitKey(0)
cv2.destroyAllWindow()
效果展示
![](https://img-blog.csdnimg.cn/7f84ba111c664065a2d2fe5765153e35.png)