如何找到图像中亮点的中心?

时间:2015-05-06 15:52:59

标签: python image matlab search image-processing

以下是我将要处理的各种图像的示例:

Balls http://pages.cs.wisc.edu/~csverma/CS766_09/Stereo/callight.jpg

每个球都有一个亮点。我想找到亮点中心的坐标。我怎么能用Python或Matlab做到这一点?我现在遇到的问题是,当场不止一个点具有相同(或大致相同)的白色,但我需要的是找到这个群集的中心。白点。

另外,对于最左边和最右边的图像,我如何找到整个圆形物体的中心?

2 个答案:

答案 0 :(得分:7)

您可以简单地对图像进行阈值处理,并找到剩余图像的平均坐标。当有多个具有相同强度的值时,这将处理这种情况。当您对图像进行阈值处理时,显然会有多个明亮的白色像素,因此如果您想将它们全部放在一起,请找到质心或平均坐标以确定所有这些白色的中心明亮的像素。在这种特殊情况下,不需要过滤。这是MATLAB中的一些内容。

我直接读取了该图像,转换为灰度并清除了每个图像周围的白色边框。接下来,我将图像分成5个块,对图像进行阈值处理,找到剩余的平均坐标,并在每个中心的位置放置一个点:

im = imread('http://pages.cs.wisc.edu/~csverma/CS766_09/Stereo/callight.jpg');
im = rgb2gray(im);
im = imclearborder(im);

%// Split up images and place into individual cells
split_point = floor(size(im,2) / 5);
images = mat2cell(im, size(im,1), split_point*ones(5,1));

%// Show image to place dots
imshow(im);
hold on;

%// For each image...
for idx = 1 : 5
    %// Get image
    img = images{idx}; 

    %// Threshold
    thresh = img > 200;

    %// Find coordinates of thresholded image
    [y,x] = find(thresh);

    %// Find average
    xmean = mean(x);
    ymean = mean(y);

    %// Place dot at centre
    %// Make sure you offset by the right number of columns
    plot(xmean + (idx-1)*split_point, ymean, 'r.', 'MarkerSize', 18);
end        

我明白了:

enter image description here

如果您需要Python解决方案,我建议您使用scikit-image结合numpymatplotlib进行绘图。以上是Python中转录的上述代码。请注意,我已将链接引用的图像手动保存在磁盘上并将其命名为balls.jpg

import skimage.io
import skimage.segmentation
import numpy as np
import matplotlib.pyplot as plt

# Read in the image
# Note - intensities are floating point from [0,1]
im = skimage.io.imread('balls.jpg', True)

# Threshold the image first then clear the border
im_clear = skimage.segmentation.clear_border(im > (200.0/255.0))

# Determine where to split up the image
split_point = int(im.shape[1]/5)

# Show image in figure and hold to place dots in
plt.figure()
plt.imshow(np.dstack([im,im,im]))

# For each image...
for idx in range(5):

  # Extract sub image
  img = im_clear[:,idx*split_point:(idx+1)*split_point]

  # Find coordinates of thresholded image
  y,x = np.nonzero(img)

  # Find average
  xmean = x.mean()
  ymean = y.mean()

  # Plot on figure
  plt.plot(xmean + idx*split_point, ymean, 'r.', markersize=14)

# Show image and make sure axis is removed
plt.axis('off')
plt.show()

我们得到这个数字:

enter image description here

小旁注

我可以完全跳过上面的代码并使用regionpropsMATLAB linkscikit-image link)。您可以简单地对图像进行阈值处理,然后应用regionprops来查找每个白色像素簇的质心,但我想我会给您一个更加手动的方式,这样您就可以欣赏算法并自己理解它

希望这有帮助!

答案 1 :(得分:1)

使用2D卷积,然后找到强度最高的点。您可以在应用2d卷积之前对强度值应用凹面非线性函数(例如exp),以增强相对于图像的调光器部分的亮点。像conv2(exp(img),ker)

这样的东西