簡體   English   中英

如何使用opencv或javacv識別多邊形?

[英]How to identify polygon using opencv or javacv?

我正在做一個使用圖像處理技術來識別不同對象及其長度的項目。 我在javaCV和OpenCV中經歷了很多例子。 但不幸的是我無法識別多邊形的T形。

我嘗試使用以下矩形識別方法,但我失敗了。

public static CvSeq findSquares( final IplImage src,  CvMemStorage storage)
{

CvSeq squares = new CvContour();
squares = cvCreateSeq(0, sizeof(CvContour.class), sizeof(CvSeq.class), storage);

IplImage pyr = null, timg = null, gray = null, tgray;
timg = cvCloneImage(src);

CvSize sz = cvSize(src.width() & -2, src.height() & -2);
tgray = cvCreateImage(sz, src.depth(), 1);
gray = cvCreateImage(sz, src.depth(), 1);
pyr = cvCreateImage(cvSize(sz.width()/2, sz.height()/2), src.depth(), src.nChannels());

// down-scale and upscale the image to filter out the noise
cvPyrDown(timg, pyr, CV_GAUSSIAN_5x5);
cvPyrUp(pyr, timg, CV_GAUSSIAN_5x5);
cvSaveImage("ha.jpg",   timg);
CvSeq contours = new CvContour();
// request closing of the application when the image window is closed
// show image on window
// find squares in every color plane of the image
for( int c = 0; c < 3; c++ )
{
    IplImage channels[] = {cvCreateImage(sz, 8, 1), cvCreateImage(sz, 8, 1), cvCreateImage(sz, 8, 1)};
    channels[c] = cvCreateImage(sz, 8, 1);
    if(src.nChannels() > 1){
        cvSplit(timg, channels[0], channels[1], channels[2], null);
    }else{
        tgray = cvCloneImage(timg);
    }
    tgray = channels[c];
    // try several threshold levels
    for( int l = 0; l < N; l++ )
    {
    //             hack: use Canny instead of zero threshold level.
    //             Canny helps to catch squares with gradient shading
        if( l == 0 )
        {
    //                apply Canny. Take the upper threshold from slider
    //                and set the lower to 0 (which forces edges merging)
                      cvCanny(tgray, gray, 0, thresh, 5);
    //                 dilate canny output to remove potential
    //                // holes between edge segments
                      cvDilate(gray, gray, null, 1);
                 }
          else
        {
    //                apply threshold if l!=0:
                      cvThreshold(tgray, gray, (l+1)*255/N, 255, CV_THRESH_BINARY);
          }
        //            find contours and store them all as a list
                      cvFindContours(gray, storage, contours, sizeof(CvContour.class), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

                      CvSeq approx;

        //            test each contour
                      while (contours != null && !contours.isNull()) {
                      if (contours.elem_size() > 0) {
                           approx = cvApproxPoly(contours, Loader.sizeof(CvContour.class),storage, CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0);
                    if( approx.total() == 4
                            &&
                            Math.abs(cvContourArea(approx, CV_WHOLE_SEQ, 0)) > 1000 &&
                        cvCheckContourConvexity(approx) != 0
                        ){
                        double maxCosine = 0;
                        //
                        for( int j = 2; j < 5; j++ )
                        {
            // find the maximum cosine of the angle between joint edges
                                                double cosine = Math.abs(angle(new CvPoint(cvGetSeqElem(approx, j%4)), new CvPoint(cvGetSeqElem(approx, j-2)), new CvPoint(cvGetSeqElem(approx, j-1))));
                                                maxCosine = Math.max(maxCosine, cosine);
                         }
                         if( maxCosine < 0.2 ){
                                 CvRect x=cvBoundingRect(approx, l);
                                 if((x.width()*x.height())<5000 ){
                                     System.out.println("Width : "+x.width()+" Height : "+x.height());
                             cvSeqPush(squares, approx);
                                     //System.out.println(x);
                                 }
                         }
                    }
                }
                contours = contours.h_next();
            }
        contours = new CvContour();
    }
}
return squares;
}

請幫助我修改此方法以識別圖像中的T形狀。 輸入圖像是這樣的。

在此輸入圖像描述

這是我要識別的T形

在此輸入圖像描述

我找到了解決問題的方法:

  • 將圖像轉換為灰度:

灰度圖像

  • 做一個閾值(轉換為1位圖像):

2位圖像

  • 查找輪廓並填充它們:

填充圖像

提示:要在OpenCV中填充輪廓,請使用-1作為drawContours函數中的thickness參數。

  • 用相同的內核進行擴張和侵蝕后:

結果圖像

就是這樣! 在此之后,您在圖像上找到T形圖形並不是問題!

不幸的是我不知道JavaCV,但我可以與你分享c ++代碼:

Mat src = imread("in.jpg"), gray;

cvtColor(src, gray, CV_BGR2GRAY);

threshold(gray, gray, 230, 255, THRESH_BINARY_INV);

vector<Vec4i> hierarchy;
vector<vector<Point> > contours;

findContours(gray, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

gray = Scalar::all(255);
for (size_t i=0; i<contours.size(); i++)
{
    drawContours(gray, contours, i, Scalar(0), -1);
}

Mat element = getStructuringElement(MORPH_RECT, Size(2, 2), Point(1, 1));
dilate(gray, gray, element);
erode(gray, gray, element);

imshow("window", gray);

提示:如果您願意,可以將此代碼轉換為JavaCV。 為此,請閱讀本教程

您最好找到輪廓並使用CvApproxPoly() 您可以找到一個很好的示例,說明如何使用此函數在此處查找矩形並調整它以查找T形狀。 此示例使用OpenCV創建並使用c ++編寫。

要遍歷序列中的所有點:

for (int i = 0; i < cornerPoints->total; i++) {
 CvPoint *cornerPoints = (CvPoint*) cvGetSeqElem(cornerPoints, i);
}

聽起來像家庭作業

預處理@Astor帶來的肯定是有幫助的。 但我仍然認為這種形狀識別與圖像處理:形態學密切相關

你可以准備一個T形的模板,然后“回旋?” 具有預處理結果的模板。 我不記得更多細節,只是關於TAG形態學和卷積的調查

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM