如何使用OpenCV在SEM图像上检测和测量(fitEllipse)对象?

时间:2019-02-09 08:39:13

标签: python image opencv image-processing

我大约有30幅SEM(扫描电子显微镜)图像:

enter image description here

您看到的是玻璃基板上的光刻胶柱。 我想做的是获取x和y方向的平均直径以及x和y方向的平均周期。

现在,我想知道是否有一种方法可以使用python和opencv使它自动自动化,而不是手动进行所有测量?

编辑: 我尝试了以下代码,看来正在努力检测圆但是我实际上需要的是椭圆形,因为我需要在x和y方向上设置直径。

...而且我还不太清楚如何获得磅秤?

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread("01.jpg",0)
output = img.copy()

edged = cv2.Canny(img, 10, 300)
edged = cv2.dilate(edged, None, iterations=1)
edged = cv2.erode(edged, None, iterations=1)



# detect circles in the image
circles = cv2.HoughCircles(edged, cv2.HOUGH_GRADIENT, 1.2, 100)


# ensure at least some circles were found
if circles is not None:
    # convert the (x, y) coordinates and radius of the circles to integers
    circles = np.round(circles).astype("int")

    # loop over the (x, y) coordinates and radius of the circles
    for (x, y, r) in circles[0]:
        print(x,y,r)
        # draw the circle in the output image, then draw a rectangle
        # corresponding to the center of the circle
        cv2.circle(output, (x, y), r, (0, 255, 0), 4)
        cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)

    # show the output image
    plt.imshow(output, cmap = 'gray', interpolation = 'bicubic')
    plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
    plt.figure()
    plt.show()

enter image description here

灵感来源:https://www.pyimagesearch.com/2014/07/21/detecting-circles-images-using-opencv-hough-circles/

3 个答案:

答案 0 :(得分:5)

我很少发现Hough在现实应用中有用,因此我宁愿遵循降噪,分割和椭圆拟合的方法。

对于降噪,选择非局部均值(NLM)。对于分割---仅查看图像---我提出了一个具有三类的高斯混合模型:一类用于背景,二类用于对象(漫反射和镜面反射分量)。在这里,混合模型本质上是通过三个高斯函数对灰度图像直方图的形状进行建模(如Wikipedia mixture-histogram gif中所示)。感兴趣的读者将重定向到Wikipedia article

最后的椭圆拟合只是基本的OpenCV工具。

在C ++中,但与OpenCV-Python类似

#include "opencv2/ml.hpp"
#include "opencv2/photo.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
void gaussianMixture(const cv::Mat &src, cv::Mat &dst, int nClasses )
{
    if ( src.type()!=CV_8UC1 )
        CV_Error(CV_StsError,"src is not 8-bit grayscale");

    // reshape
    cv::Mat samples( src.rows * src.cols, 1, CV_32FC1 );
    src.convertTo( cv::Mat( src.size(), CV_32FC1, samples.data ), CV_32F );

    cv::Mat labels;
    cv::Ptr<cv::ml::EM> em = cv::ml::EM::create();
    em->setClustersNumber( nClasses );
    em->setTermCriteria( cv::TermCriteria(CV_TERMCRIT_ITER, 4, 0.0 ) );
    em->trainEM( samples );

    if ( dst.type()!=CV_8UC1 || dst.size()!=src.size() )
        dst = cv::Mat( src.size(),CV_8UC1 );
    for(int y=0;y<src.rows;++y)
    {
        for(int x=0;x<src.cols;++x)
        {
            dst.at<unsigned char>(y,x) = em->predict( src.at<unsigned char>(y,x) );
        }
    }
}
void automate()
{
    cv::Mat input = cv::imread( /* input image in color */,cv::IMREAD_COLOR);
    cv::Mat inputDenoised;
    cv::fastNlMeansDenoising( input, inputDenoised, 8.0, 5, 17 );
    cv::Mat gray;
    cv::cvtColor(inputDenoised,gray,cv::COLOR_BGR2GRAY );
    gaussianMixture(gray,gray,3 );

    typedef std::vector< std::vector< cv::Point > >  VecOfVec;
    VecOfVec contours;
    cv::Mat objectPixels = gray>0;
    cv::findContours( objectPixels, contours, cv::RETR_LIST, cv::CHAIN_APPROX_NONE );
    cv::Mat inputcopy; // for drawing of ellipses
    input.copyTo( inputcopy );
    for ( size_t i=0;i<contours.size();++i )
    {
        if ( contours[i].size() < 5 )
            continue;
        cv::drawContours( input, VecOfVec{contours[i]}, -1, cv::Scalar(0,0,255), 2 );
        cv::RotatedRect rect = cv::fitEllipse( contours[i] );
        cv::ellipse( inputcopy, rect, cv::Scalar(0,0,255), 2 );
    }
}

