圖像輪廓尋找

1:依次掃描圖像每一行;

2:獲取每一行的團;

3.若是相鄰的團就進行mark點的合併;

struct _Line
{
    int startx;
    int endx;
    int mark;
    vector<int>x;
    vector<int>y;
}Line;


//進行圖像mark值的重新賦值
void MarkImage(vector<struct _Line>&sourceLine,int lastStartRuns,int lastEndRuns,int newStartRuns,int newEndRuns,vector<int>&myMark)
{
    int tempMark = 0;
    for (int i=newStartRuns; i<=newEndRuns; i++)
    {
        for (int j=lastStartRuns; j<=lastEndRuns; j++)
        {
            //進行比較xy值
            if (sourceLine.at(j).startx > sourceLine.at(i).endx ||
                sourceLine.at(j).endx < sourceLine.at(i).startx)
            {
                continue;
            }
            else
            {
                sourceLine.at(i).mark = sourceLine.at(j).mark;
                if (myMark.empty())
                {
                    myMark.push_back(sourceLine.at(j).mark);
                }
                else
                {
                    tempMark = sourceLine.at(j).mark;
                    bool bIsEqual = false;
                    for (int k=0; k<myMark.size(); k++)
                    {
                        if (tempMark == myMark.at(k))
                        {
                            bIsEqual = true;
                            break;
                        }
                    }
                    if (bIsEqual == false)
                    {
                        myMark.push_back(sourceLine.at(j).mark);
                    }
                }
            }
        }
    }
}


void FindRuns(Mat image,vector<struct _Line>&vectorLine,vector<int>&myMark)
{
    
    int NumberOfRuns = 0;
    unsigned char *p = NULL;
    bool bStart = false;
    int iStartRuns = 0;
    int iEndRuns = 0;
    bool isThereRuns = false;
    int lastStartRuns = -1;
    int lastEndRuns = -1;
    int newStartRuns = 0;
    int newEndRuns = 0;
    for (int j=0; j<image.rows; j++)
    {
        p = image.data + j*image.step;
        isThereRuns = false;
        for (int i=1; i<image.cols-1; i++)
        {
            if (p[i] == 0 && p[i-1] == 255)
            {
                Line.startx = i;
                Line.mark = NumberOfRuns++;
                bStart = true;
                if (isThereRuns == false)//說明這一行有團
                {
                    iStartRuns = Line.mark;//把這行的團第一個給起始值
                    isThereRuns = true;
                }
            }
            if (bStart)
            {
                Line.x.push_back(i);
                Line.y.push_back(j);
            }
            if (p[i] == 0 && p[i+1] == 255)
            {
                Line.endx = i;
                vectorLine.push_back(Line);
                Line.x.clear();
                Line.y.clear();
                bStart = false;
            }
        }
        if (isThereRuns)
        {
            iEndRuns = NumberOfRuns -1;
            if (lastStartRuns == -1 && lastEndRuns == -1)//說明是第一次出現團
            {
                lastStartRuns = iStartRuns;
                lastEndRuns = iEndRuns;
            }
            else
            {
                newStartRuns = iStartRuns;
                newEndRuns = iEndRuns;
                
                //進行mark
                MarkImage(vectorLine,lastStartRuns,lastEndRuns,newStartRuns,newEndRuns,myMark);
                lastStartRuns = newStartRuns;
                lastEndRuns = newEndRuns;
            }
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章