52 int ScanLine_SeedFillingAlgo(IplImage *src,IplImage *dst,int MinCutNumb) 53 { 54 int i, j, k; 55 for ( i = 0; i < 3; i++ ) //上下两行 56 { 57 unsigned char * t_pPos = (unsigned char *) ( &src->imageData[ i * src->widthStep ] ); 58 59 for ( j = 0; j < src->widthStep; j++ ) 60 { 61 * t_pPos = (unsigned char)0; 62 t_pPos++; 63 } 64 } 65 66 for ( i = ( src->height - 3 ); i < src->height; i++ ) //上下两行 67 { 68 unsigned char * t_pPos = (unsigned char *) ( &src->imageData[ i * src->widthStep ] ); 69 70 for ( j = 0; j < src->widthStep; j++ ) 71 { 72 * t_pPos = (unsigned char)0; 73 t_pPos++; 74 } 75 } 76 77 for ( i = 0; i < src->height; i++ ) //左右两边 78 { 79 unsigned char * t_pPos = (unsigned char *) ( &src->imageData[ i * src->widthStep ] ); 80 81 for ( j = 0; j < 3; j++ ) 82 { 83 * t_pPos = (unsigned char)0; 84 t_pPos++; 85 } 86 87 t_pPos = (unsigned char *) ( &src->imageData[ i * src->widthStep + src->widthStep - 3 ] ); 88 89 for ( j = ( src->widthStep - 3 ); j < src->widthStep; j++ ) 90 { 91 * t_pPos = (unsigned char)0; 92 t_pPos++; 93 } 94 } 95 int width = src->width; 96 int height = src->height; 97 int targetSumNumb=0; 98 int area; 99 CvPoint direction_4[]={{-1, 0}, {0, 1}, {1, 0}, {0, -1}};//上右下左 100 //CvPoint direction_8[] = { {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1} };//顺时针 101 int n_Count=sizeof(direction_4)/sizeof(CvPoint);//遍历方向个数 102 std::list<CvPoint> stk;//stl栈 103 std::list<CvPoint> lst;//stl链表 104 cvZero(dst); 105 //IplImage *tempimage=cvCreateImage(cvGetSize(src),8,1);//创建一个临时数据,保存源图像数据到目标过度数据 106 int t_i;//每次种子的位置 107 int t_j; 108 //cvZero(tempimage);//临时数据初始化,清0 109 for (int i=1;i<height-1;i++) 110 { 111 for (int j=1;j<width-1;j++) 112 { 113 //int s=clock(); 114 115 // 116 if (src->imageData[i*width+j]) 117 { 118 targetSumNumb++; 119 stk.push_back(cvPoint(i,j));//栈换成链表 120 lst.push_back(cvPoint(i,j)); 121 src->imageData[i*width+j]=0;//二值图像 122 //tempimage->imageData[i*width+j]=255; 123 area=1; 124 while (!stk.empty()) 125 { 126 CvPoint seed=stk.back();//弹出头部 127 stk.pop_back(); 128 t_i=seed.x; 129 t_j=seed.y; 130 if (t_i<=0||t_i>=height||t_j<=0||t_j>=width) 131 continue; 132 for (int ii=0;ii<n_Count;ii++)//扫描各个方向 133 { 134 if (src->imageData[(t_i+direction_4[ii].x)*width+t_j+direction_4[ii].y]) 135 { 136 area++; 137 stk.push_back(cvPoint(t_i+direction_4[ii].x,t_j+direction_4[ii].y)); 138 lst.push_back(cvPoint(t_i+direction_4[ii].x,t_j+direction_4[ii].y)); 139 src->imageData[(t_i+direction_4[ii].x)*width+t_j+direction_4[ii].y]=0;//二值图像 140 //tempimage->imageData[(t_i+direction_4[ii].x)*width+t_j+direction_4[ii].y]=255; 141 } 142 } 143 } 144 //int e=clock(); 145 //std::cout<<e-s; 146 if (area>MinCutNumb) 147 { 148 //cvOr(dst,tempimage,dst); 149 while (!lst.empty()) 150 { 151 CvPoint tmpPt=lst.front(); 152 lst.pop_front(); 153 dst->imageData[tmpPt.x*width+tmpPt.y]=255; 154 } 155 } 156 else 157 { 158 //std::list<CvPoint>().swap(lst); 159 //while (!lst.empty()) lst.pop_back(); 160 //lst.resize(0); 161 //lst. 162 lst.clear(); 163 } 164 165 }//判断是否入栈 166 //CvPoint 167 168 } 169 } 170 //cvReleaseImage(&tempimage); 171 return targetSumNumb; 172 } |
图片处理效果: