資源簡介
將圖像直方圖進(jìn)行平滑,獲得雙峰分布形式,并通過雙峰特性進(jìn)行圖像二值化。opencv3.1.0, vs2015。
代碼片段和文件信息
#include?
#include?
#include?
using?namespace?cv;
using?namespace?std;
#define?HISTGRAM_X_SCALE?2
#define?HISTGRAM_X_SIZE?(256*HISTGRAM_X_SCALE)
#define?HISTGRAM_Y_SIZE?400
void?doSomething(Mat?src);
void?showHistgram(int?*grayLevel?string?windowName);
int?smoothHistgram(int?*oldHist?double?*newHist);
bool?isTwoPolar(double?*hist);
uchar?findLocalMinimum(double?*hist);
#define?MAX_PICTURE_NUMBER?100
string?FILE_SOURCE_PATH?=?“nearBall\\“;
void?main()?{
Mat?src;
int?i?=?0;
string?no;
for?(int?i?=?0;?i?
if?(i?0)
i?=?0;
no?=?to_string(i);
src?=?imread(FILE_SOURCE_PATH?+?no?+?“.jpg“?1);
if?(src.empty())
cout?<
imshow(“原圖“?src);
doSomething(src);
int?keyValue?=?waitKey(0);
switch?(keyValue)?{
case?‘w‘:?case?‘a(chǎn)‘:
i--;
break;
case?‘b‘:
i?=?0;
break;
case?‘q‘:
return;
default:
i++;
break;
}
}
return;
}
void?doSomething(Mat?src)?{
Mat?grayImg;
int?grayLevel[256]?=?{?0?};
if?(src.type()?!=?CV_8UC1)
cvtColor(src?grayImg?CV_BGR2GRAY);
imshow(“灰度圖“?grayImg);
//?統(tǒng)計(jì)灰度分布
for?(int?i?=?0;?i? for?(int?j?=?0;?j? uchar?value?=?src.at(i?j);
grayLevel[value]++;
}
}
showHistgram(grayLevel?“Original“);
double?newHist[256]?=?{?0?};
if?(smoothHistgram(grayLevel?newHist)?!=?-1)?{
uchar?p1?p2;
int?intHist[256]?=?{?0?};
for?(int?i?=?0;?i?256;?++i)
intHist[i]?=?static_cast(newHist[i]);
showHistgram(intHist?“New“);
int?thresh?=?findLocalMinimum(newHist);
Mat?binImg;
threshold(grayImg?binImg?thresh?255?THRESH_BINARY);
imshow(“二值化圖“?binImg);
}
else
;
//?繪制平滑直方圖
}
void?showHistgram(int?*grayLevel?string?windowName?=?“Default“)?{
double?minValue?=?*min_element(grayLevel?grayLevel?+?256);
double?maxValue?=?*max_element(grayLevel?grayLevel?+?256);
//cout?<“Min:?“?<
Mat?d
評論
共有 條評論