赞
踩
第一种:Zhang并行算法(个人表示来搞笑的)
p1 p2 p3
p8 p1 p4
p7 p6 p5
一次细化算法份两次迭代
第一次删去满足下列三个条件的点
2<= p2+p3+p4+p5+p6+p7+p8+p9<=6(0孤立点1端点78内部点)
p2->p9的排列顺序中,01模式的数量(这里假设二值图非零值为1)为1
P2*p4*p6 = 0且p4*p6*p8 = 0(移去东南角点)
第二次删去满足下列三个条件的点
2<= p2+p3+p4+p5+p6+p7+p8+p9<=6(0孤立点1端点78内部点)
p2->p9的排列顺序中,01模式的数量(这里假设二值图非零值为1)为1
P2*p4*p8 = 0且p2*p6*p8 = 0(移去西北角点)
- #include<opencv2/opencv.hpp>
- using namespace cv;
- void cvThin(cv::Mat& src, cv::Mat& dst, int intera){
- if(src.type()!=CV_8UC1){
- printf("只能处理二值或灰度图像\n");
- return;
- }
- //非原地操作时候,copy src到dst
- if(dst.data!=src.data)src.copyTo(dst);
- int i, j, n,width, height;
- width = src.cols -1;//之所以减1,是方便处理8邻域,防止越界
- height = src.rows -1;
- int step = src.step,p2,p3,p4,p5,p6,p7,p8,p9;
- uchar* img;
- bool ifEnd;
- int A1;
- cv::Mat tmpimg;//n表示迭代次数
- for(n = 0; n<intera; n++){
- dst.copyTo(tmpimg);
- ifEnd = false;
- img = tmpimg.data;
- for(i = 1; i < height; i++){
- img += step;
- for(j =1; j<width; j++){
- uchar* p = img + j;
- A1 = 0;
- if( p[0] > 0){
- if(p[-step]==0&&p[-step+1]>0)A1++; //p2,p3 01模式
- if(p[-step+1]==0&&p[1]>0)A1++; //p3,p4 01模式
- if(p[1]==0&&p[step+1]>0)A1++; //p4,p5 01模式
- if(p[step+1]==0&&p[step]>0)A1++; //p5,p6 01模式
- if(p[step]==0&&p[step-1]>0)A1++; //p6,p7 01模式
- if(p[step-1]==0&&p[-1]>0)A1++; //p7,p8 01模式
- if(p[-1]==0&&p[-step-1]>0)A1++; //p8,p9 01模式
- if(p[-step-1]==0&&p[-step]>0)A1++; //p9,p2 01模式
- p2 = p[-step]>0?1:0;
- p3 = p[-step+1]>0?1:0;
- p4 = p[1]>0?1:0;
- p5 = p[step+1]>0?1:0;
- p6 = p[step]>0?1:0;
- p7 = p[step-1]>0?1:0;
- p8 = p[-1]>0?1:0;
- p9 = p[-step-1]>0?1:0;
- if((p2+p3+p4+p5+p6+p7+p8+p9)>1 && (p2+p3+p4+p5+p6+p7+p8+p9)<7 && A1==1){
- if((p2==0||p4==0||p6==0)&&(p4==0||p6==0||p8==0)){ //p2*p4*p6=0 && p4*p6*p8==0
- dst.at<uchar>(i,j) = 0; //满足删除条件,设置当前像素为0
- ifEnd = true;
- }
- }
- }
- }
- }
- dst.copyTo(tmpimg);
- img = tmpimg.data;
- for(i = 1; i < height; i++){
- img += step;
- for(j =1; j<width; j++){
- A1 = 0;
- uchar* p = img + j;
- if( p[0] > 0){
- if(p[-step]==0&&p[-step+1]>0)A1++; //p2,p3 01模式
- if(p[-step+1]==0&&p[1]>0)A1++; //p3,p4 01模式
- if(p[1]==0&&p[step+1]>0)A1++; //p4,p5 01模式
- if(p[step+1]==0&&p[step]>0)A1++; //p5,p6 01模式
- if(p[step]==0&&p[step-1]>0)A1++; //p6,p7 01模式
- if(p[step-1]==0&&p[-1]>0)A1++; //p7,p8 01模式
- if(p[-1]==0&&p[-step-1]>0)A1++; //p8,p9 01模式
- if(p[-step-1]==0&&p[-step]>0)A1++; //p9,p2 01模式
- p2 = p[-step]>0?1:0;
- p3 = p[-step+1]>0?1:0;
- p4 = p[1]>0?1:0;
- p5 = p[step+1]>0?1:0;
- p6 = p[step]>0?1:0;
- p7 = p[step-1]>0?1:0;
- p8 = p[-1]>0?1:0;
- p9 = p[-step-1]>0?1:0;
- if((p2+p3+p4+p5+p6+p7+p8+p9)>1 && (p2+p3+p4+p5+p6+p7+p8+p9)<7 && A1==1){
- if((p2==0||p4==0||p8==0)&&(p2==0||p6==0||p8==0)){ //p2*p4*p8=0 && p2*p6*p8==0
- dst.at<uchar>(i,j) = 0; //满足删除条件,设置当前像素为0
- ifEnd = true;
- }
- }
- }
- }
- }
- //如果两个子迭代已经没有可以细化的像素了,则退出迭代
- if(!ifEnd) break;
- }
- }
- int main(){
- Mat src=imread("1.jpg",0);
- imshow("abc",src);
- Mat dst;
- cvThin(src,dst,30);
- imshow("def",dst);
- waitKey(0);
- return 0;
- }
-
-
第二种:Hilditch算法(个人表示来搞笑的)
p9 p2 p3
p8 p1 p4
p7 p6 p5
每次迭代删除符合下列条件的点
2<= p2+p3+p4+p5+p6+p7+p8+p9<=6
p2->p9的排列顺序中,01模式的数量为1(如p2=0,p3=1这就叫一个01模式)
p2.p4.p8 = 0 or A(p2)!=1,A(p2)表示p2周围8邻域的01模式和(保证2个像素宽的垂直条不完全被腐蚀掉)
p2.p4.p6 = 0 or A(p4)!=1,A(p4)表示p4周围8邻域的01模式和(保证2个像素宽的水平条不完全被腐蚀掉)
- #include<opencv2/opencv.hpp>
- using namespace cv;
- void cvHilditchThin1(cv::Mat& src, cv::Mat& dst){
- if(src.type()!=CV_8UC1){
- printf("只能处理二值或灰度图像\n");
- return;
- }
- //非原地操作时候,copy src到dst
- if(dst.data!=src.data)src.copyTo(dst);
- int i, j,width, height;
- //之所以减2,是方便处理8邻域,防止越界
- width = src.cols -2;
- height = src.rows -2;
- int step = src.step;
- int p2,p3,p4,p5,p6,p7,p8,p9;
- uchar* img;
- bool ifEnd;
- int A1;
- cv::Mat tmpimg;
- while(1){
- dst.copyTo(tmpimg);
- ifEnd = false;
- img = tmpimg.data+step;
- for(i = 2; i < height; i++){
- img += step;
- for(j =2; j<width; j++){
- uchar* p = img + j;
- A1 = 0;
- if( p[0] > 0){
- if(p[-step]==0&&p[-step+1]>0)A1++; //p2,p3 01模式
- if(p[-step+1]==0&&p[1]>0)A1++; //p3,p4 01模式
- if(p[1]==0&&p[step+1]>0)A1++; //p4,p5 01模式
- if(p[step+1]==0&&p[step]>0)A1++; //p5,p6 01模式
- if(p[step]==0&&p[step-1]>0)A1++; //p6,p7 01模式
- if(p[step-1]==0&&p[-1]>0)A1++; //p7,p8 01模式
- if(p[-1]==0&&p[-step-1]>0)A1++; //p8,p9 01模式
- if(p[-step-1]==0&&p[-step]>0)A1++; //p9,p2 01模式
- p2 = p[-step]>0?1:0;
- p3 = p[-step+1]>0?1:0;
- p4 = p[1]>0?1:0;
- p5 = p[step+1]>0?1:0;
- p6 = p[step]>0?1:0;
- p7 = p[step-1]>0?1:0;
- p8 = p[-1]>0?1:0;
- p9 = p[-step-1]>0?1:0;
- //计算AP2,AP4
- int A2, A4;
- A2 = 0;
- if(p[-step]>0){
- if(p[-2*step]==0&&p[-2*step+1]>0) A2++;
- if(p[-2*step+1]==0&&p[-step+1]>0) A2++;
- if(p[-step+1]==0&&p[1]>0) A2++;
- if(p[1]==0&&p[0]>0) A2++;
- if(p[0]==0&&p[-1]>0) A2++;
- if(p[-1]==0&&p[-step-1]>0) A2++;
- if(p[-step-1]==0&&p[-2*step-1]>0) A2++;
- if(p[-2*step-1]==0&&p[-2*step]>0) A2++;
- }
- A4 = 0;
- if(p[1]>0){
- if(p[-step+1]==0&&p[-step+2]>0) A4++;
- if(p[-step+2]==0&&p[2]>0) A4++;
- if(p[2]==0&&p[step+2]>0) A4++;
- if(p[step+2]==0&&p[step+1]>0) A4++;
- if(p[step+1]==0&&p[step]>0) A4++;
- if(p[step]==0&&p[0]>0) A4++;
- if(p[0]==0&&p[-step]>0) A4++;
- if(p[-step]==0&&p[-step+1]>0) A4++;
- }
- //printf("p2=%d p3=%d p4=%d p5=%d p6=%d p7=%d p8=%d p9=%d\n", p2, p3, p4, p5, p6,p7, p8, p9);
- //printf("A1=%d A2=%d A4=%d\n", A1, A2, A4);
- if((p2+p3+p4+p5+p6+p7+p8+p9)>1 && (p2+p3+p4+p5+p6+p7+p8+p9)<7 && A1==1){
- if(((p2==0||p4==0||p8==0)||A2!=1)&&((p2==0||p4==0||p6==0)||A4!=1)){
- dst.at<uchar>(i,j) = 0; //满足删除条件,设置当前像素为0
- ifEnd = true;
- //printf("\n");
- //PrintMat(dst);
- }
- }
- }
- }
- }
- //已经没有可以细化的像素了,则退出迭代
- if(!ifEnd) break;
- }
- }
- int main(){
- Mat src=imread("1.jpg",0);
- imshow("abc",src);
- Mat dst;
- cvHilditchThin1(src,dst);
- imshow("def",dst);
- waitKey(0);
- return 0;
- }
-
-
三、形态学算法(个人表示最简单,第三选择)
通过十字型结构元素开操作的的补集和当前像素相与来作为骨架。当图像腐蚀为全0时候,就得到了整个图像的骨架
while (not_empty(img)) { skel = skel | (img & !open(img)); img = erosion(img); }
- #include<opencv2/opencv.hpp>
- using namespace cv;
- void cvmorphThin(cv::Mat& src, cv::Mat& dst){
- if(src.type()!=CV_8UC1){
- printf("只能处理二值或灰度图像\n");
- return;
- }
- //非原地操作时候,copy src到dst
- if(dst.data!=src.data)src.copyTo(dst);
- cv::Mat skel(dst.size(), CV_8UC1, cv::Scalar(0));
- cv::Mat temp(dst.size(), CV_8UC1);
- cv::Mat element = cv::getStructuringElement(cv::MORPH_CROSS, cv::Size(3, 3));
- bool done;
- do{
- cv::morphologyEx(dst, temp, cv::MORPH_OPEN, element);
- cv::bitwise_not(temp, temp);
- cv::bitwise_and(dst, temp, temp);
- cv::bitwise_or(skel, temp, skel);
- cv::erode(dst, dst, element);
- double max;
- cv::minMaxLoc(dst, 0, &max);
- done = (max == 0);
- } while (!done);
- dst = skel;
- }
- int main(){
- Mat src=imread("1.jpg",0);
- imshow("abc",src);
- Mat dst;
- cvmorphThin(src,dst);
- imshow("def",dst);
- waitKey(0);
- return 0;
- }
-
-
四、Rosenfeld算法(个人表示最优,第一选择)
- #include<opencv2/opencv.hpp>
- using namespace cv;
- void cvRosenfeld(cv::Mat& src, cv::Mat& dst){
- if(src.type()!=CV_8UC1) {
- printf("只能处理二值或灰度图像\n");
- return;
- }
- //非原地操作时候,copy src到dst
- if(dst.data!=src.data){
- src.copyTo(dst);
- }
- int i, j, n,width, height;
- //之所以减1,是方便处理8邻域,防止越界
- width = src.cols -1;
- height = src.rows -1;
- int step = src.step;
- int p2,p3,p4,p5,p6,p7,p8,p9;
- uchar* img;
- bool ifEnd;
- cv::Mat tmpimg;
- int dir[4] = {-step, step, 1, -1};
- while(1){
- //分四个子迭代过程,分别对应北,南,东,西四个边界点的情况
- ifEnd = false;
- for(n =0; n < 4; n++){
- dst.copyTo(tmpimg);
- img = tmpimg.data;
- for(i = 1; i < height; i++){
- img += step;
- for(j =1; j<width; j++){
- uchar* p = img + j;
- //如果p点是背景点或者且为方向边界点,依次为北南东西,继续循环
- if(p[0]==0||p[dir[n]]>0) continue;
- p2 = p[-step]>0?1:0;
- p3 = p[-step+1]>0?1:0;
- p4 = p[1]>0?1:0;
- p5 = p[step+1]>0?1:0;
- p6 = p[step]>0?1:0;
- p7 = p[step-1]>0?1:0;
- p8 = p[-1]>0?1:0;
- p9 = p[-step-1]>0?1:0;
- //8 simple判定
- int is8simple = 1;
- if(p2==0&&p6==0){
- if((p9==1||p8==1||p7==1)&&(p3==1||p4==1||p5==1))
- is8simple = 0;
- }
- if(p4==0&&p8==0){
- if((p9==1||p2==1||p3==1)&&(p5==1||p6==1||p7==1))
- is8simple = 0;
- }
- if(p8==0&&p2==0){
- if(p9==1&&(p3==1||p4==1||p5==1||p6==1||p7==1))
- is8simple = 0;
- }
- if(p4==0&&p2==0){
- if(p3==1&&(p5==1||p6==1||p7==1||p8==1||p9==1))
- is8simple = 0;
- }
- if(p8==0&&p6==0){
- if(p7==1&&(p3==9||p2==1||p3==1||p4==1||p5==1))
- is8simple = 0;
- }
- if(p4==0&&p6==0){
- if(p5==1&&(p7==1||p8==1||p9==1||p2==1||p3==1))
- is8simple = 0;
- }
- int adjsum;
- adjsum = p2 + p3 + p4+ p5 + p6 + p7 + p8 + p9;
- //判断是否是邻接点或孤立点,0,1分别对于那个孤立点和端点
- if(adjsum!=1&&adjsum!=0&&is8simple==1){
- dst.at<uchar>(i,j) = 0; //满足删除条件,设置当前像素为0
- ifEnd = true;
- }
- }
- }
- }
- //已经没有可以细化的像素了,则退出迭代
- if(!ifEnd) break;
- }
- }
- int main(){
- Mat src=imread("1.jpg",0);
- threshold(src,src,100,255,0);//注意一定要化二值,不仅是灰度图
- imshow("abc",src);
- Mat dst;cvRosenfeld(src, dst);
- imshow("def",dst);
- waitKey(0);
- return 0;
- }
-
-
五、查表法(个人表示次优,第二选择)
二值图,前景值为1,背景值为0
p0 p1 p2
p7 p3
p6 p5 p4
8邻域,用8位二进制表示,打表枚举每种情况
- #include<opencv2/opencv.hpp>
- using namespace cv;
- void ImgSkeletonization_H(Mat &input_src, int *search_arr){
- int h = input_src.rows;
- int w = input_src.cols;
- bool NEXT = true;
- for (size_t j = 1; j < w - 1; j++){//注意边界问题!!!!!!
- for (size_t i = 1; i < h - 1; i++){
- if (!NEXT)
- NEXT = true;
- else{
- int judge_value;
- if (1 <i < h - 1)
- judge_value = input_src.at<uchar>(i - 1, j) + input_src.at<uchar>(i, j) + input_src.at<uchar>(i + 1, j);
- else
- judge_value = 1;
- if (input_src.at<uchar>(i, j) == 0 && judge_value != 0){
- int a[9] = { 1,1,1,1,1,1,1,1,1};
- for (size_t m = 0; m < 3; m++){
- for (size_t n = 0; n < 3; n++){
- if ((0 <= (i - 1 + m) < h) && (0 <= (j - 1 + n) < w) && input_src.at<uchar>(i - 1 + m, j - 1 + n) == 0)
- a[m * 3 + n] = 0;
- }
- }
- int sum_value = a[0] * 1 + a[1] * 2 + a[2] * 4 + a[3] * 8 + a[5] * 16 + a[6] * 32 + a[7] * 64 + a[8] * 128;
- input_src.at<uchar>(i, j) = search_arr[sum_value] * 255;
- if (search_arr[sum_value] == 1)
- NEXT = false;
- }
- }
- }
- }
- }
- void ImgSkeletonization_V(Mat &input_src, int *search_arr){
- int h = input_src.rows;
- int w = input_src.cols;
- bool NEXT = true;
- for (size_t i = 1; i < h - 1; i++){//注意边界问题!!!!!!
- for (size_t j = 1; j < w - 1; j++){
- if (!NEXT)
- NEXT = true;
- else{
- int judge_value;
- if (1 < j <w - 1)
- judge_value = input_src.at<uchar>(i, j - 1) + input_src.at<uchar>(i, j) + input_src.at<uchar>(i, j + 1);
- else
- judge_value = 1;
- if (input_src.at<uchar>(i, j) == 0 && judge_value != 0){
- int a[9] = {1,1,1,1,1,1,1,1,1 };
- for (size_t m = 0; m < 3; m++){
- for (size_t n = 0; n < 3; n++){
- if ((0 <= (i - 1 + m) < h) && (0 <= (j - 1 + n) < w) && input_src.at<uchar>(i - 1 + m, j - 1 + n) == 0)
- a[m * 3 + n] = 0;
- }
- }
- int sum_value = a[0] * 1 + a[1] * 2 + a[2] * 4 + a[3] * 8 + a[5] * 16 + a[6] * 32 + a[7] * 64 + a[8] * 128;
- input_src.at<uchar>(i, j) = search_arr[sum_value] * 255;
- if (search_arr[sum_value] == 1)
- NEXT = false;
- }
- }
- }
- }
- }
- void ImgSkeletonization(Mat &input_src,Mat & output_dst, int number){
- output_dst = input_src.clone();
- int search_array[]= { 0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,\
- 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,1,\
- 0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,\
- 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,1,\
- 1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,\
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
- 1,1,0,0,1,1,0,0,1,1,0,1,1,1,0,1,\
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
- 0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,\
- 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,1,\
- 0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,\
- 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,\
- 1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,\
- 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,\
- 1,1,0,0,1,1,0,0,1,1,0,1,1,1,0,0,\
- 1,1,0,0,1,1,1,0,1,1,0,0,1,0,0,0 };
- for (size_t i = 0; i < number; i++){
- ImgSkeletonization_H(output_dst, &search_array[0]);
- ImgSkeletonization_V(output_dst, &search_array[0]);
-
- }
- }
- int main(){
- Mat src=imread("2.jpg",0);
- threshold(src,src,100,255,0);//注意一定要化二值,不仅是灰度图
- imshow("abc",src);
- Mat dst;ImgSkeletonization(src, dst, 50);
- imshow("def",dst);
- waitKey(0);
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。