在绘制椭圆形之前,我应该已经清理了非常小的轮廓(第二行,第二行)(大于最小5个点)。

*编辑* 添加了不带降噪和查找轮廓部分的Python预测变量。学习模型后,预测时间约为1.1秒

img = cv.imread('D:/tmp/8b3Lm.jpg', cv.IMREAD_GRAYSCALE )

class Predictor :
    def train( self, img ):
        self.em = cv.ml.EM_create()
        self.em.setClustersNumber( 3 )
        self.em.setTermCriteria( ( cv.TERM_CRITERIA_COUNT,4,0 ) )
        samples = np.reshape( img, (img.shape[0]*img.shape[1], -1) ).astype('float')
        self.em.trainEM( samples )

    def predict( self, img ):
        samples = np.reshape( img, (img.shape[0]*img.shape[1], -1) ).astype('float')
        labels = np.zeros( samples.shape, 'uint8' )
        for i in range ( samples.shape[0] ):
            retval, probs = self.em.predict2( samples[i] )
            labels[i] = retval[1] * (255/3) # make it [0,255] for imshow
        return np.reshape( labels, img.shape )

predictor = Predictor()

predictor.train( img )
t = time.perf_counter()
predictor.train( img )
t = time.perf_counter() - t
print ( "train %s s" %t )

t = time.perf_counter()
labels = predictor.predict( img )
t = time.perf_counter() - t
print ( "predict %s s" %t )

cv.imshow( "prediction", labels  )
cv.waitKey( 0 )

Denoised Contours Ellipses Mixture of Gaussians

答案 1 :(得分:1)

我会使用来自openCV的HoughCircles方法。它会带给您图像中的所有圆圈。这样就很容易计算出每个圆的半径和位置。

查看:https://docs.opencv.org/3.4/d4/d70/tutorial_hough_circle.html

答案 2 :(得分:1)

我首先使用25px在OpenCV(Python)中分割图像,其成本约为.squareLoader。如果仅在脱粒图像的轮廓上使用<div>,则其成本为var kiara = 1; var rodeo = 3; var calypso = 3; var balthazar = 3; var mistral = 4; var saya = 4; var luna = 4; var points = [{rodeo}, {calypso}, {balthazar}, {mistral}, {saya}, {luna},{kiara}]; var maxNumberArray = []; var maxNamesArray = []; // ADDED THIS LINE var max; for(var char in points){ for(var name in points[char]){ if(!max){ max = points[char][name]; } else { if(max < points[char][name]){ max = points[char][name]; } } } } for(var char in points){ for(var name in points[char]){ if(points[char][name] === max){ maxNumberArray.push(points[char]); maxNamesArray.push(name); // ADDED THIS LINE } } } console.log(JSON.stringify(maxNumberArray)); console.log(maxNamesArray); // ADDED this LINE ,结果可能不那么准确。只是一个权衡。

enter image description here


详细信息:

  
      
  1. 转换为灰度并将其脱粒

  2.   
  3. 去噪的形态

  4.   
  5. 找到外部轮廓

  6.   
  7. 适合的椭圆形

  8.   

代码:

cv2.ml.EM