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

jam*_*mes 5 python opencv image image-processing

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

在此输入图像描述

你看到的是玻璃基板上的光刻胶柱.我想做的是获得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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

mai*_*ual 5

我很少发现Hough对于真实世界的应用很有用,因此我宁愿遵循去噪,分割和椭圆拟合的路径.

对于去噪,选择非局部均值(NLM).对于分割 - 只是看图像 - 我想出了一个有三个类的高斯混合模型:一个用于背景,两个用于对象(漫反射和镜面反射分量).这里,混合模型基本上通过三个高斯函数来模拟灰度图像直方图的形状(如Wikipedia mixed -histogram gif中所示).感兴趣的读者被重定向到维基百科的文章.

椭圆适合最后只是一个基本的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 );
    }
}
Run Code Online (Sandbox Code Playgroud)

在绘制椭圆之前,我应该清理非常小的轮廓(在上排第二个)(大于最小的5个点).

*edit* 添加了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 )
Run Code Online (Sandbox Code Playgroud)

去噪 等高线 省略号 高斯混合物