OpenCV如何提取圖片中曲線
簡單介紹
在實際的應(yīng)用中,我們常常需要對圖像中的曲線進行描述、處理,這個曲線可以是輪廓,骨架或者其他??梢杂?span style="color: #800000">deque<Point> 描述曲線,接下來簡單介紹下如何從圖片中搜索這些曲線并保存。
首先,輸入的圖片是一張二值圖片 (白色為曲線),其中包含的曲線寬度為 1 像素的 (如果曲線不是 1 像素的 先提取其骨架)。遍歷尋找圖像中第一個白色的點,然后從這個點開始延伸尋找曲線。注意,第一個找到的點不一定是曲線的端點,因此應(yīng)該分別向兩邊尋找相鄰的點,因此deque 會好一些。每找到一個點,將其保存deque 而后置黑(防止重復(fù)尋找)。搜索到一個沒有相鄰點的點,表示一端搜索完成。
值得注意的一點是,我在寫搜尋相鄰點的時候,會首先搜尋此點與上一個點相鄰位置相對的位置,如果沒有,則分別搜索向兩邊搜索。這樣的好處是可以減少尋找的次數(shù),而且當有相交的曲線時,能連接到我們一般認為的曲線。
代碼
//尋找圖像曲線上某個點的下一個點 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; } //尋找圖像上的第一個點 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); //由于第一個點不一定是線段的起始位置,雙向找 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); } } } //隨機取色 用于畫線的時候 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é)果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:opencv檢測直線方法之形態(tài)學(xué)方法
欄 目:C語言
本文標題:OpenCV如何提取圖片中曲線
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/553.html
您可能感興趣的文章
- 01-10如何判斷一個數(shù)是否為2的冪次方?若是,并判斷出來是多少次方
- 01-10如何判斷一個數(shù)是否為4的冪次方?若是,并判斷出來是多少次方
- 01-10如何查看進程實際的內(nèi)存占用情況詳解
- 01-10如何尋找數(shù)組中的第二大數(shù)
- 01-10解析如何在C語言中調(diào)用shell命令的實現(xiàn)方法
- 01-10如何用C語言去除字符串兩邊的空字符
- 01-10如何判斷一個整數(shù)的二進制中有多少個1
- 01-10如何用C語言生成簡單格式的xml
- 01-10如何求連續(xù)幾個數(shù)之和的最大值
- 01-10如何在二叉樹中找出和為某一值的所有路徑


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改