OpenCV如何提取圖片中曲線
簡(jiǎn)單介紹
在實(shí)際的應(yīng)用中,我們常常需要對(duì)圖像中的曲線進(jìn)行描述、處理,這個(gè)曲線可以是輪廓,骨架或者其他??梢杂?span style="color: #800000">deque<Point> 描述曲線,接下來(lái)簡(jiǎn)單介紹下如何從圖片中搜索這些曲線并保存。
首先,輸入的圖片是一張二值圖片 (白色為曲線),其中包含的曲線寬度為 1 像素的 (如果曲線不是 1 像素的 先提取其骨架)。遍歷尋找圖像中第一個(gè)白色的點(diǎn),然后從這個(gè)點(diǎn)開(kāi)始延伸尋找曲線。注意,第一個(gè)找到的點(diǎn)不一定是曲線的端點(diǎn),因此應(yīng)該分別向兩邊尋找相鄰的點(diǎn),因此deque 會(huì)好一些。每找到一個(gè)點(diǎn),將其保存deque 而后置黑(防止重復(fù)尋找)。搜索到一個(gè)沒(méi)有相鄰點(diǎn)的點(diǎn),表示一端搜索完成。
值得注意的一點(diǎn)是,我在寫(xiě)搜尋相鄰點(diǎn)的時(shí)候,會(huì)首先搜尋此點(diǎn)與上一個(gè)點(diǎn)相鄰位置相對(duì)的位置,如果沒(méi)有,則分別搜索向兩邊搜索。這樣的好處是可以減少尋找的次數(shù),而且當(dāng)有相交的曲線時(shí),能連接到我們一般認(rèn)為的曲線。
代碼
//尋找圖像曲線上某個(gè)點(diǎn)的下一個(gè)點(diǎn)
bool findNextPoint(vector<Point> &_neighbor_points, Mat &_image, Point _inpoint, int flag, Point& _outpoint, int &_outflag)
{
int i = flag;
int count = 1;
bool success = false;
while (count <= 7)
{
Point tmppoint = _inpoint + _neighbor_points[i];
if (tmppoint.x > 0 && tmppoint.y > 0 && tmppoint.x < _image.cols&&tmppoint.y < _image.rows)
{
if (_image.at<uchar>(tmppoint) == 255)
{
_outpoint = tmppoint;
_outflag = i;
success = true;
_image.at<uchar>(tmppoint) = 0;
break;
}
}
if (count % 2)
{
i += count;
if (i > 7)
{
i -= 8;
}
}
else
{
i += -count;
if (i < 0)
{
i += 8;
}
}
count++;
}
return success;
}
//尋找圖像上的第一個(gè)點(diǎn)
bool findFirstPoint(Mat &_inputimg, Point &_outputpoint)
{
bool success = false;
for (int i = 0; i < _inputimg.rows; i++)
{
uchar* data = _inputimg.ptr<uchar>(i);
for (int j = 0; j < _inputimg.cols; j++)
{
if (data[j] == 255)
{
success = true;
_outputpoint.x = j;
_outputpoint.y = i;
data[j] = 0;
break;
}
}
if (success)
break;
}
return success;
}
//尋找曲線
void findLines(Mat &_inputimg, vector<deque<Point>> &_outputlines)
{
vector<Point> neighbor_points = { Point(-1,-1),Point(0,-1),Point(1,-1),Point(1,0),Point(1,1),Point(0,1),Point(-1,1),Point(-1,0) };
Point first_point;
while (findFirstPoint(_inputimg, first_point))
{
deque<Point> line;
line.push_back(first_point);
//由于第一個(gè)點(diǎn)不一定是線段的起始位置,雙向找
Point this_point = first_point;
int this_flag = 0;
Point next_point;
int next_flag;
while (findNextPoint(neighbor_points, _inputimg, this_point, this_flag, next_point, next_flag))
{
line.push_back(next_point);
this_point = next_point;
this_flag = next_flag;
}
//找另一邊
this_point = first_point;
this_flag = 0;
//cout << "flag:" << this_flag << endl;
while (findNextPoint(neighbor_points, _inputimg, this_point, this_flag, next_point, next_flag))
{
line.push_front(next_point);
this_point = next_point;
this_flag = next_flag;
}
if (line.size() > 10)
{
_outputlines.push_back(line);
}
}
}
//隨機(jī)取色 用于畫(huà)線的時(shí)候
Scalar random_color(RNG& _rng)
{
int icolor = (unsigned)_rng;
return Scalar(icolor & 0xFF, (icolor >> 8) & 0xFF, (icolor >> 16) & 0xFF);
}
int main()
{
Mat image = imread("images\\2.bmp");
Mat gray;
cvtColor(image, gray, CV_BGR2GRAY);
vector<deque<Point>> lines;
findLines(gray, lines);
cout << lines.size() << endl;
//draw lines
Mat draw_img = image.clone();
RNG rng(123);
Scalar color;
for (int i = 0; i < lines.size(); i++)
{
color = random_color(rng);
for (int j = 0; j < lines[i].size(); j++)
{
draw_img.at<Vec3b>(lines[i][j]) = Vec3b(color[0], color[1], color[2]);
}
}
imshow("draw_img", draw_img);
imwrite("images\\draw_img.bmp", draw_img);
waitKey(0);
system("pause");
return 0;
}
結(jié)果
輸入圖像

結(jié)果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++中的QCommandLineParser簡(jiǎn)介
這篇文章主要介紹了QCommandLineParser是Qt框架中的一個(gè)類,用于簡(jiǎn)化命令行參數(shù)的解析和處理,它支持定義命令行選項(xiàng)、解析參數(shù)、驗(yàn)證輸入有效性,并自動(dòng)生成幫助文本,適用于各種需要命令行輸入的應(yīng)用程序2024-09-09
C++輸出斐波那契數(shù)列的兩種實(shí)現(xiàn)方法
以下是對(duì)C++中輸出斐波那契數(shù)列的兩種實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-10-10
C++課程設(shè)計(jì)之運(yùn)動(dòng)會(huì)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++課程設(shè)計(jì)之運(yùn)動(dòng)會(huì)管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
C利用語(yǔ)言實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)之隊(duì)列
隊(duì)列 (Queue):簡(jiǎn)稱隊(duì),是另一種限定性的線性表,它只允許在表的一端插入元素,而在另一端刪除元素。q=(a1, a2, a3, … an),其中a1為隊(duì)頭,an為隊(duì)尾,下面文章小編將為大家詳細(xì)介紹,需要的下伙伴可以參考一下2021-10-10
mac 配置Clion運(yùn)行C和C++的環(huán)境的詳細(xì)步驟
這篇文章主要介紹了mac 配置Clion運(yùn)行C和C++的環(huán)境的步驟詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
結(jié)構(gòu)體類型數(shù)據(jù)作為函數(shù)參數(shù)(三種方法)
將一個(gè)結(jié)構(gòu)體中變量中的數(shù)據(jù)傳遞給另一個(gè)函數(shù),有以下三種方法。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-10-10
基于MFC實(shí)現(xiàn)自定義復(fù)選框效果
復(fù)選框是一種可同時(shí)選中多項(xiàng)的基礎(chǔ)控件,主要是有兩種明顯的狀態(tài):選中與非選中。本文將通過(guò)MFC框架實(shí)現(xiàn)自定義復(fù)選框效果,感興趣的可以了解一下2022-02-02

