Cvthin 细化算法的简化(LUT)
来源:互联网 发布:淘宝小二介入买家输 编辑:程序博客网 时间:2024/06/07 00:49
是根据图像中像素点得八个相邻点得情况,得到一个索引值,然后查表,如果为0怎保留该点,否则删除
算法代码:
- #include "stdafx.h"
- #include "cv.h"
- #include "highgui.h"
- #include "cxcore.h"
- #include<iostream>
- #include<fstream>
- #include<iomanip>
-
- using namespace std;
-
- static int erasetable[256] = {
- 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
- };
-
-
-
-
-
- void save()
- {
- fstream writefile("erasetable.txt" , ios::out);
- int n=0;
- for(int i=0;i<256;i++)
- {
- writefile<<setw(2)<<erasetable[i];
- }
- writefile<<endl;
- }
-
-
-
-
-
-
- int npow(int n)
- {
- int mul = 1;
- for(int i=0;i<n;i++)
- {
- mul *= 2;
- }
- return mul;
- }
-
-
-
-
-
- void Threshold(IplImage *src , int lower , int higher)
- {
- assert(src->nChannels==1);
- for(int h=0;h<src->height;h++)
- for(int w=0;w<src->width;w++)
- {
- if(*(src->imageData +h*src->widthStep+w)>0&&*(src->imageData+h*src->widthStep+w)<87)
- *(src->imageData +h*src->widthStep+w) = 255;
- else
- *(src->imageData +h*src->widthStep+w) = 0;
-
- }
- }
-
-
-
-
-
-
- void scantable(IplImage *src)
- {
- assert(src->nChannels==1);
- int scan[8] = {0};
- for(int h=1;h<(src->height-1);h++)
- {
- for(int w=1;w<(src->width-1);w++)
- {
- int index = 0;
- if(*(src->imageData+(h)*src->widthStep+w)!=0)
- {
- if(*(src->imageData+(h-1)*src->widthStep+w-1)==0)
- scan[0] = 1;
- else
- scan[0] = 0;
- if(*(src->imageData+(h-1)*src->widthStep+w)==0)
- scan[1] = 1;
- else
- scan[1] = 0;
- if(*(src->imageData+(h-1)*src->widthStep+w+1)==0)
- scan[2] = 1;
- else
- scan[2] = 0;
- if(*(src->imageData+(h)*src->widthStep+w-1)==0)
- scan[3] = 1;
- else
- scan[3] = 0;
- if(*(src->imageData+(h)*src->widthStep+w+1)==0)
- scan[4] = 1;
- else
- scan[4] = 0;
- if(*(src->imageData+(h+1)*src->widthStep+w-1)==0)
- scan[5] = 1;
- else
- scan[5] = 0;
- if(*(src->imageData+(h+1)*src->widthStep+w)==0)
- scan[6] = 1;
- else
- scan[6] = 0;
- if(*(src->imageData+(h+1)*src->widthStep+w+1)==0)
- scan[7] = 1;
- else
- scan[7] = 0;
-
-
- for(int i=0;i<8;i++)
- {
- index += scan[i]*npow(i);
- }
-
- if(erasetable[index] == 1)
- {
-
- *(src->imageData+h*src->widthStep+w) = 0;
- }
- }
-
- }
- }
- }
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- save();
- IplImage *img = 0;
- IplImage *gray = 0;
- IplImage *copy_gray = 0;
- int vmin = 0;
- int vmax = 0;
-
- img = cvLoadImage("1.bmp" , 1);
- gray = cvCreateImage(cvGetSize(img) , 8 , 1);
- copy_gray = cvCreateImage(cvGetSize(img) , 8 ,1);
-
- cvCvtColor(img , gray , CV_BGR2GRAY);
- cvSmooth(gray , gray , CV_MEDIAN , 3 , 0 , 0 , 0);
-
- cvNamedWindow("gray" , 1);
- cvNamedWindow("erase" , 1);
-
-
-
- Threshold(gray , vmin , vmax);
- cvCopy(gray , copy_gray);
- scantable(copy_gray);
- cvShowImage("gray" , gray);
- cvShowImage("erase" , copy_gray);
-
- if(cvWaitKey(0)==27)
- {
- cvReleaseImage(&img);
- cvReleaseImage(&gray);
- cvReleaseImage(©_gray);
- }
-
-
- return 0;
- }
原图:
效果图: