国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

基于Opencv實(shí)現(xiàn)Matlab的bwmorph中的bridge操作

2019-11-14 10:57:55
字體:
供稿:網(wǎng)友

工程需要,但是不能用matlab,所以用Opencv實(shí)現(xiàn)了一下bridge,也不難。

一. bridge操作的定義

在官方文檔https://cn.mathworks.com/help/images/ref/bwmorph.html中,bridge操作的定義如下: Bridges unconnected pixels, that is, sets 0-valued pixels to 1 if they have two nonzero neighbors that are not connected. For example: 1 0 0 1 0 1 0 0 1 becomes 1 1 0 1 1 1 0 1 1 也就是說,在一個(gè)二值化后的圖像中,對(duì)于任意一個(gè)值為0的像素點(diǎn),如果它周圍(4連通或8連通)存在至少2個(gè)值為1且屬于不同連通區(qū)域的像素點(diǎn),則把這個(gè)像素點(diǎn)值修改為1。

二. 實(shí)現(xiàn)思路

按照定義把所有值為0的像素點(diǎn)都走一遍就ok了,注意每次修改后,需要更新連通區(qū)域的信息。

三. 代碼實(shí)現(xiàn)

#include <iostream>#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgPRoc/imgproc.hpp>#include <set>using namespace cv;using namespace std;const int dx[] = { 0, 1, 0, -1, 1, -1, 1, -1 };const int dy[] = { 1, 0, -1, 0, 1, -1, -1, 1 };set<int> get_neighbors1(Mat src, Mat label_mat, int x, int y, int connectivity = 8) { set<int> neighbors; for (int i = 0; i < connectivity; ++i) { int tmp_x = x + dx[i]; int tmp_y = y + dy[i]; if (tmp_x >= 0 && tmp_x < src.rows && tmp_y >= 0 && tmp_y < src.cols && src.at<uchar>(tmp_x, tmp_y) == 255) { neighbors.insert(label_mat.at<int>(tmp_x, tmp_y)); } } return neighbors;}void bridge(Mat src, Mat &dst, int connectivity = 8) { Mat label_mat; connectedComponents(src, label_mat, connectivity); dst = src.clone(); for (int i = 0; i < src.rows; ++i) { for (int j = 0; j < src.cols; ++j) { uchar pixel = src.at<uchar>(i, j); if (pixel == 0) { if (get_neighbors1(src, label_mat, i, j, 4).size() >= 2) { dst.at<uchar>(i, j) = 255; connectedComponents(dst, label_mat, connectivity); } } } }}int main() { Mat image = imread("C://Users//whai//Desktop//1.png"); cvtColor(image, image, COLOR_BGR2GRAY); // Canny(image, image, 100, 200, 3); imshow("修復(fù)前", image); waitKey(); Mat dst; bridge(image, dst, 4); imshow("修復(fù)后", dst); imwrite("C://Users//whai//Desktop//2.png", dst); waitKey(); return 0;}

四. 效果

bridge前 bridge前

bridge后 bridge后


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 赣州市| 泾源县| 丰都县| 武功县| 佛教| 项城市| 黄陵县| 桃源县| 建德市| 农安县| 杂多县| 嫩江县| 荣成市| 博客| 定襄县| 鸡泽县| 溧水县| 穆棱市| 桃园市| 曲沃县| 同心县| 广州市| 福清市| 新沂市| 星座| 会同县| 莱阳市| 留坝县| 五大连池市| 婺源县| 温宿县| 中方县| 江门市| 阳山县| 旺苍县| 南昌市| 中江县| 滨海县| 德昌县| 酒泉市| 思南县|