Exemplo n.º 1
0
void CmSaliencyRC::SmoothSaliency(CMat &colorNum1i, Mat &sal1f, float delta, const vector<vector<CostfIdx>> &similar)
{
	if (sal1f.cols < 2)
		return;
	CV_Assert(sal1f.rows == 1 && sal1f.type() == CV_32FC1);
	CV_Assert(colorNum1i.size() == sal1f.size() && colorNum1i.type() == CV_32SC1);

	int binN = sal1f.cols;
	Mat newSal1d= Mat::zeros(1, binN, CV_64FC1);
	float *sal = (float*)(sal1f.data);
	double *newSal = (double*)(newSal1d.data);
	int *pW = (int*)(colorNum1i.data);

	// Distance based smooth
	int n = max(cvRound(binN * delta), 2);
	vecD dist(n, 0), val(n), w(n);
	for (int i = 0; i < binN; i++){
		const vector<CostfIdx> &similari = similar[i];
		double totalDist = 0, totoalWeight = 0;
		for (int j = 0; j < n; j++){
			int ithIdx =similari[j].second;
			dist[j] = similari[j].first;
			val[j] = sal[ithIdx];
			w[j] = pW[ithIdx];
			totalDist += dist[j];
			totoalWeight += w[j];
		}
		double valCrnt = 0;
		for (int j = 0; j < n; j++)
			valCrnt += val[j] * (totalDist - dist[j]) * w[j];

		newSal[i] =  valCrnt / (totalDist * totoalWeight);
	}
	normalize(newSal1d, sal1f, 0, 1, NORM_MINMAX, CV_32FC1);
}
Exemplo n.º 2
0
void CmShow::Pseudocolor(CMat& matfd1, CStr& title)
{
    Mat hsvMat[3], hsvM;
    matfd1.convertTo(hsvMat[0], CV_32FC1, -240, 240);
    hsvMat[1] = hsvMat[2] = Mat::ones(matfd1.size(), CV_32F);
    merge(hsvMat, 3, hsvM);
    cvtColor(hsvM, hsvM, CV_HSV2BGR);
    SaveShow(hsvM, title);
}
Exemplo n.º 3
0
void BenchMarkLatex::printMatTransport(CMat &mat1di, CStr texFile, const char* rowDes[], bool *descendOrder)
{
	FILE* f = fopen(_S(texFile), "w");
	if (f == NULL){
		printf("Can't open file %s\n", _S(texFile));
		return;
	}
	CV_Assert(mat1di.cols == _numMethod);
	const int MAX_COL = min(20, _numMethod);
	const int NUM_BLOCK = (_numMethod + MAX_COL - 1) / MAX_COL;
	string strAlign = "|l||";
	for (int i = 0; i < MAX_COL; i++)
		strAlign += "c|";	
	const char* rankCommand[3] = {"\\first", "\\second", "\\third"};

	Mat mat1d = mat1di.t();
	mat1d.convertTo(mat1d, CV_64F);
	Mat rnk1i = getRankIdx(mat1d, true).t();
	Mat rnkInv = getRankIdx(mat1d, false).t(); 
	for (int i = 0; i < mat1di.rows; i++)
		if (!descendOrder[i])	
			rnkInv.row(i).copyTo(rnk1i.row(i)); // Revert ordering for row i
	for (int b = 0; b < NUM_BLOCK; b++){
		fprintf(f, "\\begin{tabular}{%s} \\hline\n\tMethod ", _S(strAlign));
		for (int i = 0; i < MAX_COL; i++){
			int colIdx = i + b * MAX_COL;
			fprintf(f, "& %4s", colIdx < _numMethod ?  _S(_methodNames[colIdx]) : "");
		}
		fprintf(f, "\\\\\\hline\n");
		for (int i = 0; i < mat1di.rows; i++){
			fprintf(f, "\t%-5s ", rowDes[i]);
			for (int j = 0; j < MAX_COL; j++){
				int colIdx = j + b * MAX_COL;
				if (colIdx >= _numMethod){
					fprintf(f, "&      ");
					continue;
				}
				int idx = rnk1i.at<int>(i, colIdx);
				if (mat1di.type() == CV_32S){
					if (idx < 3)
						fprintf(f, "& %s{%d} ", rankCommand[idx], mat1di.at<int>(i, colIdx));
					else
						fprintf(f, "& %d ", mat1di.at<int>(i, colIdx));
				}
				else{// CV_64F
					if (idx < 3)
						fprintf(f, "& %s{%5.3f} ", rankCommand[idx], mat1di.at<double>(i, colIdx));
					else
						fprintf(f, "& %5.3f ", mat1di.at<double>(i, colIdx));
				}
			}
			fprintf(f, "\\\\\n");
		}
		fprintf(f, "\\hline\n\\end{tabular}\n");
	}
	fclose(f);
}
Exemplo n.º 4
0
void CmSaliencyRC::SmoothByHist(CMat &img3f, Mat &sal1f, float delta)
{
	//imshow("Before", sal1f); imshow("Src", img3f);

	// Quantize colors
	CV_Assert(img3f.size() == sal1f.size() && img3f.type() == CV_32FC3 && sal1f.type() == CV_32FC1);
	Mat idx1i, binColor3f, colorNums1i;
	int binN = Quantize(img3f, idx1i, binColor3f, colorNums1i);
	//CmShow::HistBins(binColor3f, colorNums1i, "Frequency");
	
	// Get initial color saliency
	Mat _colorSal =  Mat::zeros(1, binN, CV_64FC1);
	int rows = img3f.rows, cols = img3f.cols;{
		double* colorSal = (double*)_colorSal.data;
		if (img3f.isContinuous() && sal1f.isContinuous())
			cols *= img3f.rows, rows = 1;
		for (int y = 0; y < rows; y++){
			const int* idx = idx1i.ptr<int>(y);
			const float* initialS = sal1f.ptr<float>(y);
			for (int x = 0; x < cols; x++)
				colorSal[idx[x]] += initialS[x];
		}
		const int *colorNum = (int*)(colorNums1i.data);
		for (int i = 0; i < binN; i++)
			colorSal[i] /= colorNum[i];
		normalize(_colorSal, _colorSal, 0, 1, NORM_MINMAX, CV_32F);
	}
	// Find similar colors & Smooth saliency value for color bins
	vector<vector<CostfIdx>> similar(binN); // Similar color: how similar and their index
	Vec3f* color = (Vec3f*)(binColor3f.data);
	cvtColor(binColor3f, binColor3f, CV_BGR2Lab);
	for (int i = 0; i < binN; i++){
		vector<CostfIdx> &similari = similar[i];
		similari.push_back(make_pair(0.f, i));
		for (int j = 0; j < binN; j++)
			if (i != j)
				similari.push_back(make_pair(vecDist<float, 3>(color[i], color[j]), j));
		sort(similari.begin(), similari.end());
	}
	cvtColor(binColor3f, binColor3f, CV_Lab2BGR);
	//CmShow::HistBins(binColor3f, _colorSal, "BeforeSmooth", true);
	SmoothSaliency(colorNums1i, _colorSal, delta, similar);
	//CmShow::HistBins(binColor3f, _colorSal, "AfterSmooth", true);

	// Reassign pixel saliency values
	float* colorSal = (float*)(_colorSal.data);
	for (int y = 0; y < rows; y++){
		const int* idx = idx1i.ptr<int>(y);
		float* resSal = sal1f.ptr<float>(y);
		for (int x = 0; x < cols; x++)
			resSal[x] = colorSal[idx[x]];
	}
	//imshow("After", sal1f);
	//waitKey(0);
}
Exemplo n.º 5
0
void Objectness::meanStdDev(CMat &data1f, Mat &mean1f, Mat &stdDev1f)
{
    const int DIM = data1f.cols, NUM = data1f.rows;
    mean1f = Mat::zeros(1, DIM, CV_32F), stdDev1f = Mat::zeros(1, DIM, CV_32F);
    for (int i = 0; i < NUM; i++)
        mean1f += data1f.row(i);
    mean1f /= NUM;
    for (int i = 0; i < NUM; i++) {
        Mat tmp;
        pow(data1f.row(i) - mean1f, 2, tmp);
        stdDev1f += tmp;
    }
    pow(stdDev1f/NUM, 0.5, stdDev1f);
}
int main(int argc, char **argv){

    DEFAULT Default;

    for(int i = 1 ; i < argc ; i++){
        if(string(argv[i]) == string("-i")) {
            Default.INPUT() = string(argv[i+1]);
            ++i;
        } else if (string(argv[i]) == string("-o")) {
            Default.OUTPUT() = string(argv[i+1]);
            ++i;
        } else if (string(argv[i]) == string("-mo")) {
            Default.MASK_OUTPUT() = string(argv[i+1]);
            ++i;
        } else if (string(argv[i]) == string("-l")) {
            Default.LONELY_START() = string2int(string(argv[i+1]));
            Default.LONELY_END()   = string2int(string(argv[i+2]));
            Default.LONELY_TIMES() = string2int(string(argv[i+3]));
            i+=3;
        } else if (string(argv[i]) == string("-bw")) {
            Default.BLACK_WHITE() = string2int(string(argv[i+1]));
            ++i;
        } else if (string(argv[i]) == string("-m")) {
            Default.MARGIN() = string2int(string(argv[i+1]));
            ++i;
        } else if (string(argv[i]) == string("-s")) { 
            Default.TOTAL_SOURCES() = string2int(string(argv[i+1]));
            ++i;
            Default.source.pop_back();
            for(int a=0;a<Default.TOTAL_SOURCES();a++){
                ++i;
                string input_point = string(argv[i]);
                int sep=input_point.find(",");
                int x=string2int(input_point.substr(0,sep));
                int y=string2int(input_point.substr(sep+1,input_point.length()-1));
                Default.source.push_back(pair<int,int>(x,y));
            }
        } else if (string(argv[i]) == string("help") || string(argv[i]) == string("-h")) {
            cout << "[Help]" << endl;
            cout << "[-i input] [-o output] [-mo mask_output]" << endl;
            cout << "[-l lonely_start lonely_end lonely_times]" << endl;
            cout << "[-s $total_sources \"a1,b1\" \"a2,b2\" ... source]" << endl;
            cout << "[-bw black_white] [-m margin] [-h | help]" << endl;
            return 0;
        } else {
            return 0;
        }
    }
    Default.m_default.print_tree();

    CMat pic;
    CMat mask = pic.set_in_file_path( Default.INPUT() ).set_out_file_path( Default.OUTPUT() ).open();
    mask.guass().cvt_color().soble().guass().black_white( Default.BLACK_WHITE() );
    for(int i = 0 ; i < Default.LONELY_TIMES() ; i++){
        mask.lonely( Default.LONELY_START() + i, Default.LONELY_END() - i);
    }
    mask.set_out_file_path( Default.MASK_OUTPUT() ).save();
    pic.kill_pos_color(mask.flood(Default.source), Default.MARGIN()).save();
    return 0;
}
Exemplo n.º 7
0
Mat CmSaliencyRC::GetFT(CMat &img3f)
{
	CV_Assert(img3f.data != NULL && img3f.type() == CV_32FC3);
	Mat sal(img3f.size(), CV_32F), tImg;
	GaussianBlur(img3f, tImg, Size(3, 3), 0);
	cvtColor(tImg, tImg, CV_BGR2Lab);
	Scalar colorM = mean(tImg);
	for (int r = 0; r < tImg.rows; r++) {
		float *s = sal.ptr<float>(r);
		float *lab = tImg.ptr<float>(r);
		for (int c = 0; c < tImg.cols; c++, lab += 3)
			s[c] = (float)(sqr(colorM[0] - lab[0]) + sqr(colorM[1] - lab[1]) + sqr(colorM[2] - lab[2]));
	}
	normalize(sal, sal, 0, 1, NORM_MINMAX);
	return sal;
}
Exemplo n.º 8
0
// Show a label map. labelNum: how many number of random colors used for show, use default colors if is -1
Mat CmShow::Label(CMat& label1i, CStr& title, int labelNum, bool showIdx)
{
    bool useRandom = labelNum > 0;
    labelNum = useRandom ? labelNum : COLOR_NU_NO_GRAY;
    vector<Vec3b> colors(labelNum);
    if (useRandom)
        for (size_t i = 0; i < colors.size(); i++)
            colors[i] = RandomColor();
    else
        for (size_t i = 0; i < colors.size(); i++)
            colors[i] = gColors[i];

    Mat showImg = Mat::zeros(label1i.size(), CV_8UC3);
    for (int y = 0; y < label1i.rows; y++)	{
        Vec3b* showD = showImg.ptr<Vec3b>(y);
        const int* label = label1i.ptr<int>(y);
        for (int x = 0; x < label1i.cols; x++)
            if (label[x] >= 0) {
                showD[x] = colors[label[x] % labelNum];
                if (showIdx)
                    showD[x][2] = (byte)(label[x]);
            }
    }
    SaveShow(showImg, title);
    return showImg;
}
Exemplo n.º 9
0
void BenchMarkLatex::printModelRanking(CStr outName, CMat &meanF1d, CMat &maxF1d, CMat &cutAdp1d, CMat &cutSC1d, CMat &auc1d, CMat &mae1d)
{
	CV_Assert(meanF1d.size == cutAdp1d.size && cutAdp1d.size == cutSC1d.size);
	CV_Assert(meanF1d.size() == Size(_numDb, _numMethod));
	Mat RnkMean1i = getRankIdx(meanF1d), RnkAdp1i = getRankIdx(cutAdp1d), RnkSc1i = getRankIdx(cutSC1d);
	Mat RnkMax1i = getRankIdx(maxF1d), RnkAuc1i = getRankIdx(auc1d), RnkMae1i = getRankIdx(mae1d, false);
	const int NUM_ROW = 5; // Number of rows in the table
	const char* rowDes[NUM_ROW] = { "Max", "AUC", "MAE", "AdpT", "SCut" };
	bool decentOrder[NUM_ROW];
	memset(decentOrder, 0, sizeof(bool) * NUM_ROW);
	Mat dbTable = Mat::zeros(NUM_ROW, _numMethod, CV_64F);

	for (int m = 0; m < _numMethod; m++){
		for (int d = 0; d < _numDb; d++) {
			dbTable.at<double>(0, m) += RnkMax1i.at<int>(m, d) + 1;
			dbTable.at<double>(1, m) += RnkAuc1i.at<int>(m, d) + 1;
			dbTable.at<double>(2, m) += RnkMae1i.at<int>(m, d) + 1;
			dbTable.at<double>(3, m) += RnkAdp1i.at<int>(m, d) + 1;
			dbTable.at<double>(4, m) += RnkSc1i.at<int>(m, d) + 1;			
		}
	}
	printMatTransport(dbTable, outName + "D.tex", rowDes, decentOrder);

	Mat rnkDb = getRankIdx(dbTable.t(), false).t() + 1;
	printMatTransport(rnkDb, outName + ".tex", rowDes, decentOrder);
}
Exemplo n.º 10
0
void CmSaliencyRC::GetCmplx(CMat& mag32F, CMat& ang32F, Mat& cmplx32FC2)
{
	CV_Assert(mag32F.type() == CV_32FC1 && ang32F.type() == CV_32FC1 && mag32F.size() == ang32F.size());
	cmplx32FC2.create(mag32F.size(), CV_32FC2);
	for (int y = 0; y < mag32F.rows; y++)
	{
		float* cmpD = cmplx32FC2.ptr<float>(y);
		const float* dataA = ang32F.ptr<float>(y);
		const float* dataM = mag32F.ptr<float>(y);
		for (int x = 0; x < mag32F.cols; x++, cmpD += 2)
		{
			cmpD[0] = dataM[x] * cos(dataA[x]);
			cmpD[1] = dataM[x] * sin(dataA[x]);
		}
	}
}
Exemplo n.º 11
0
Mat CmSaliencyRC::GetSR(CMat &img3f)
{
	Size sz(64, 64);
	Mat img1f[2], sr1f, cmplxSrc2f, cmplxDst2f;
	cvtColor(img3f, img1f[1], CV_BGR2GRAY);
	resize(img1f[1], img1f[0], sz, 0, 0, CV_INTER_AREA); 

	img1f[1] = Mat::zeros(sz, CV_32F);
	merge(img1f, 2, cmplxSrc2f);
	dft(cmplxSrc2f, cmplxDst2f);
	AbsAngle(cmplxDst2f, img1f[0], img1f[1]);

	log(img1f[0], img1f[0]);
	blur(img1f[0], sr1f, Size(3, 3));
	sr1f = img1f[0] - sr1f;

	exp(sr1f, sr1f);
	GetCmplx(sr1f, img1f[1], cmplxDst2f);
	dft(cmplxDst2f, cmplxSrc2f, DFT_INVERSE | DFT_SCALE);
	split(cmplxSrc2f, img1f);

	pow(img1f[0], 2, img1f[0]);
	pow(img1f[1], 2, img1f[1]);
	img1f[0] += img1f[1];

	GaussianBlur(img1f[0], img1f[0], Size(3, 3), 0);
	normalize(img1f[0], img1f[0], 0, 1, NORM_MINMAX);
	resize(img1f[0], img1f[1], img3f.size(), 0, 0, INTER_CUBIC);

	return img1f[1];
}
Exemplo n.º 12
0
void GrabCutMF::illuProb(CMat sampleDf, CmGMM &bGMM, CmGMM &fGMM, CStr &nameNE)
{
	vector<Mat> pciF, pciB;
	Mat pI = Mat::zeros(sampleDf.size(), CV_32F);
	fGMM.GetProbsWN(sampleDf, pciF);
	bGMM.GetProbsWN(sampleDf, pciB);
	int fK = fGMM.K(), bK = bGMM.K();
	assert(fGMM.maxK() == bGMM.maxK());
	for (int i = 0; i < fK; i++)
		add(pciF[i], pI, pI);
	for (int i = 0; i < bK; i++)
		add(pciB[i], pI, pI);

	Mat tmpShow;
	for (int i = 0; i < fK; i++){
		divide(pciF[i], pI, pciF[i]);
		imwrite(nameNE + format("_F%d.png", i), pciF[i]*255);
	}
	for (int i = 0; i < bK; i++){
		divide(pciB[i], pI, pciB[i]);
		imwrite(nameNE + format("_B%d.png", i), pciB[i]*255);
	}

	for (int i = fK; i < fGMM.maxK(); i++)
		CmFile::WriteNullFile(nameNE + format("_F%d.nul", i));
	for (int i = bK; i < bGMM.maxK(); i++)
		CmFile::WriteNullFile(nameNE + format("_B%d.nul", i));

}
Exemplo n.º 13
0
Mat CmSaliencyRC::GetLC(CMat &img3f)
{
	Mat img;
	cvtColor(img3f, img, CV_BGR2GRAY);
	img.convertTo(img, CV_8U, 255);
	double f[256], s[256];
	memset(f, 0, 256*sizeof(double));
	memset(s, 0, 256*sizeof(double));
	for (int r = 0; r < img.rows; r++){
		byte* data = img.ptr<byte>(r);
		for (int c = 0; c < img.cols; c++)
			f[data[c]] += 1;
	}
	for (int i = 0; i < 256; i++)
		for (int j = 0; j < 256; j++)
			s[i] += abs(i - j) * f[j];
	Mat sal1f(img3f.size(), CV_64F);
	for (int r = 0; r < img.rows; r++){
		byte* data = img.ptr<byte>(r);
		double* sal = sal1f.ptr<double>(r);
		for (int c = 0; c < img.cols; c++)
			sal[c] = s[data[c]];
	}
	normalize(sal1f, sal1f, 0, 1, NORM_MINMAX, CV_32F);
	return sal1f;
}
// Training SVM with feature vector X and label Y. 
// Each row of X is a feature vector, with corresponding label in Y.
// Return a CV_32F weight Mat
Mat Objectness::trainSVM(CMat &X1f, const vecI &Y, int sT, double C, double bias, double eps)
{
	// Set SVM parameters
	parameter param; {
		param.solver_type = sT; // L2R_L2LOSS_SVC_DUAL;
		param.C = C;
		param.eps = eps; // see setting below
		param.p = 0.1;
		param.nr_weight = 0;
		param.weight_label = NULL;
		param.weight = NULL;
		set_print_string_function(print_null);
		CV_Assert(X1f.rows == Y.size() && X1f.type() == CV_32F);
	}

	// Initialize a problem
	feature_node *x_space = NULL;
	problem prob;{
		prob.l = X1f.rows;
		prob.bias = bias;
		prob.y = Malloc(double, prob.l);
		prob.x = Malloc(feature_node*, prob.l);
		const int DIM_FEA = X1f.cols;
		prob.n = DIM_FEA + (bias >= 0 ? 1 : 0);
		x_space = Malloc(feature_node, (prob.n + 1) * prob.l);
		int j = 0;
		for (int i = 0; i < prob.l; i++){
			prob.y[i] = Y[i];
			prob.x[i] = &x_space[j];
			const float* xData = X1f.ptr<float>(i);
			for (int k = 0; k < DIM_FEA; k++){
				x_space[j].index = k + 1;
				x_space[j++].value = xData[k];
			}
			if (bias >= 0){
				x_space[j].index = prob.n;
				x_space[j++].value = bias;
			}
			x_space[j++].index = -1;
		}
		CV_Assert(j == (prob.n + 1) * prob.l);
	}

	// Training SVM for current problem
	const char*  error_msg = check_parameter(&prob, &param);
	if(error_msg){
		fprintf(stderr,"ERROR: %s\n",error_msg);
		exit(1);
	}
	model *svmModel = train(&prob, &param);
	Mat wMat(1, prob.n, CV_64F, svmModel->w);
	wMat.convertTo(wMat, CV_32F);
	free_and_destroy_model(&svmModel);
	destroy_param(&param);
	free(prob.y);
	free(prob.x);
	free(x_space);
	return wMat;
}
Exemplo n.º 15
0
Mat CmShow::Complex(CMat& _cmplx, CStr& title, float minMagShowAng, int flag)
{
    CV_Assert(_cmplx.channels() == 2 && _cmplx.data != NULL);
    Mat ang(_cmplx.size(), CV_32FC1), mag(_cmplx.size(), CV_32FC1), cmplx;
    _cmplx.convertTo(cmplx, CV_32F);

    for (int y = 0; y < cmplx.rows; y++) {
        float* cpV = cmplx.ptr<float>(y);
        float* angV = ang.ptr<float>(y);
        float* magV = mag.ptr<float>(y);
        for (int x = 0; x < cmplx.cols; x++, cpV+=2) {
            magV[x] = sqrt(cpV[0] * cpV[0] + cpV[1] * cpV[1]);
            angV[x] = cvFastArctan(cpV[1], cpV[0]);
        }
    }
    return Complex(ang, mag, title, minMagShowAng, flag);
}
Exemplo n.º 16
0
void CmSaliencyRC::AbsAngle(CMat& cmplx32FC2, Mat& mag32FC1, Mat& ang32FC1)
{
	CV_Assert(cmplx32FC2.type() == CV_32FC2);
	mag32FC1.create(cmplx32FC2.size(), CV_32FC1);
	ang32FC1.create(cmplx32FC2.size(), CV_32FC1);

	for (int y = 0; y < cmplx32FC2.rows; y++)
	{
		const float* cmpD = cmplx32FC2.ptr<float>(y);
		float* dataA = ang32FC1.ptr<float>(y);
		float* dataM = mag32FC1.ptr<float>(y);
		for (int x = 0; x < cmplx32FC2.cols; x++, cmpD += 2)
		{
			dataA[x] = atan2(cmpD[1], cmpD[0]);
			dataM[x] = sqrt(cmpD[0] * cmpD[0] + cmpD[1] * cmpD[1]);
		}
	}
}
Exemplo n.º 17
0
Mat CmSaliencyRC::GetBorderReg(CMat &idx1i, int regNum, double ratio, double thr)
{
	// Variance of x and y
	vecD vX(regNum), vY(regNum);
	int w = idx1i.cols, h = idx1i.rows;{
		vecD mX(regNum), mY(regNum), n(regNum); // Mean value of x and y, pixel number of region
		for (int y = 0; y < idx1i.rows; y++){
			const int *idx = idx1i.ptr<int>(y);
			for (int x = 0; x < idx1i.cols; x++, idx++)
				mX[*idx] += x, mY[*idx] += y, n[*idx]++;
		}
		for (int i = 0; i < regNum; i++)
			mX[i] /= n[i], mY[i] /= n[i];
		for (int y = 0; y < idx1i.rows; y++){
			const int *idx = idx1i.ptr<int>(y);
			for (int x = 0; x < idx1i.cols; x++, idx++)
				vX[*idx] += abs(x - mX[*idx]), vY[*idx] += abs(y - mY[*idx]);
		}
		for (int i = 0; i < regNum; i++)
			vX[i] = vX[i]/n[i] + EPS, vY[i] = vY[i]/n[i] + EPS;
	}

	// Number of border pixels in x and y border region
	vecI xbNum(regNum), ybNum(regNum); 
	int wGap = cvRound(w * ratio), hGap = cvRound(h * ratio);
	vector<Point> bPnts; { 
		ForPoints2(pnt, 0, 0, w, hGap) // Top region
			ybNum[idx1i.at<int>(pnt)]++, bPnts.push_back(pnt);
		ForPoints2(pnt, 0, h - hGap, w, h) // Bottom region
			ybNum[idx1i.at<int>(pnt)]++, bPnts.push_back(pnt);
		ForPoints2(pnt, 0, 0, wGap, h) // Left region
			xbNum[idx1i.at<int>(pnt)]++, bPnts.push_back(pnt);
		ForPoints2(pnt, w - wGap, 0, w, h)
			xbNum[idx1i.at<int>(pnt)]++, bPnts.push_back(pnt);
	}

	Mat bReg1u(idx1i.size(), CV_8U);{  // likelihood map of border region
		double xR = 1.0/(4*hGap), yR = 1.0/(4*wGap);
		vector<byte> regL(regNum); // likelihood of each region belongs to border background
		for (int i = 0; i < regNum; i++) {
			double lk = xbNum[i] * xR / vY[i] + ybNum[i] * yR / vX[i];
			regL[i] = lk/thr > 1 ? 255 : 0; //saturate_cast<byte>(255 * lk / thr);
		}

		for (int r = 0; r < h; r++)	{
			const int *idx = idx1i.ptr<int>(r);
			byte* maskData = bReg1u.ptr<byte>(r);
			for (int c = 0; c < w; c++, idx++)
				maskData[c] = regL[*idx];
		}
	}

	for (size_t i = 0; i < bPnts.size(); i++)
		bReg1u.at<byte>(bPnts[i]) = 255;
	return bReg1u;
}
Exemplo n.º 18
0
Mat CmSaliencyRC::Get(CMat &img3f, GET_SAL_FUNC fun, int _wkSize)
{
	Mat sal;
	double ratio = _wkSize * 1.0 / max(img3f.rows, img3f.cols);
	Size wkSize(cvRound(img3f.cols * ratio), cvRound(img3f.rows * ratio));
	Mat wkImg, wkSal;
	resize(img3f, wkImg, wkSize, 0, 0, INTER_AREA);
	wkSal = fun(wkImg);
	resize(wkSal, sal, img3f.size(), 0, 0, INTER_CUBIC);
	return sal;
}
Exemplo n.º 19
0
int SegmentImage(CMat &_src3f, Mat &pImgInd, float sigma, float c, int min_size)
{
	CV_Assert(_src3f.type() == CV_32FC3);
	int width(_src3f.cols), height(_src3f.rows);
	pImgInd.create(height, width, CV_32S);
	image<RGB_f> *im = new image<RGB_f>(width, height, _src3f.data);
	image<int> *regIdx = new image<int>(width, height, pImgInd.data);
	int regNum = SegmentImage(im, regIdx, sigma, c, min_size);
	im->data = NULL; regIdx->data = NULL;
	delete im; delete regIdx;
	return regNum;
}
Exemplo n.º 20
0
void CmShow::SaveShow(CMat& img, CStr& title)
{
    if (title.size() == 0)
        return;

    int mDepth = CV_MAT_DEPTH(img.type());
    double scale = (mDepth == CV_32F || mDepth == CV_64F ? 255 : 1);
    if (title.size() > 4 && title[title.size() - 4] == '.')
        imwrite(title, img*scale);
    else if (title.size())
        imshow(title, img);
}
Exemplo n.º 21
0
// Write matrix to binary file
bool CmFile::matWrite(CStr& filename, CMat& _M){
	Mat M;
	_M.copyTo(M);
	FILE* file = fopen(_S(filename), "wb");
	if (file == NULL || M.empty())
		return false;
	fwrite("CmMat", sizeof(char), 5, file);
	int headData[3] = {M.cols, M.rows, M.type()};
	fwrite(headData, sizeof(int), 3, file);
	fwrite(M.data, sizeof(char), M.step * M.rows, file);
	fclose(file);
	return true;
}
Exemplo n.º 22
0
void BenchMarkLatex::saveData2Csv(CMat &_value1d, CStr csvFile, vecS &rowStart)
{
	Mat value1d = _value1d.reshape(1);
	FILE *f = fopen(_S(csvFile), "w");
	CV_Assert(f != NULL && value1d.type() == CV_64FC1);
	for (int r = 0; r < value1d.rows; r++)	{
		const double *v = value1d.ptr<double>(r);
		fprintf(f, "\n%s", _S(rowStart[r]));
		for (int c = 0; c < value1d.cols; c++)	
			fprintf(f, ", %5.3f", v[c]);
	}
	fclose(f);
}
Exemplo n.º 23
0
// src3f are BGR, color3f are 1xBinDim matrix represent color fore each histogram bin
int CmColorQua::S_BinInf(CMat& idx1i, Mat &color3f, vecI &colorNum, int method, CMat &src3f)
{
	int totalBinNum = 0;
	CV_Assert(idx1i.data != NULL && idx1i.type() == CV_32S && method >= 0 && method < S_Q_NUM);

	// Find colors for each bin
	color3f = Mat::zeros(1, binNum[method], CV_32FC3);
	Vec3f* color = (Vec3f*)(color3f.data);

	vector<Vec3d> colorD(color3f.cols, 0);
	colorNum.resize(color3f.cols, 0);
	if (src3f.size() != Size() && src3f.data != NULL)	{
		for (int r = 0; r < idx1i.rows; r++) {
			const int *idx = idx1i.ptr<int>(r);
			const Vec3f *src = src3f.ptr<Vec3f>(r);
			for (int c = 0; c < idx1i.cols; c++)	{
				colorD[idx[c]] += src[c];
				colorNum[idx[c]] ++;
			}
		}
	}
	S_RECOVER_FUNC SR_Function = srFuns[method];
	for (int i = 0; i < color3f.cols; i++)	{
		if (colorNum[i] == 0)
			(*SR_Function)(i, color[i]);
		else
			totalBinNum += colorNum[i];
	}
	if (method == 1)
		cvtColor(color3f, color3f, CV_HSV2BGR);
	else if (method == 2)
		cvtColor(color3f, color3f, CV_Lab2BGR);

	for (int i = 0; i < color3f.cols; i++)
		if (colorNum[i] > 0)
			color[i] = Vec3f((float)(colorD[i][0]/colorNum[i]), (float)(colorD[i][1]/colorNum[i]), (float)(colorD[i][2]/colorNum[i]));

	return totalBinNum;
}
Exemplo n.º 24
0
Mat GrabCutMF::getGrabMask(CMat &img3u, Rect rect)
{
	// Initialize flood fill
	queue<Point> selectedPnts;
	const int _h = img3u.rows, _w = img3u.cols, BW = 5;
	{// If not connected to image border, expand selection border unless stopped by edges
		Point rowT(rect.x, rect.y), rowB(rect.x, rect.y + rect.height - 1);
		Point colL(rect.x, rect.y), colR(rect.x + rect.width - 1, rect.y);
		if (rect.x >= BW) // Expand left edge
			for (int y = 0; y < rect.height; y++, colL.y++) selectedPnts.push(colL);
		else
			rect.x = BW;
		if (rect.y >= BW) // Expand top edge
			for (int x = 0; x < rect.width; x++, rowT.x++)	selectedPnts.push(rowT);
		else
			rect.y = BW;
		if (rect.x + rect.width + BW <= _w) // Expand right edge	
			for (int y = 0; y < rect.height; y++, colR.y++) selectedPnts.push(colR);
		else
			rect.width = _w - rect.x - BW;
		if (rect.y + rect.height + BW <= _h) // Expand bottom edge
			for (int x = 0; x < rect.width; x++, rowB.x++) selectedPnts.push(rowB);
		else
			rect.height = _h - rect.y - BW;
	}

	Mat mask1u(img3u.size(), CV_8U);
	memset(mask1u.data, 255, mask1u.step.p[0] * mask1u.rows);
	mask1u(rect) = Scalar(0);

	Mat edge1u;
	CmCv::CannySimpleRGB(img3u, edge1u, 120, 1200, 5);
	dilate(edge1u, edge1u, Mat(), Point(-1, -1), 3);
	//rectangle(edge1u, rect, Scalar(128));
	//imwrite(sameNameNE + "_Selection.png", edge1u);

	// Flood fill
	while (!selectedPnts.empty()){
		Point crntPnt = selectedPnts.front();
		mask1u.at<byte>(crntPnt) = 255;
		selectedPnts.pop();
		for (int i = 0; i < 4; i++){
			Point nbrPnt = crntPnt + DIRECTION4[i];
			if (CHK_IND(nbrPnt) && mask1u.at<byte>(nbrPnt) == 0 && edge1u.at<byte>(nbrPnt) == 0)
				mask1u.at<byte>(nbrPnt) = 255, selectedPnts.push(nbrPnt);
		}
	}
	cv::Mat temp(mask1u(Rect(rect.x + 1, rect.y + 1, rect.width - 2, rect.height - 2)));
	CmCv::rubustifyBorderMask(temp);
	return mask1u;
}
Exemplo n.º 25
0
Mat CmSaliencyRC::GetRC(CMat &img3f, CMat &regIdx1i, int regNum, double sigmaDist)
{
	Mat colorIdx1i, regSal1v, tmp, color3fv;
	int QuatizeNum = Quantize(img3f, colorIdx1i, color3fv, tmp);
	if (QuatizeNum == 2){
		printf("QuatizeNum == 2, %d: %s\n", __LINE__, __FILE__);
		Mat sal;
		compare(colorIdx1i, 1, sal, CMP_EQ);
		sal.convertTo(sal, CV_32F, 1.0/255);
		return sal;
	}
	if (QuatizeNum <= 2) // Color quantization
		return Mat::zeros(img3f.size(), CV_32F);
	
	cvtColor(color3fv, color3fv, CV_BGR2Lab);
	vector<Region> regs(regNum);
	BuildRegions(regIdx1i, regs, colorIdx1i, color3fv.cols);
	RegionContrast(regs, color3fv, regSal1v, sigmaDist);

	Mat sal1f = Mat::zeros(img3f.size(), CV_32F);
	cv::normalize(regSal1v, regSal1v, 0, 1, NORM_MINMAX, CV_32F);
	float* regSal = (float*)regSal1v.data;
	for (int r = 0; r < img3f.rows; r++){
		const int* regIdx = regIdx1i.ptr<int>(r);
		float* sal = sal1f.ptr<float>(r);
		for (int c = 0; c < img3f.cols; c++)
			sal[c] = regSal[regIdx[c]];
	}

	Mat bdReg1u = GetBorderReg(regIdx1i, regNum, 0.02, 0.4);
	sal1f.setTo(0, bdReg1u); 
	SmoothByHist(img3f, sal1f, 0.1f); 
	SmoothByRegion(sal1f, regIdx1i, regNum); 
	sal1f.setTo(0, bdReg1u);

	GaussianBlur(sal1f, sal1f, Size(3, 3), 0);
	return sal1f;
}
Exemplo n.º 26
0
void BenchMarkLatex::printMat(CMat &_mat1d, CStr texFile, bool descendOrder)
{
	FILE* f = fopen(_S(texFile), "w");
	if (f == NULL){
		printf("Can't open file %s\n", _S(texFile));
		return;
	}
	CV_Assert(_mat1d.rows == _numMethod);

	Mat mat1d = _mat1d.reshape(1);
	int dataWidth = mat1d.cols;
	string strAlign = "|l||";
	for (int i = 0; i < _mat1d.cols; i++){
		for (int c = 0; c < _mat1d.channels(); c++)
			strAlign += "c";
		strAlign += "|";
	}
	fprintf(f, "\\begin{tabular}{%s} \\hline\n\t\\tabTitle \\\\", _S(strAlign));
	const char* rankCommand[3] = {"\\first", "\\second", "\\third"};
	Mat rnk1i = getRankIdx(mat1d, descendOrder);
	for (int i = 0; i < _numMethod; i++){
		if (find(_subNumbers.begin(), _subNumbers.end(), i) != _subNumbers.end())
			fprintf(f, "\t\\hline \\hline\n");
		fprintf(f, "\t\\textbf{%-5s ", _S(_methodNames[i] + "}"));
		for (int j = 0; j < dataWidth; j++){
			int idx = rnk1i.at<int>(i, j);
			if (idx < 3)
				fprintf(f, "& %s{%5.3f} ", rankCommand[idx], mat1d.at<double>(i, j));
			else
				fprintf(f, "& %5.3f ", mat1d.at<double>(i, j));
		}
		fprintf(f, "\\\\\n");
	}
	fprintf(f, "\\hline\n\\end{tabular}\n");

	fclose(f);
}
Exemplo n.º 27
0
// img3f and src3f are BGR 
void CmColorQua::S_Recover(CMat& idx1i, Mat& img3f, int method, CMat &src3f)
{
	Mat color3f;
	vecI colorNum;
	S_BinInf(idx1i, color3f, colorNum, method, src3f);
	Vec3f* color = (Vec3f*)(color3f.data);

	img3f.create(idx1i.size(), CV_32FC3);
	for (int r = 0; r < idx1i.rows; r++)	{
		const int *idx = idx1i.ptr<int>(r);
		Vec3f *img = img3f.ptr<Vec3f>(r);
		for (int c = 0; c < idx1i.cols; c++)
			img[c] = color[idx[c]]; 
	}
}
Exemplo n.º 28
0
void CmColorQua::D_Recover(CMat& idx1i, Mat &img3f, CMat &color3f)
{
	CV_Assert(idx1i.data != NULL);
	img3f.create(idx1i.size(), CV_32FC3);
	
	Vec3f* color = (Vec3f*)(color3f.data);
	for (int y = 0; y < idx1i.rows; y++) {
		Vec3f* imgData = img3f.ptr<Vec3f>(y);
		const int* idx = idx1i.ptr<int>(y);
		for (int x = 0; x < idx1i.cols; x++) {
			imgData[x] = color[idx[x]];
			CV_Assert(idx[x] < color3f.cols);
		}
	}
}
Exemplo n.º 29
0
Mat CmShow::HistBins(CMat& color3f, CMat& val, CStr& title, bool descendShow, CMat &with)
{
    // Prepare data
    int H = 300, spaceH = 6, barH = 10, n = color3f.cols;
    CV_Assert(color3f.size() == val.size() && color3f.rows == 1);
    Mat binVal1i, binColor3b, width1i;
    if (with.size() == val.size())
        with.convertTo(width1i, CV_32S, 400/sum(with).val[0]); // Default shown width
    else
        width1i = Mat(1, n, CV_32S, Scalar(600.0/(val.cols*val.rows))); // Default bin width = 10
    int W = cvRound(sum(width1i).val[0]);
    color3f.convertTo(binColor3b, CV_8UC3, 255);
    double maxVal, minVal;
    minMaxLoc(val, &minVal, &maxVal);
    val.convertTo(binVal1i, CV_32S, H/max(maxVal, -minVal));
    Size szShow(W, H + spaceH + barH);
    szShow.height += minVal < 0 && !descendShow ? H + spaceH : 0;
    Mat showImg3b(szShow, CV_8UC3, WHITE);
    int* binH = (int*)(binVal1i.data);
    Vec3b* binColor = (Vec3b*)(binColor3b.data);
    int* binW = (int*)(width1i.data);
    vector<CostiIdx> costIdx(n);
    if (descendShow) {
        for (int i = 0; i < n; i++)
            costIdx[i] = make_pair(binH[i], i);
        sort(costIdx.begin(), costIdx.end(), std::greater<CostiIdx>());
    }

    // Show image
    for (int i = 0, x = 0; i < n; i++) {
        int idx = descendShow ? costIdx[i].second : i;
        int h = descendShow ? abs(binH[idx]) : binH[idx];
        Scalar color(binColor[idx]);
        Rect reg(x, H + spaceH, binW[idx], barH);
        showImg3b(reg) = color; // Draw bar
        rectangle(showImg3b, reg, BLACK);

        reg.height = abs(h);
        reg.y = h >= 0 ? H - h : H + 2 * spaceH + barH;
        showImg3b(reg) = color;
        rectangle(showImg3b, reg, BLACK);

        x += binW[idx];
    }
    putText(showImg3b, format("Min = %g, Max = %g", minVal, maxVal), Point(5, 20), CV_FONT_HERSHEY_PLAIN, 1, CV_RGB(255,0,0));
    SaveShow(showImg3b, title);
    return showImg3b;
}
Exemplo n.º 30
0
// Write matrix to binary file
bool Objectness::matWrite(CStr& filename, CMat& _M) {
    Mat M;
    _M.copyTo(M);
    FILE* file = fopen(_S(filename), "wb");
    if (file == NULL || M.empty())
        return false;
    fwrite("CmMat", sizeof(char), 5, file);
    int headData[3] = {M.cols, M.rows, M.type()};
    fwrite(headData, sizeof(int), 3, file);
    fwrite(M.data, sizeof(char), M.step * M.rows, file);
    fclose(file);

    FileStorage fs(filename + ".yml.gz", FileStorage::WRITE);
    fs << removeExtension(basename(filename)) << M;
    fs.release();

    return true;
}