opencv-python 怎么获取4角点xy坐标值

小白求大佬指教!!!

opencv-python 怎么获取4角点坐标数值   

运行如下代码获取后面图

import cv2

import numpy as np

img = cv2.imread('C:/Users/Administrator/Desktop/01/04.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
dst = cv2.cornerHarris(gray, 2, 3, 0.04)
img[dst > 0.01 * dst.max()] = [0, 0, 255]
cv2.imshow('res', img)
cv2.waitKey(0)

我要获取 左上xy  右上xy  左下xy  右下xy  的角点坐标值,

最好能得到的数据后面加一位小数点.0 如:

  左上x=**.0   y=**.0

  右上x=**.0   y=**.0

  左下x=**.0   y=**.0

  右下x=**.0   y=**.0

import cv2 
import numpy as np
import matplotlib.pyplot as plt
img =cv2.imread('E:/python/ha.png') 
imgray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#harris角点检测图像需为float32
gray=np.float32(imgray)
dst=cv2.cornerHarris(gray,8,3,0.04)
dst=cv2.dilate(dst,None)
ret,dst=cv2.threshold(dst,0.01*dst.max(),255,0)
dst=np.uint8(dst)
#图像连通域
ret,labels,stats,centroids=cv2.connectedComponentsWithStats(dst)
#迭代停止规则
criteria=(cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER,100,0.001)
corners=cv2.cornerSubPix(gray,np.float32(centroids),(5,5),(-1,-1),criteria)
res=np.hstack((centroids,corners))
res=np.int0(res)
 
#img[res[:,1],res[:,0]]=[0,120,255]
#img[res[:,3],res[:,2]]=[45,255,100]
 
 
for i in res:
    x1,y1,x2,y2=i.ravel()
    cv2.circle(img,(x1,y1),3,255,-1)
    cv2.circle(img,(x2,y2),3,(0,255,0),-1)
img=img[:,:,::-1]
plt.imshow(img)

参考https://blog.csdn.net/zhu_hongji/article/details/81235643

参考:https://blog.csdn.net/weixin_39571179/article/details/111974131

你要的是第一图的外界矩形角点还是第二图的角点?如果是第一张要做成第二张的话,可以用霍夫圆来做(当然你用findcountours也是可以的)

import cv2
import numpy as np

img=cv2.imread("./666.png")
cv2.imshow("1.",img)
canny=cv2.Canny(img,50,150)
cv2.imshow("2",canny)
circles=cv2.HoughCircles(canny,cv2.HOUGH_GRADIENT,1,minDist=50,param1=150,param2=20,minRadius=10,maxRadius=100)
for i in circles[0,:]:
    x=i[0]
    y=i[1]
    r=i[2]
    left_x=x-r
    top_y=y-r
    right_x=r+x
    #bottom_y=r+y
    cv2.circle(img,(int(x),int(y)),int(r),(0,0,255),thickness=1,lineType=cv2.LINE_AA)
    cv2.imshow("ciecle",img)
    cv2.rectangle(img,(int(left_x),int(top_y)),(int(right_x),int(bottom_y)),color=[0,0,0],thickness=1,lineType=cv2.LINE_AA)
    cv2.imshow("rect",img)
    cv2.rectangle(img,(int(left_x),int(top_y)),(int(right_x),int(bottom_y)),color=[0,0,0],thickness=-1,lineType=cv2.LINE_AA)#填充矩形框
    cv2.imshow("rect2", img)

    cv2.waitKey()

 

您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!

速戳参与调研>>>https://t.csdnimg.cn/Kf0y