如何找到此矩形的中心点 [英] How to find the center point of this rectangle

查看:799
本文介绍了如何找到此矩形的中心点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到位于鱼后的绿色矩形的中心点,但是我的方法是不管用.这是我的代码:

I am trying to find the center point of the green rectangle which is behind the fish, but my approach is not working. Here is my code:

#Finding contours (almost always finds those 2 retangles + some noise):
_, conts, hierarchy = cv2.findContours(img_green, cv2.RETR_TREE , cv2.CHAIN_APPROX_SIMPLE)
for cnt in conts:
    area = cv2.contourArea(cnt)

    #filter noise
    if area > 25:

        M = cv2.moments(cnt)
        x1, y1, w, h = cv2.boundingRect(cnt)
        x2 = x1 + w                           # (x1, y1) = top-left vertex
        y2 = y1 + h                           # (x2, y2) = bottom-right vertex
        cy = int(M['m01']/M['m00'])           # (cx, cy) = rect center
        cx = int(M['m10']/M['m00'])
        rect = cv2.rectangle(green_bar_win, (x1, y1), (x2, y2), (255,0,0), 2)
        center = cv2.circle(green_bar_win, (cx, cy), 2, (0,0,255), 4)

如您所见,它找到矩形的轮廓,但在鱼所在的位置进行划分,从而形成2种不同的形状.它还可以找到这两个形状的中心(蓝点),但是我不知道如何找到大形状的中间.我考虑过对找到的所有矩形中心进行平均,但是我不知道如何写出来.我正在通过hsv颜色找到矩形.帮助吗?

As you can see, it finds the rectangle's contour but divided where the fish is, making 2 different shapes. It also finds the center of this 2 shapes (the blue points), however I don't know how to find the middle of the big one. I thought about averaging all the found rectangle centers but I don't know how to write this out. I am finding the rectangles by hsv color. Help?

我在顶部矩形中有'y1',但是在for循环中,我不知道如何从底部矩形中获得y2.我试过了:

I have 'y1' from the top rectangle, but don't know how to get y2 from the bottom one while inside the for loop. I tried this:

_, conts, hierarchy = cv2.findContours(img_green, cv2.RETR_TREE , cv2.CHAIN_APPROX_SIMPLE)
for cnt in conts:
    area = cv2.contourArea(cnt)

    #filter noise
    if area > 25:

        x1, y1, w, h = cv2.boundingRect(cnt)
        x2 = x1 + w                           # (x1, y1) = top-left vertex

        try:
            rect_center = np.average([y1, y2])
        except:
            print("Failed to average")

        y2 = y1 + h                           # (x2, y2) = bottom-right vertex
        rect = cv2.rectangle(green_bar_win, (x1, y1), (x2, y2), (255,0,0), 2)

但是它仍然失败,因为在分配之前使用了y2.那么,在y1被"for"循环覆盖之前,如何从第二个循环迭代中获得y2?

But it still fails, because y2 is used before assigment. So, how could I get y2 from the second loop itineration before y1 is overwritten by the 'for' loop?

推荐答案

是时候将其分解为一个完整的答案了.

It's time to break this to a full answer.

识别每个矩形后,将其角与每个现有形状进行比较.如果它具有相同的颜色并且有一对共同的角(共享边),则更新旧的矩形:用新矩形的 other 角替换这两个角.

After you identify each rectangle, compare its corners to each existing shape. If it has the same color and a pair of corners in common (shared edge), then update the old rectangle: replace those two corners with the other corners of the new rectangle.

在代码中添加一些内容:

Add a couple of things to your code:

  1. 保留到目前为止找到的形状的列表
  2. 在找到每种形状时,将其与已经找到的形状进行比较.

  1. keep a list of the shapes found so far
  2. as you find each shape, compare to the ones found already.

rect_list = []

rect_list = []

对于续续中的cnt: 面积= cv2.contourArea(cnt)

for cnt in conts: area = cv2.contourArea(cnt)

#filter noise
if area > 25:

    M = cv2.moments(cnt)
    # ... several lines deleted
    rect = cv2.rectangle(green_bar_win, (x1, y1), (x2, y2), (255,0,0), 2)
    center = cv2.circle(green_bar_win, (cx, cy), 2, (0,0,255), 4)

    # Look for adjacent shapes here
    for old_rect in rect_list:
        # fetch the corners of old_rect
        # if old_rect has a pair of corners in common with
        #     the current rect, then merge the two.
        #     Do this by expanding the corners of old_rect.
        else:
            rect_list.append(rect)

这篇关于如何找到此矩形的中心点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