the first attempt to write a blog, hoping to continue ...
Recently in the infrared small target detection, using a maximum entropy segmentation, OK, the following introduction.
maximum entropy partition method
Now the entropy algorithm mainly uses the P-entropy algorithm, KSW entropy algorithm, JM entropy algorithm, the classical KSW entropy algorithm is used to introduce its principle and calculation process.
KSW entropy Algorithm
Set the segmentation threshold to set the split threshold to T,
T is the grayscale distribution of {0,1,2,...t}, B is {t+1,t+2,... The gray distribution of L-1}, the probability distribution is:
In-style
The entropy associated with these two probability densities is:
Defines the and of the functions φ (t) as H (t) and H (B), then
It is the best threshold to find the gray level t when φ (t) is the largest.
The method of gray histogram to see http://blog.csdn.net/xiaowei_cqu/article/details/7600666
The code is as follows:
#include <cv.h> #include <opencv2/opencv.hpp> #include <opencv2/legacy/legacy.hpp> using namespace
Cv Float calc_entropy (cvhistogram *hist, int begin, int end) {float total = 0;
Total probability//Get the General pi for (int i = begin; I < end; i++) {Total + = cvqueryhistvalue_1d (hist,i); float entropy = 0;
Entropy for (int i = begin; I < end; i++) {Float probability = cvqueryhistvalue_1d (hist, i);
if (probability = = 0) continue;
Probability/= total;
Entropy + +-probability*log (probability);
return entropy;
int ksw_entropy (Iplimage *img) {assert (img!= NULL);
ASSERT (img->depth = 8);
ASSERT (img->nchannels = 1);
Float Range[2] = {0,255};
float *ranges[1] = {&range[0]};
int sizes = 256;
Create histograms Cvhistogram *hist = cvcreatehist (1, &sizes, Cv_hist_array, ranges, 1);
Histogram calculation cvcalchist (&img, hist, 0, 0);
Histogram normalized cvnormalizehist (hist, 1.0);
int threshold = 0;
float max_entropy = 0;
Cycle calculation, get bigger entropy and split threshold value for (int i = 0; I < sizes i++) {Float entropy = calc_entropy (hist, 0, I) + calc_entropy (hist, i+1, sizes);
if (Entropy > max_entropy) {max_entropy = entropy;
threshold = i;
} return threshold;
int main (int argc, char **argv) {iplimage *img = cvloadimage ("1.bmp", Cv_load_image_grayscale);
Iplimage *reimg = cvcreateimage (Cvgetsize (IMG), ipl_depth_8u, 1);
int threshold = Ksw_entropy (IMG);
Cvthreshold (IMG, reimg, Threshold, 255, cv_thresh_binary);
Cvnamedwindow ("img");
Cvshowimage ("IMG", IMG);
Cvnamedwindow ("reimg");
Cvshowimage ("Reimg", reimg);
Cvwaitkey (0);
return 0; }
In learning to get the gray histogram, there is a function that makes me puzzled for a long time, cvnormalizehist, gray-scale normalization, just at the beginning is not understand what this gray normalization refers to,
The functions given by OPENCV are explained as follows:
What is called the sum of the bins becomes equal to factor, OK, then understand, in fact, the meaning of the histogram of the value added and equal to the second parameter, so if set to 1, the ordinate value is the probability of the gray level.
Here is the experimental picture:
Fig. 1 Original Figure 2 maximum entropy threshold segmentation image
The effect is very unsatisfactory, so you need to join the tophat before doing threshold segmentation, the following blog will talk about.
Complete the full text.