如何检测图像中的圆形区域并使用Python将其居中? [英] How to detect circlular region in images and centre it with Python?

查看:445
本文介绍了如何检测图像中的圆形区域并使用Python将其居中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的图形火焰:

I have a figure flame of the form shown below:

我正在尝试检测相机视图的外边缘并使图形居中,以使火焰的圆形视图恰好位于图的中心.由于圆圈的位置可能会随图像拍摄日期而变化.有时可能在上半部,有时在下半部,等等.

I am trying to detect the outer edge of the camera's view and centre the figure so that circular view of the flame is exactly at the centre of the plot. As the position of the circle might change with the image capture date. Sometimes it might be at the upper half, sometimes lower half, etc.

Python中是否有任何模块可以检测视图并将其居中?

Are there any modules in Python that can detect the view and centre it?

可复制的代码

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img=mpimg.imread('flame.png')
lum_img = img[:,:,0]
img_plot = plt.imshow(lum_img)
img_plot.set_cmap('jet')
plt.axis('Off')
plt.show()

推荐答案

改编自此答案,进行边缘检测,并使用RANSAC将圆形牢固地拟合到轮廓上:

Adapted from this answer, do an edge detection and robustly fit a circle to the outline using RANSAC:

from __future__ import print_function
from skimage import io, feature, color, measure, draw, img_as_float
import numpy as np

image = img_as_float(color.rgb2gray(io.imread('flame.png')))
edges = feature.canny(image)
coords = np.column_stack(np.nonzero(edges))

model, inliers = measure.ransac(coords, measure.CircleModel,
                                min_samples=3, residual_threshold=1,
                                max_trials=1000)

print(model.params)

rr, cc = draw.circle_perimeter(int(model.params[0]),
                               int(model.params[1]),
                               int(model.params[2]),
                               shape=image.shape)

image[rr, cc] = 1

import matplotlib.pyplot as plt
plt.imshow(image, cmap='gray')
plt.scatter(model.params[1], model.params[0], s=50, c='red')
plt.axis('off')
plt.savefig('/tmp/flame_center.png', bbox_inches='tight')
plt.show()

这将产生:

这篇关于如何检测图像中的圆形区域并使用Python将其居中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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