计算图像中对象的中心 [英] Calculating center of an object in an image

查看:115
本文介绍了计算图像中对象的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读此帖子使用OpenCV计算图像的中心,而OpenCV使用Moments.但是我正在尝试计算使用HoughLinesP检测到的对象的中心. OpenCV有办法做到这一点吗?

I was reading this post to calculate the center of an image using OpenCV which uses Moments. But I am trying to calculate the center of an object I detected using HoughLinesP. Is there a way with OpenCV I could do this?

这是我要为其计算中心的图像.

Here is the image for which I am trying to calculate the centers.

找到线段,输出图像如下:

The line segments were found and the output image looks like:

import cv2
import numpy as np
import math

img = cv2.imread("./images/octa.jpg")

b,g,r = cv2.split(img)

smoothed = cv2.GaussianBlur(g, (3,3), 0)

edges = cv2.Canny(smoothed, 15, 60, apertureSize = 3)

lines = cv2.HoughLinesP(edges,1,np.pi/180,35, 30, 20)


print("length of lines detected ", lines.shape)


for line in lines:
        for x1,y1,x2,y2 in line:
          cv2.line(img,(x1,y1),(x2,y2),(255,0,0),2)
          print("x1,y1", x1,",",y1, " --- ", "x2,y2", x2,",",y2)



cv2.imshow('detected',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

使用坐标如何计算该图像的中心?如何在这里使用Moments?

Using the coordinates how could I calculate the center of this image? How could I use Moments here?

我的一个限制是我不能使用OpenCV附带的Contour方法.

One constraint I have is that I cannot use Contour methods included with OpenCV.

推荐答案

以下代码与3.3.1cv2版本一起使用.

The following code was used with cv2 version of 3.3.1.

我密切关注了 opencv文档,它确实有效美好的.

I closely followed the opencv docs and it worked fine.

import cv2

img = cv2.imread("octa.jpg", 0)
ret,thresh = cv2.threshold(img,100,255,0)
im2, contours, hierachy = cv2.findContours(thresh, 1, 2)
cnt = contours[0]

M = cv2.moments(cnt)

cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])

im2 = cv2.cvtColor(im2, cv2.COLOR_GRAY2RGB)

cv2.polylines(im2, cnt, True, (0, 0, 255), 2)

cv2.circle(im2, (cx, cy), 5, (0, 0, 255), 1)

cv2.imshow("res", im2)

两个注意事项:

  • 您需要将0自变量添加到imread中,否则轮廓查找将不起作用
  • 我将阈值设置得低一点,所以只找到了八边形的轮廓
  • you need to add the argument 0 to imread otherwise the contour finding would not work
  • I set the threshold just a little bit lower, so only the contours of the octagon were found

结果:

如果使用其他版本的cv2,则只需将文档更改为您的版本即可;文档真的很好.

If you use a different version of cv2, you can just change the docs to your version; the documentation is really good.

您可能还想稍微模糊图像或进行其他一些预处理,但是在这种情况下,不需要它.

You also may want to blur your image a bit or do some other preprocessing, but in this case, there was no need for it.

编辑,不带轮廓:

我从这篇文章中获得了有用的评论,并做了一些修改.这不使用轮廓.它找到线并用它们找到中心

I took the helpful comments from this post and tinkered around a bit. This does not use contours. It finds lines and uses them to find the center

import cv2
import numpy as np

mg = cv2.imread('octa.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

kernel_size = 5
blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)
ret,thresh = cv2.threshold(blur_gray,100,255,0)

low_threshold = 50
high_threshold = 150
edges = cv2.Canny(thresh, low_threshold, high_threshold)

rho = 1  # distance resolution in pixels of the Hough grid
theta = np.pi / 180  # angular resolution in radians of the Hough grid
threshold = 15  # minimum number of votes (intersections in Hough grid cell)
min_line_length = 50  # minimum number of pixels making up a line
max_line_gap = 50  # maximum gap in pixels between connectable line segments
line_image = np.copy(img) * 0  # creating a blank to draw lines on

# Run Hough on edge detected image
# Output "lines" is an array containing endpoints of detected line segments
lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
                    min_line_length, max_line_gap)

for line in lines:
    for x1,y1,x2,y2 in line:
        cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),2)

lines_edges = cv2.addWeighted(img, 0.5, line_image, 1, 0)

line_image_gray = cv2.cvtColor(line_image, cv2.COLOR_RGB2GRAY)

M = cv2.moments(line_image_gray)

cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])

cv2.circle(lines_edges, (cx, cy), 5, (0, 0, 255), 1)

cv2.imshow("res", lines_edges)

结果: 找到的线用蓝色绘制;中心为红色

Result: Found lines are drawn in blue; the center in red

这篇关于计算图像中对象的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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