python,使用np.where和box[:, 0]

用作自己的学习笔记,侵权删。 

代码: 

# 获取四个顶点坐标

# 找到最左边的 x 坐标
left_point_x = np.min(box[:, 0])
# 找到最右边的 x 坐标
right_point_x = np.max(box[:, 0])
# 找到最上面的 y 坐标
top_point_y = np.min(box[:, 1])
# 找到最下面的 y 坐标
bottom_point_y = np.max(box[:, 1])

# 通过找到最左边 x 坐标对应的 y 坐标,找到左上角的坐标
left_point_y = box[:, 1][np.where(box[:, 0] == left_point_x)][0]
# 通过找到最右边 x 坐标对应的 y 坐标,找到右上角的坐标
right_point_y = box[:, 1][np.where(box[:, 0] == right_point_x)][0]
# 通过找到最上面 y 坐标对应的 x 坐标,找到左上角的坐标
top_point_x = box[:, 0][np.where(box[:, 1] == top_point_y)][0]
# 通过找到最下面 y 坐标对应的 x 坐标,找到左下角的坐标
bottom_point_x = box[:, 0][np.where(box[:, 1] == bottom_point_y)][0]

# 创建包含上下左右四个点坐标的数组
vertices = np.array([[top_point_x, top_point_y], [bottom_point_x, bottom_point_y], [left_point_x, left_point_y], [right_point_x, right_point_y]])

box[:, 0]

这里引用别人文章里的解释:

在python中会出现[-1]、[:-1]、[::-1]、[n::-1]、[:,:,0]、[…,0]、[…,::-1],他们分别是什么意思呢,这里就来详尽的说一下:

[-1]: 列表最后一项

[:-1]: 从第一项到最后一项

[::-1]: 代表从全列表倒序取

[n::-1]: 同上所述,从索引n开始倒序取值:

[:,:,0]: 因为有了逗号,所以这个方法就和前面的有所不同了。它是对多维数据的一种处理方式,代表了前两维全选,取其中的所有0号索引。

[…,0]: 代表了取最里边一层的所有第0号元素,…代表了对:,:,:,的省略。注意只能进行一次省略,不能是[… , …],全省略那不就是 a 吗。

[…,::-1]: 是对最内层的列表进行逆序取值

原文章:Python中[-1]、[:-1]、[::-1]、[n::-1]、[:,:,0]、[…,0]、[…,::-1] 的理解_python [-1]-CSDN博客

所以对于[:,0]来说,这个就相当于对这个向量组里的所有向量的第一个数进行访问。

np.where(condition)

只有条件 (condition),没有x和y,则输出满足条件元素的坐标.

对于我们这段代码来说,np.where他检索满足条件的数组中的x所在的行 :

box[:,1]包含了所有的y坐标,构成一个n*1的数组,后面的[m][0]是对这个数组的一个索引,访问这个数组的第m行第一个元素。

有些是我自己的理解,有问题欢迎大家指正。

def SegMentLWelder(mask_k, mask_box, img_show, contours_info, box, k_class): imgL = cv2.copyTo(mask_k, mask_box) contours_mask_k, _ = cv2.findContours( imgL, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) (x, y), radius = cv2.minEnclosingCircle(contours_mask_k[0]) center = [int(x), int(y)] area, trg1 = cv2.minEnclosingTriangle(contours_mask_k[0]) triange_P1 = trg1[0][0] triange_P2 = trg1[1][0] triange_P3 = trg1[2][0] areaL = len(np.where(imgL == 255)[0]) triangle_cnt1 = np.array([triange_P1, center, triange_P2]) mask_triange = np.zeros_like(imgL) mask_triange = cv2.drawContours( mask_triange, [triangle_cnt1.astype(int)], 0, 255, -1) img_triange1 = cv2.copyTo(imgL, mask_triange) triangle_cnt2 = np.array([triange_P2, center, triange_P3]) mask_triange = np.zeros_like(imgL) mask_triange = cv2.drawContours( mask_triange, [triangle_cnt2.astype(int)], 0, 255, -1) img_triange2 = cv2.copyTo(imgL, mask_triange) area1 = len(np.where(img_triange1 == 255)[0]) if area1 < 0.25*areaL or area1 > 0.75*areaL: triangle_cnt1 = np.array([triange_P1, center, triange_P3]) mask_triange = np.zeros_like(imgL) mask_triange = cv2.drawContours( mask_triange, [triangle_cnt1.astype(int)], 0, 255, -1) img_triange1 = cv2.copyTo(imgL, mask_triange) else: area2 = len(np.where(img_triange2 == 255)[0]) if area2 < 0.25*areaL or area2 > 0.75*areaL: triangle_cnt2 = np.array([triange_P1, center, triange_P3]) mask_triange = np.zeros_like(imgL) mask_triange = cv2.drawContours( mask_triange, [triangle_cnt2.astype(int)], 0, 255, -1) img_triange2 = cv2.copyTo(imgL, mask_triange) FindContourSinge(k_class, img_triange1, contours_info, img_show, times=1) FindContourSinge(k_class, img_triange2, contours_info, img_show, times=1)
06-12
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值