请帮我编写C代码或C++代码,实现两种骨架提取算法:顺序细化和北、南、东、西提取算法。
时间: 2023-12-03 15:46:50 浏览: 60
顺序细化算法的C代码实现:
```c
#include <stdio.h>
#define M 8
#define N 8
int skeleton[M][N] = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 0, 0, 0, 1, 0},
{0, 1, 1, 0, 0, 0, 1, 0},
{0, 0, 0, 1, 0, 1, 0, 0},
{0, 0, 0, 0, 1, 1, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
};
int neighbors(int i, int j) {
int count = 0;
for (int ii = i - 1; ii <= i + 1; ii++) {
for (int jj = j - 1; jj <= j + 1; jj++) {
if (ii >= 0 && ii < M && jj >= 0 && jj < N) {
if (skeleton[ii][jj] == 1) {
count++;
}
}
}
}
return count;
}
int check(int i, int j) {
int count = 0;
if (skeleton[i][j] == 0) {
return 0;
}
for (int ii = i - 1; ii <= i + 1; ii++) {
for (int jj = j - 1; jj <= j + 1; jj++) {
if (ii >= 0 && ii < M && jj >= 0 && jj < N) {
if (skeleton[ii][jj] == 1) {
count++;
}
}
}
}
return count >= 2 && count <= 6;
}
void thinImage() {
int flag = 1;
while (flag) {
flag = 0;
for (int i = 1; i < M - 1; i++) {
for (int j = 1; j < N - 1; j++) {
if (skeleton[i][j] == 1 && check(i, j)) {
if (neighbors(i, j) >= 2 && neighbors(i, j) <= 6) {
int a = 0;
if (skeleton[i - 1][j] == 0 && skeleton[i - 1][j + 1] == 1) {
a++;
}
if (skeleton[i - 1][j + 1] == 0 && skeleton[i][j + 1] == 1) {
a++;
}
if (skeleton[i][j + 1] == 0 && skeleton[i + 1][j + 1] == 1) {
a++;
}
if (skeleton[i + 1][j + 1] == 0 && skeleton[i + 1][j] == 1) {
a++;
}
if (skeleton[i + 1][j] == 0 && skeleton[i + 1][j - 1] == 1) {
a++;
}
if (skeleton[i + 1][j - 1] == 0 && skeleton[i][j - 1] == 1) {
a++;
}
if (skeleton[i][j - 1] == 0 && skeleton[i - 1][j - 1] == 1) {
a++;
}
if (skeleton[i - 1][j - 1] == 0 && skeleton[i - 1][j] == 1) {
a++;
}
if (a == 1) {
skeleton[i][j] = 0;
flag = 1;
}
}
}
}
}
}
}
int main() {
// 输出原始骨架
printf("Original Skeleton:\n");
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
printf("%d", skeleton[i][j]);
}
printf("\n");
}
// 调用顺序细化算法
thinImage();
// 输出细化后的骨架
printf("Thinned Skeleton:\n");
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
printf("%d", skeleton[i][j]);
}
printf("\n");
}
return 0;
}
```
北、南、东、西提取算法的C++代码实现:
```c++
#include <iostream>
#include <vector>
#define M 8
#define N 8
int skeleton[M][N] = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 0, 0, 0, 1, 0},
{0, 1, 1, 0, 0, 0, 1, 0},
{0, 0, 0, 1, 0, 1, 0, 0},
{0, 0, 0, 0, 1, 1, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
};
void north_thinning(std::vector<int>& flag) {
for (int i = 1; i < M - 1; i++) {
for (int j = 1; j < N - 1; j++) {
if (skeleton[i][j] == 1) {
int p2 = skeleton[i - 1][j];
int p3 = skeleton[i - 1][j + 1];
int p4 = skeleton[i][j + 1];
int p5 = skeleton[i + 1][j + 1];
int p6 = skeleton[i + 1][j];
int p7 = skeleton[i + 1][j - 1];
int p8 = skeleton[i][j - 1];
int p9 = skeleton[i - 1][j - 1];
int C = (!p2 && (p3 || p4)) + (!p4 && (p5 || p6)) +
(!p6 && (p7 || p8)) + (!p8 && (p9 || p2));
int N1 = (p9 || p2) + (p3 || p4) + (p5 || p6) + (p7 || p8);
int N2 = (p2 || p3) + (p4 || p5) + (p6 || p7) + (p8 || p9);
int N = N1 < N2 ? N1 : N2;
int m = ((p6 || p7 || !p9) && p8) + ((p2 || p3 || !p5) && p4);
if (C == 1 && (N >= 2 && N <= 3) && m == 0) {
flag.push_back(i * N + j);
}
}
}
}
}
void south_thinning(std::vector<int>& flag) {
for (int i = 1; i < M - 1; i++) {
for (int j = 1; j < N - 1; j++) {
if (skeleton[i][j] == 1) {
int p2 = skeleton[i - 1][j];
int p3 = skeleton[i - 1][j + 1];
int p4 = skeleton[i][j + 1];
int p5 = skeleton[i + 1][j + 1];
int p6 = skeleton[i + 1][j];
int p7 = skeleton[i + 1][j - 1];
int p8 = skeleton[i][j - 1];
int p9 = skeleton[i - 1][j - 1];
int C = (!p2 && (p3 || p4)) + (!p4 && (p5 || p6)) +
(!p6 && (p7 || p8)) + (!p8 && (p9 || p2));
int N1 = (p9 || p2) + (p3 || p4) + (p5 || p6) + (p7 || p8);
int N2 = (p2 || p3) + (p4 || p5) + (p6 || p7) + (p8 || p9);
int N = N1 < N2 ? N1 : N2;
int m = ((p2 || p3 || !p7) && p4) + ((p6 || p5 || !p9) && p8);
if (C == 1 && (N >= 2 && N <= 3) && m == 0) {
flag.push_back(i * N + j);
}
}
}
}
}
void east_thinning(std::vector<int>& flag) {
for (int i = 1; i < M - 1; i++) {
for (int j = 1; j < N - 1; j++) {
if (skeleton[i][j] == 1) {
int p2 = skeleton[i - 1][j];
int p3 = skeleton[i - 1][j + 1];
int p4 = skeleton[i][j + 1];
int p5 = skeleton[i + 1][j + 1];
int p6 = skeleton[i + 1][j];
int p7 = skeleton[i + 1][j - 1];
int p8 = skeleton[i][j - 1];
int p9 = skeleton[i - 1][j - 1];
int C = (!p2 && (p3 || p4)) + (!p4 && (p5 || p6)) +
(!p6 && (p7 || p8)) + (!p8 && (p9 || p2));
int N1 = (p9 || p2) + (p3 || p4) + (p5 || p6) + (p7 || p8);
int N2 = (p2 || p3) + (p4 || p5) + (p6 || p7) + (p8 || p9);
int N = N1 < N2 ? N1 : N2;
int m = ((p2 || p3 || !p4) && p6) + ((p4 || p5 || !p8) && p2);
if (C == 1 && (N >= 2 && N <= 3) && m == 0) {
flag.push_back(i * N + j);
}
}
}
}
}
void west_thinning(std::vector<int>& flag) {
for (int i = 1; i < M - 1; i++) {
for (int j = 1; j < N - 1; j++) {
if (skeleton[i][j] == 1) {
int p2 = skeleton[i - 1][j];
int p3 = skeleton[i - 1][j + 1];
int p4 = skeleton[i][j + 1];
int p5 = skeleton[i + 1][j + 1];
int p6 = skeleton[i + 1][j];
int p7 = skeleton[i + 1][j - 1];
int p8 = skeleton[i][j - 1];
int p9 = skeleton[i - 1][j - 1];
int C = (!p2 && (p3 || p4)) + (!p4 && (p5 || p6)) +
(!p6 && (p7 || p8)) + (!p8 && (p9 || p2));
int N1 = (p9 || p2) + (p3 || p4) + (p5 || p6) + (p7 || p8);
int N2 = (p2 || p3) + (p4 || p5) + (p6 || p7) + (p8 || p9);
int N = N1 < N2 ? N1 : N2;
int m = ((p6 || p5 || !p3) && p8) + ((p4 || p3 || !p1) && p6);
if (C == 1 && (N >= 2 && N <= 3) && m == 0) {
flag.push_back(i * N + j);
}
}
}
}
}
void thinImage() {
std::vector<int> flag;
int count = 0;
while (true) {
count++;
flag.clear();
north_thinning(flag);
south_thinning(flag);
east_thinning(flag);
west_thinning(flag);
if (flag.empty()) {
break;
}
for (int i = 0; i < flag.size(); i++) {
int row = flag[i] / N;
int col = flag[i] % N;
skeleton[row][col] = 0;
}
}
}
int main() {
// 输出原始骨架
std::cout << "Original Skeleton:" << std::endl;
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
std::cout << skeleton[i][j];
}
std::cout << std::endl;
}
// 调用北、南、东、西提取算法
thinImage();
// 输出细化后的骨架
std::cout << "Thinned Skeleton:" << std::endl;
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
std::cout << skeleton[i][j];
}
std::cout << std::endl;
}
return 0;
}
```
阅读全文