问题描述
- (Matlab)基于量子粒子群的二维大津图像分割
-
请教编写基于qpso算法,适应度函数为最大类间方差的图像分割算法,有懂行的请加qq
2893541647,可以交流下,加我时请说csdn
解决方案
类似下面?
void ImageBinarization(IplImage *src)
85.{ /*对灰度图像二值化,自适应门限threshold*/
86. int i,j,width,height,step,chanel,threshold;
87. /*size是图像尺寸,svg是灰度直方图均值,va是方差*/
88. float size,avg,va,maxVa,p,a,s;
89. unsigned char *dataSrc;
90. float histogram[256];
91.
92. width = src->width;
93. height = src->height;
94. dataSrc = (unsigned char *)src->imageData;
95. step = src->widthStep/sizeof(char);
96. chanel = src->nChannels;
97. /*计算直方图并归一化histogram*/
98. for(i=0; i<256; i++)
99. histogram[i] = 0;
100. for(i=0; i<height; i++)
101. for(j=0; j<width*chanel; j++)
102. {
103. histogram[dataSrc[i*step+j]-'0'+48]++;
104. }
105. size = width * height;
106. for(i=0; i<256; i++)
107. histogram[i] /=size;
108. /*计算灰度直方图中值和方差*/
109. avg = 0;
110. for(i=0; i<256; i++)
111. avg += i*histogram[i];
112. va = 0;
113. for(i=0; i<256; i++)
114. va += fabs(i*i*histogram[i]-avg*avg);
115. /*利用加权最大方差求门限*/
116. threshold = 20;
117. maxVa = 0;
118. p = a = s = 0;
119. for(i=0; i<256; i++)
120. {
121. p += histogram[i];
122. a += i*histogram[i];
123. s = (avg*p-a)*(avg*p-a)/p/(1-p);
124. if(s > maxVa)
125. {
126. threshold = i;
127. maxVa = s;
128. }
129. }
130. /*二值化*/
131. for(i=0; i<height; i++)
132. for(j=0; j<width*chanel; j++)
133. {
134. if(dataSrc[i*step+j] > threshold)
135. dataSrc[i*step+j] = 255;
136. else
137. dataSrc[i*step+j] = 0;
138. }
139.}
时间: 2024-10-15 02:21:43