The threshold method in the search image binary method has the following:
Pick one part of the study.
A global threshold method based on point
1. Histogram Shuangfeng method (need to have a clear bottom)
2. Maximum Inter-class variance method (the size of the target and background is less than an hour)
3. Entropy method
4. Minimum error threshold value
5. Moment Retention method
6. Fuzzy Set method
The global threshold method based on region
1. Two-D entropy threshold segmentation method
2. Simple Statistical method
3. Histogram Change method
4. Relaxation method
Third, local threshold method and multi-threshold method
1. Local threshold (dynamic threshold value)
2. Threshold value Difference method
3. Watermark Threshold method
4. Wavelet based multi-threshold method
5. Recursive multi-threshold method based on boundary points
6. Equalization contrast degree recursive multiple threshold method
1.1 Histogram Shuangfeng method
Note: This method is suitable for images with two peaks in the histogram, if the histogram curve is flat or only one peak image is not suitable.
Implementation method Reference http://www.cnblogs.com/Imageshop/p/3307308.html
Step: A. The color space is transferred from RGB to HSV, and the V channel histogram is computed.
B. Histogram smoothing. The calculation method is (i-1), I, (i+1) three numbers to calculate the average value as the number of new values, iterations 1000 times, if 1000 or no Shuangfeng to explain the iterative failure is not suitable for the Shuangfeng method of binary value.
C. After the Shuangfeng figure is obtained, the bottom is calculated as the threshold value.
The test diagram is as follows. The background color is single, the foreground color is single, background and foreground color differ greatly.
Code:
Declaration of the Hist_t.h class
------------histogram threshold Segmentation----------
//Histogram based multiple threshold segmentation algorithms
#include <opencv2\opencv.hpp>
#include < opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\core\ core.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace CV;
Class hist_t
{public
:
hist_t ();
Mat hist_calculation (Mat in); Output hist
Mat drawhist (Mat _hist, string f); The output is painted histogram histimg
void Setfilename (string f); Incoming image name
int getminthreshold (Mat _hist); The histogram is smoothed to Shuangfeng histogram, and the gray level
Mat inputimg is obtained. Enter the original image
string filename; Image name
Mat filter_hist; After filtering the histogram
//mat histimg; The histogram curve is used to display the
bool showstep;
BOOL SaveImage;
Private:
bool Isdimodal (Mat _hist); Determine if the histogram is Shuangfeng
};
The definition function action of the Hist_t.cpp class is explained in the note
---------Histogram based threshold segmentation------#include "hist_t.h" hist_t::hist_t () {showstep = false;
SaveImage = false;
} void Hist_t::setfilename (string s) {Filename=s}
--compute histogram-//input single channel image, output statistic histogram hist Mat hist_t::hist_calculation (Mat in) {//output Mat;
if (In.channels () ==3) {Cvtcolor (in, in, Cv_bgr2gray);
}//establish the number of bins int histsize = 256;
Set the range float range[] = {0, 256};
Const float* Histrange = {range}; BOOL uniform = true;
bool accumulate = false; Calchist (&in, 1,//the number of source arrays 0,//the channel (dim) To is measured Mat ( ),//a mask to is used on the source array hist,//the Mat object where the Historgam would be stored (Floa T type) 1,//the histogram dimensionality &histsize,//the number of bins per each used dimension &am
P;histrange,//the range of values to is measured per each dimension uniform,//bins have the same size; Accumulate); Clear the histograms in the beginning//cout<<_hist.rows<< "T" <<_hist.cols<< "T" <<_ Hist.channels () <<endl;
256*1*1 return hist; }//---Draw the Historgram Mat hist_t::d rawhist (Mat _hist, string s) {int hist_w = 512; Displays the histogram of the wide cols int hist_h = 400; High rows int histsize = 256 showing the histogram; Establish the number of bins int bin_w = Cvround (double) hist_w/histsize);
How many gray levels per bin (here =2)//cout<< "bin_w=" <<bin_w<<endl;
Mat _histimage = Mat::zeros (Hist_h, Hist_w, CV_8UC3); Normalize the result to [0, histimage.rows] Histogram height 0~400 normalize (_hist, _hist, 0, _histimage.rows, Norm_minmax,-1, Ma
T ()); Draw for (int i=1; iMain.cpp Main Function entry
---------threshold segmentation based on histogram------//Histogram based multiple threshold segmentation algorithms #include <opencv2\opencv.hpp> #include <opencv2\highgui\ highgui.hpp> #include <opencv2\imgproc\imgproc.hpp> #include <opencv2\core\core.hpp> #include <
iostream> #include <stdio.h> #include "hist_t.h" using namespace CV;
using namespace Std;
int main () {//load image Mat src = imread ("1.bmp", 1); Mat img1 = Src.clone ();
Avoid modification of original artwork if (!src.data) {return-1} in Process
Color space conversion Cvtcolor (IMG1, IMG1, CV_BGR2HSV);
Cvtcolor (IMG1, IMG1, Cv_bgr2gray);
Separate the image in 3 places () vector<mat> hsv_planes;
Split (IMG1, Hsv_planes);
Imwrite ("Hsv_h.bmp", Hsv_planes[0]);
Imwrite ("Hsv_s.bmp", hsv_planes[1]);
Imwrite ("Hsv_v.bmp", hsv_planes[2]);
The statistical histogram (hist) is obtained and the histogram curve (histimg) is preserved hist_t hist_t.
Hist_t.showstep = true;
Hist_t.saveimage = true; Mat Hist_h, hist_s, hist_v; Statistical histogram Mat Histimg_h, histimg_s, Histimg_v; Histogram graph for display string SN1 = "D:/vc2010-project/allthresholD/savedimg/histimg_h.bmp ";
String SN2 = "D:/vc2010-project/allthreshold/savedimg/histimg_s.bmp";
String sn3 = "D:/vc2010-project/allthreshold/savedimg/histimg_v.bmp";
Hist_h = Hist_t.hist_calculation (Hsv_planes[0]);
hist_s = Hist_t.hist_calculation (hsv_planes[1]);
Hist_v = Hist_t.hist_calculation (hsv_planes[2]);
Histimg_h = Hist_t.drawhist (Hist_h, SN1);
histimg_s = Hist_t.drawhist (hist_s, SN2); Histimg_v = Hist_t.drawhist (Hist_v, SN3);
V Channel histogram//found Hist_v histogram is relatively good//smooth histogram, get Shuangfeng histogram, find out the bottom gray level as threshold int minTh;
MinTh = Hist_t.getminthreshold (hist_v); cout<< "Minth=" <<minTh<<endl; MinTh = Mat f_hist = hist_t.filter_hist; Hist_t.filter_hist//cout<<f_hist.rows<< "\ T" is only obtained when the Getminthreshold () function is executed <<f_hist.cols << "T" <<f_hist.channels () <<endl;
Tests whether the f_hist is an empty string sn4 = "D:/vc2010-project/allthreshold/savedimg/histimg_v_filter.bmp";
Mat histimg_v_filter = hist_t.drawhist (f_hist, SN4); Mat of Binary Value Bit_h = Mat::zeros (Img1.size (), CV_8UC1);
H Channel binary threshold (hsv_planes[2], bit_h, minTh, 255, cv_thresh_binary);
String sn5 = "D:/vc2010-project/allthreshold/savedimg/bit_h.bmp";
Imwrite (Sn5, Bit_h);
for (;;)
{int C;
c = Cvwaitkey (10);
if ((char) c = =) break;
return 0; }Experimental results:
1. First look at the H, S, v three channels of gray-scale map and corresponding histogram
Only the V channel is good to understand, V-Channel Histogram smoothing effect chart (before and after smoothing)
The threshold value minth=66 is obtained. Second-valued Results:
2. If you do not convert color space, the effect of direct gray. The following are gray-level histogram, smooth histogram and two-value effect.
3. If you switch to Lab color space.
It is found that L channel Shuangfeng characteristics are relatively obvious, a channel (or B channel) foreground and background gray value difference becomes smaller, it may be necessary to combine (A,B) channel to explain color, the single channel is not meaningful.
L Channel histogram minth=59, smooth effect chart and two-value effect chart:
Summarize:
1. In view of this test image, the histogram Shuangfeng method has a good effect.
2. Color space conversion has little effect on this test diagram.