void Get_fg_mask_New(int *bg_color, Mat &samples, Parameters Paras, Mat &k_img, vector<pair<int, int>> &UnzeroValue, bool flag)
{
float csaturation = 0.0;
float cvalue = 0.0;
int kmax = Quick_search(bg_color, CHANNELS);
int kmin = Quick_search(bg_color, CHANNELS, false);
int cdelta = bg_color[kmax] - bg_color[kmin];
csaturation = bg_color[kmax] == 0 ? 0 : ((float)cdelta) / ((float)bg_color[kmax]);
cvalue = ((float)bg_color[kmax]) / 255.0;
Mat SaturationMat(samples.rows, samples.cols, CV_32FC1,Scalar::all(csaturation));
Mat CvalueMat(samples.rows, samples.cols, CV_32FC1, Scalar::all(cvalue));
Mat perPixelS = Mat::zeros(samples.rows, samples.cols, CV_32FC1);
Mat perPixelC = Mat::zeros(samples.rows, samples.cols, CV_32FC1);
Mat perPixelSdiff = Mat::zeros(samples.rows, samples.cols, CV_32FC1);
Mat perPixelCdiff = Mat::zeros(samples.rows, samples.cols, CV_32FC1);
Mat SfgMat = Mat::zeros(samples.rows, samples.cols, CV_8UC1);
Mat CfgMat = Mat::zeros(samples.rows, samples.cols, CV_8UC1);
Mat FgMask = Mat::zeros(samples.rows, samples.cols, CV_8UC1);
Rgb_to_sv_New(samples, perPixelS, perPixelC);
perPixelSdiff = abs(perPixelS - SaturationMat);
perPixelCdiff = abs(perPixelC - CvalueMat);
SfgMat = perPixelSdiff >= Paras.sat_threshold;
CfgMat = perPixelCdiff >= Paras.value_threshold;
FgMask = SfgMat | CfgMat;
Mat CH3_fgMat = Mat::zeros(samples.rows, samples.cols, CV_8UC3);
vector<Mat>Channels;
for (int i = 0;i < CHANNELS;i++) //单通道转三通道图像;
Channels.push_back(FgMask);
merge(Channels, CH3_fgMat);
k_img = CH3_fgMat & samples;
for (int i = 0;i < k_img.rows;i++)
{
uchar* p = k_img.ptr<uchar>(i);
for (int j = 0;j < k_img.cols*CHANNELS;j=j+ CHANNELS)
{
if ((max(p[j], max(p[j + 1], p[j + 2]))) == 0)
continue;
if (flag)
{
pair<int, int>temp;
temp.first = i;
temp.second = j/ CHANNELS;
UnzeroValue.push_back(temp);
}
}
}
}
代码为本人原创,转载请注明出处。