资源简介
大津法及最优迭代求分割阈值(opencv2),采用了该文献中的代码思想并作了修改。大津法OSTU:http://blog.csdn.net/guoyk1990/article/details/7606032
迭代的最优阈值选择:http://blog.csdn.net/lichengyu/article/details/4775974 ,这篇文中附的代码有问题,做了修改。
代码片段和文件信息
#include “stdafx.h“
#include “iostream“
#include “opencv2/opencv.hpp“
using namespace cv;
using namespace std;
/// 函数迭代法求阈值输入灰度图像,返回最优阈值
int LQIterationThreshold(Mat& src)
{
CV_Assert(1 == src.channels());
int width = src.cols;
int height = src.rows;
int temp = 0 Threshold = 0;//Threshold是阈值
int Ub = 0 Uo = 0;//Ub是背景的平均值,Uo是目标的平均值
int Number_black = 4 Number_object = 0;//nUb是背景区域像素的数目nUb是背景区域像素初始为4,nUo是目标区域像素的数目
Number_object = width * height - Number_black;
Ub = (int)(src.at(00) + src.at(0width-1) + src.at(height-10) +
src.at(height-1width-1)) / Number_black;
Uo = Uo - Ub * Number_black;
Uo /= Number_object;
Threshold = (Ub + Uo) / 2;
while(temp != Threshold)
{
Ub = 0;
Uo = 0;
Number_black = 0;
Number_object = 0;
for (int y=0;y {
uchar* srcdata = src.ptr(y);
for (int x=0;x {
if((int)srcdata[x] < Threshold)//if f(ij) {
Ub += (int)srcdata[x];
Number_black++;
}
else
{
Uo += (int)srcdata[x];
Number_object++;
}
}
}
Ub /= (Number_black + 1);
Uo /= (Number_object + 1);
temp = Threshold;
Threshold = (Ub + Uo) / 2;
}
return Threshold;
}
/// 最优迭代法求阈值,输入灰度图,输出二值化图像
void ThreIteration(Mat &src Mat &dst)
{
CV_Assert(1 == src.channels());
int width = src.cols;
int height = src.rows;
dst.create(src.size() CV_8UC1);
int temp = 0 Threshold = 0;//Threshold是阈值
int Ub = 0 Uo = 0;//Ub是背景的平均值,Uo是目标的平均值
int Number_black = 4 Number_object = 0;//nUb是背景区域像素的数目nUb是背景区域像素初始为4,nUo是目标区域像素的数目
//Number_black = 4; //
Number_object = width * height - Number_black;
Ub = (int)(src.at(00) + src.at(0width-1) + src.at(height-10) +
src
- 上一篇:C++版贪吃蛇游戏源代码
- 下一篇:灰度共生矩阵vs2010+opencv2
评论
共有 条评论