标签:style blog class code c java
1 #include <stdlib.h> 2 #include <string.h> 3 4 #include "opencv/cv.h" 5 #include "opencv/highgui.h" 6 #include "opencv/cxcore.h" 7 8 IplImage* ColorSrc ,*ColorSrcCopy; 9 10 //#define SHOW 11 12 //#define LONG int 13 //#define BYTE unsigned char 14 ///////////////////////////////////////////////////////////////////////// 15 //基于索引表的细化细化算法 16 //功能:对图象进行细化 17 //参数:lpDIBBits:代表图象的一维数组 18 // lWidth:图象高度 19 // lHeight:图象宽度 20 // 无返回值 21 bool ThiningDIBSkeleton (unsigned char* lpDIBBits, int lWidth, int lHeight) 22 { 23 //循环变量 24 long i; 25 long j; 26 long lLength; 27 28 unsigned char deletemark[256] = { 29 0,0,0,0,0,0,0,1, 0,0,1,1,0,0,1,1, 30 0,0,0,0,0,0,0,0, 0,0,1,1,1,0,1,1, 31 0,0,0,0,0,0,0,0, 1,0,0,0,1,0,1,1, 32 0,0,0,0,0,0,0,0, 1,0,1,1,1,0,1,1, 33 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 34 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 35 0,0,0,0,0,0,0,0, 1,0,0,0,1,0,1,1, 36 1,0,0,0,0,0,0,0, 1,0,1,1,1,0,1,1, 37 0,0,1,1,0,0,1,1, 0,0,0,1,0,0,1,1, 38 0,0,0,0,0,0,0,0, 0,0,0,1,0,0,1,1, 39 1,1,0,1,0,0,0,1, 0,0,0,0,0,0,0,0, 40 1,1,0,1,0,0,0,1, 1,1,0,0,1,0,0,0, 41 0,1,1,1,0,0,1,1, 0,0,0,1,0,0,1,1, 42 0,0,0,0,0,0,0,0, 0,0,0,0,0,1,1,1, 43 1,1,1,1,0,0,1,1, 1,1,0,0,1,1,0,0, 44 1,1,1,1,0,0,1,1, 1,1,0,0,1,1,0,0 45 };//索引表 46 47 unsigned char p0, p1, p2, p3, p4, p5, p6, p7; 48 unsigned char *pmid, *pmidtemp; 49 unsigned char sum; 50 int changed; 51 bool bStart = true; 52 lLength = lWidth * lHeight; 53 unsigned char *pTemp = (unsigned char *)malloc(sizeof(unsigned char) * lWidth * lHeight); 54 55 // P0 P1 P2 56 // P7 P3 57 // P6 P5 P4 58 59 while(bStart) 60 { 61 bStart = false; 62 changed = 0; 63 64 //首先求边缘点(并行) 65 pmid = (unsigned char *)lpDIBBits + lWidth + 1; 66 memset(pTemp, 0, lLength); 67 pmidtemp = (unsigned char *)pTemp + lWidth + 1; 68 for(i = 1; i < lHeight -1; i++) 69 { 70 for(j = 1; j < lWidth - 1; j++) 71 { 72 if( *pmid == 0) 73 { 74 pmid++; 75 pmidtemp++; 76 continue; 77 } 78 79 p3 = *(pmid + 1); 80 p2 = *(pmid + 1 - lWidth); 81 p1 = *(pmid - lWidth); 82 p0 = *(pmid - lWidth -1); 83 p7 = *(pmid - 1); 84 p6 = *(pmid + lWidth - 1); 85 p5 = *(pmid + lWidth); 86 p4 = *(pmid + lWidth + 1); 87 88 sum = p0 & p1 & p2 & p3 & p4 & p5 & p6 & p7; 89 if(sum == 0) 90 { 91 *pmidtemp = 1; 92 93 #ifdef SHOW 94 cvSet2D(ColorSrc,i,j,cvScalar(0,0,255)); 95 #endif 96 } 97 98 pmid++; 99 pmidtemp++; 100 } 101 pmid++; 102 pmid++; 103 pmidtemp++; 104 pmidtemp++; 105 } 106 #ifdef SHOW 107 cvNamedWindow("color"); 108 cvShowImage("color",ColorSrc); 109 cvWaitKey(0); 110 #endif 111 112 //现在开始串行删除 113 pmid = (unsigned char *)lpDIBBits + lWidth + 1; 114 pmidtemp = (unsigned char *)pTemp + lWidth + 1; 115 116 for(i = 1; i < lHeight -1; i++) 117 { 118 for(j = 1; j < lWidth - 1; j++) 119 { 120 if( *pmidtemp == 0) 121 { 122 pmid++; 123 pmidtemp++; 124 continue; 125 } 126 127 p3 = *(pmid + 1); 128 p2 = *(pmid + 1 - lWidth); 129 p1 = *(pmid - lWidth); 130 p0 = *(pmid - lWidth -1); 131 p7 = *(pmid - 1); 132 p6 = *(pmid + lWidth - 1); 133 p5 = *(pmid + lWidth); 134 p4 = *(pmid + lWidth + 1); 135 136 p1 *= 2; 137 p2 *= 4; 138 p3 *= 8; 139 p4 *= 16; 140 p5 *= 32; 141 p6 *= 64; 142 p7 *= 128; 143 144 sum = p0 | p1 | p2 | p3 | p4 | p5 | p6 | p7; 145 // sum = p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7; 146 if(deletemark[sum] == 1) 147 { 148 *pmid = 0; 149 bStart = true; 150 151 #ifdef SHOW 152 cvSet2D(ColorSrc,i,j,cvScalar(0,0,0)); 153 154 cvNamedWindow("delcolor"); 155 cvShowImage("delcolor",ColorSrc); 156 cvWaitKey(2); 157 #endif 158 } 159 160 pmid++; 161 pmidtemp++; 162 } 163 164 pmid++; 165 pmid++; 166 pmidtemp++; 167 pmidtemp++; 168 } 169 170 #ifdef SHOW 171 printf("过了一圈\n"); 172 #endif 173 174 175 } 176 177 return true; 178 } 179 180 181 182 int main(int argc, char* argv[]) 183 { 184 185 186 IplImage* src = cvLoadImage("t.png",0); 187 cvThreshold(src,src,100,255,CV_THRESH_BINARY); 188 unsigned char* imagedata ; 189 190 ColorSrc = cvLoadImage("t.png",1); 191 192 cvNamedWindow("s"); 193 cvShowImage("s" , src); 194 195 196 FILE* fp ; 197 198 #ifdef WRITEIMAGE 199 fp = fopen("data255.txt","rt+"); 200 #endif 201 202 imagedata = (unsigned char*)malloc(sizeof(char)*src->width*src->height); 203 204 int x , y; 205 for(y=0;y<src->height;y++) 206 { 207 unsigned char* ptr = (unsigned char*)(src->imageData + y*src->widthStep); 208 for(x=0;x<src->width;x++) 209 { 210 imagedata[y*src->width+x] = ptr[x] > 0 ? 1 : 0; 211 212 #ifdef WRITEIMAGE 213 if(ptr[x] > 0) 214 fprintf(fp,"1"); 215 else 216 { 217 fprintf(fp,"0"); 218 } 219 #endif 220 } 221 222 #ifdef WRITEIMAGE 223 fprintf(fp,"\n"); 224 #endif 225 } 226 227 #ifdef WRITEIMAGE 228 fclose(fp); 229 #endif 230 231 #ifdef WRITERESULT 232 fp = fopen("result.txt","rt+"); 233 #endif 234 235 ThiningDIBSkeleton(imagedata,src->width,src->height); 236 237 for(y=0;y<src->height;y++) 238 { 239 unsigned char* ptr = (unsigned char*)(src->imageData + y*src->widthStep); 240 for(x=0;x<src->width;x++) 241 { 242 ptr[x] = imagedata[y*src->width + x]>0? 255 : 0; 243 244 #ifdef WRITERESULT 245 if(ptr[x] > 0) 246 fprintf(fp,"1"); 247 else 248 { 249 fprintf(fp,"0"); 250 } 251 #endif 252 } 253 254 #ifdef WRITERESULT 255 fprintf(fp,"\n"); 256 #endif 257 } 258 259 #ifdef WRITERESULT 260 fclose(fp); 261 #endif 262 263 cvNamedWindow("src"); 264 cvShowImage("src" , src); 265 cvWaitKey(0); 266 267 cvReleaseImage(&src); 268 cvReleaseImage(&ColorSrc); 269 free(imagedata); 270 271 return 0; 272 }
thinning&&opencv,布布扣,bubuko.com
标签:style blog class code c java
原文地址:http://www.cnblogs.com/fandaojian/p/3725785.html