打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
一种细化算法及其与opencv的实现

是根据图像中像素点得八个相邻点得情况,得到一个索引值,然后查表,如果为0怎保留该点,否则删除

算法代码:

  1. #include "stdafx.h"   
  2. #include "cv.h"   
  3. #include "highgui.h"   
  4. #include "cxcore.h"   
  5. #include<iostream>   
  6. #include<fstream>   
  7. #include<iomanip>   
  8.   
  9. using namespace std;  
  10.   
  11. static int erasetable[256] = {  
  12.     0,0,1,1,0,0,1,1,     1,1,0,1,1,1,0,1,  
  13.     1,1,0,0,1,1,1,1,     0,0,0,0,0,0,0,1,  
  14.     0,0,1,1,0,0,1,1,     1,1,0,1,1,1,0,1,  
  15.     1,1,0,0,1,1,1,1,     0,0,0,0,0,0,0,1,  
  16.     1,1,0,0,1,1,0,0,     0,0,0,0,0,0,0,0,  
  17.     0,0,0,0,0,0,0,0,     0,0,0,0,0,0,0,0,  
  18.     1,1,0,0,1,1,0,0,     1,1,0,1,1,1,0,1,  
  19.     0,0,0,0,0,0,0,0,     0,0,0,0,0,0,0,0,  
  20.     0,0,1,1,0,0,1,1,     1,1,0,1,1,1,0,1,  
  21.     1,1,0,0,1,1,1,1,     0,0,0,0,0,0,0,1,  
  22.     0,0,1,1,0,0,1,1,     1,1,0,1,1,1,0,1,  
  23.     1,1,0,0,1,1,1,1,     0,0,0,0,0,0,0,0,  
  24.     1,1,0,0,1,1,0,0,     0,0,0,0,0,0,0,0,  
  25.     1,1,0,0,1,1,1,1,     0,0,0,0,0,0,0,0,  
  26.     1,1,0,0,1,1,0,0,     1,1,0,1,1,1,0,0,  
  27.     1,1,0,0,1,1,1,0,     1,1,0,0,1,0,0,0  
  28. };//这个表是检测是用来细化黑色点边缘的,若为0则保留,否则删除,如果用来细化白色点边缘的话就取fan   
  29. /******************************************************************888 
  30. 函数名:save 
  31. 参数类型:void 
  32. 功能:保存细化查询表 
  33. ****************************************************************************/  
  34. void save()  
  35. {  
  36.     fstream writefile("erasetable.txt" , ios::out);  
  37.     int n=0;  
  38.     for(int i=0;i<256;i++)  
  39.     {  
  40.             writefile<<setw(2)<<erasetable[i];  
  41.     }  
  42.     writefile<<endl;  
  43. }  
  44. /**********************************************************88 
  45. 函数名:npow 
  46. 参数类型:int n 
  47. 功能:求2的n次方并返回结果 
  48. 返回值类型:int 
  49. ***************************************************************/  
  50. int npow(int n)  
  51. {  
  52.     int mul = 1;  
  53.     for(int i=0;i<n;i++)  
  54.     {  
  55.         mul *= 2;  
  56.     }  
  57.     return mul;  
  58. }  
  59. /*************************************************************************** 
  60. 函数名:Threshold 
  61. 参数类型:IplImage *src , int lower , int higher 
  62. 功能:二值化灰度图像 
  63. *******************************************************************************/  
  64. void Threshold(IplImage *src , int lower , int higher)  
  65. {  
  66.     assert(src->nChannels==1);  
  67.     for(int h=0;h<src->height;h++)  
  68.         for(int w=0;w<src->width;w++)  
  69.         {  
  70.             if(*(src->imageData +h*src->widthStep+w)>0&&*(src->imageData+h*src->widthStep+w)<87)  
  71.                 *(src->imageData +h*src->widthStep+w) = 255;  
  72.             else  
  73.                 *(src->imageData +h*src->widthStep+w) = 0;  
  74.   
  75.         }  
  76. }  
  77. /*****************************************************************************8 
  78. 函数名:scantable 
  79. 参数类型:IplImage *src 
  80. 功能:扫描像素8领域周围的八个像素值(只检测白色点得周围),像素值为0置1,否则为0,并保存(如果是检测黑色的周围的话就相反), 
  81. 再根据得到的数,将得到的数看做一个二进制数化为十进制的数,这个值即为查询索引,如果查到的值为0则保留,否则删除 
  82. ***************************************************************************************/  
  83. void scantable(IplImage *src)  
  84. {  
  85.     assert(src->nChannels==1);  
  86.     int scan[8] = {0};  
  87.     for(int h=1;h<(src->height-1);h++)  
  88.     {  
  89.         for(int w=1;w<(src->width-1);w++)  
  90.         {  
  91.             int index = 0;  
  92.             if(*(src->imageData+(h)*src->widthStep+w)!=0)  
  93.             {  
  94.             if(*(src->imageData+(h-1)*src->widthStep+w-1)==0)  
  95.                 scan[0] = 1;  
  96.             else  
  97.                 scan[0] = 0;  
  98.             if(*(src->imageData+(h-1)*src->widthStep+w)==0)  
  99.                 scan[1] = 1;  
  100.             else  
  101.                 scan[1] = 0;  
  102.             if(*(src->imageData+(h-1)*src->widthStep+w+1)==0)  
  103.                 scan[2] = 1;  
  104.             else  
  105.                 scan[2] = 0;  
  106.             if(*(src->imageData+(h)*src->widthStep+w-1)==0)  
  107.                 scan[3] = 1;  
  108.             else  
  109.                 scan[3] = 0;  
  110.             if(*(src->imageData+(h)*src->widthStep+w+1)==0)  
  111.                 scan[4] = 1;  
  112.             else  
  113.                 scan[4] = 0;  
  114.             if(*(src->imageData+(h+1)*src->widthStep+w-1)==0)  
  115.                 scan[5] = 1;  
  116.             else  
  117.                 scan[5] = 0;  
  118.             if(*(src->imageData+(h+1)*src->widthStep+w)==0)  
  119.                 scan[6] = 1;  
  120.             else  
  121.                 scan[6] = 0;  
  122.             if(*(src->imageData+(h+1)*src->widthStep+w+1)==0)  
  123.                 scan[7] = 1;  
  124.             else  
  125.                 scan[7] = 0;  
  126.          
  127.   
  128.             for(int i=0;i<8;i++)  
  129.             {  
  130.                 index += scan[i]*npow(i);  
  131.             }  
  132.           //  printf("%d\n" ,index);   
  133.             if(erasetable[index] == 1)  
  134.             {  
  135.                 //printf("%d\n" , erasetable[index]);   
  136.                 *(src->imageData+h*src->widthStep+w) = 0;  
  137.             }  
  138.             }  
  139.   
  140.         }  
  141.     }  
  142. }  
  143.   
  144. int _tmain(int argc, _TCHAR* argv[])  
  145. {  
  146.     save();  
  147.     IplImage *img = 0;  
  148.     IplImage *gray = 0;  
  149.     IplImage *copy_gray = 0;  
  150.     int vmin = 0;  
  151.     int vmax = 0;  
  152.   
  153.     img = cvLoadImage("1.bmp" , 1);  
  154.     gray = cvCreateImage(cvGetSize(img) , 8 , 1);  
  155.     copy_gray = cvCreateImage(cvGetSize(img) , 8 ,1);  
  156.   
  157.     cvCvtColor(img , gray , CV_BGR2GRAY);  
  158.     cvSmooth(gray , gray , CV_MEDIAN , 3 , 0 , 0 , 0);  
  159.   
  160.     cvNamedWindow("gray" , 1);  
  161.     cvNamedWindow("erase" , 1);  
  162.     //cvCreateTrackbar("lower" , "gray" , &vmin , 256 , 0);   
  163.     //cvCreateTrackbar("higher" , "gray" , &vmax , 256 , 0);   
  164.   
  165.      Threshold(gray , vmin , vmax);  
  166.      cvCopy(gray , copy_gray);  
  167.      scantable(copy_gray);  
  168.         cvShowImage("gray" , gray);  
  169.         cvShowImage("erase" , copy_gray);  
  170.   
  171.         if(cvWaitKey(0)==27)  
  172.         {  
  173.           cvReleaseImage(&img);  
  174.           cvReleaseImage(&gray);  
  175.           cvReleaseImage(?_gray);  
  176.         }  
  177.   
  178.   
  179.     return 0;  
  180. }  


 

 原图:

 

效果图:

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
分水岭分割算法
OPENCV2学习(1)
工业相机SDK之opencv二次开发
【scrapy】学习Scrapy入门(3)
SURF算法浅析(转)_dzh_漫漫修行路
图像算法工程师
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服