openCV C++ 形态学应用 对图片进行细化,粗化和提取骨架的具体代码
时间: 2023-06-07 14:08:13 浏览: 206
对于你的第一个问题,以下是形态学应用的代码示例:
1. 细化
```c++
Mat src = imread("image.jpg", IMREAD_GRAYSCALE);
Mat dst;
threshold(src, dst, 128, 255, THRESH_BINARY);
Mat element = getStructuringElement(MORPH_CROSS, Size(3, 3));
Mat skel(src.size(), CV_8UC1, Scalar(0));
Mat temp;
Mat eroded;
do {
erode(dst, eroded, element);
dilate(eroded, temp, element);
subtract(dst, temp, temp);
bitwise_or(skel, temp, skel);
eroded.copyTo(dst);
} while (countNonZero(dst) != 0);
```
2. 粗化
```c++
Mat src = imread("image.jpg", IMREAD_GRAYSCALE);
Mat dst;
threshold(src, dst, 128, 255, THRESH_BINARY);
Mat element = getStructuringElement(MORPH_CROSS, Size(3, 3));
Mat temp;
erode(dst, temp, element);
dilate(temp, dst, element);
```
3. 提取骨架
```c++
Mat src = imread("image.jpg", IMREAD_GRAYSCALE);
Mat dst;
threshold(src, dst, 128, 255, THRESH_BINARY);
Mat element = getStructuringElement(MORPH_CROSS, Size(3, 3));
Mat skel(src.size(), CV_8UC1, Scalar(0));
Mat temp;
do {
erode(dst, temp, element);
dilate(temp, temp, element);
subtract(dst, temp, temp);
bitwise_or(skel, temp, skel);
temp.copyTo(dst);
} while (countNonZero(dst) != 0);
```
阅读全文