我的形象火焰如下所示:
我正在尝试检测相机视图的外边缘并使图形居中,以便火焰的圆形视图正好位于绘图的中心。由于圆圈的位置可能会随着图像捕捉日期而改变。有时它可能在上半部分,有时是下半部分等。
Python中是否有可以检测视图并使其居中的模块?
可重现的代码
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()
答案 0 :(得分:4)
我认为你有很多选择。我想到的两个简单的方法是将您的输入图像设置为低强度值,这将为您提供白色圆圈。然后你可以对其上的圆圈运行Hough变换来找到中心。
或者您可以使用阈值化白色像素的距离变换并获取此距离变换的最大值:
# code derived from watershed example of scikit-image
# http://scikit-image.org/docs/dev/auto_examples/plot_watershed.html
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage as ndi
from skimage.morphology import watershed
from skimage.feature import peak_local_max
from skimage.color import rgb2gray
from skimage.io import imread
img = imread('flame.png')
image = rgb2gray(img) > 0.01
# Now we want to separate the two objects in image
# Generate the markers as local maxima of the distance to the background
distance = ndi.distance_transform_edt(image)
# get global maximum like described in
# http://stackoverflow.com/a/3584260/2156909
max_loc = unravel_index(distance.argmax(), distance.shape)
fig, axes = plt.subplots(ncols=4, figsize=(10, 2.7))
ax0, ax1, ax2, ax3 = axes
ax0.imshow(img,interpolation='nearest')
ax0.set_title('Image')
ax1.imshow(image, cmap=plt.cm.gray, interpolation='nearest')
ax1.set_title('Thresholded')
ax2.imshow(-distance, cmap=plt.cm.jet, interpolation='nearest')
ax2.set_title('Distances')
ax3.imshow(rgb2gray(img), cmap=plt.cm.gray, interpolation='nearest')
ax3.set_title('Detected centre')
ax3.scatter(max_loc[1], max_loc[0], color='red')
for ax in axes:
ax.axis('off')
fig.subplots_adjust(hspace=0.01, wspace=0.01, top=1, bottom=0, left=0,
right=1)
plt.show()
只是为了让您了解这种方法的稳健性,如果我选择一个非常糟糕的阈值(image = rgb2gray(img) > 0.001
- 太低而无法得到一个好的圆圈),结果几乎相同:
答案 1 :(得分:3)
改编自this answer,使用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()
这会产生: