void run(cv::Mat& image,cv::Mat& outImage,cv::Mat& outMinTrace,cv::Mat& outDeletedLine)
{
	cv::Mat image_gray(image.rows,image.cols,CV_8U,cv::Scalar(0));
	cv::cvtColor(image,image_gray,CV_BGR2GRAY); //彩色图像转换为灰度图像

	cv::Mat gradiant_H(image.rows,image.cols,CV_32F,cv::Scalar(0));//水平梯度矩阵
	cv::Mat gradiant_V(image.rows,image.cols,CV_32F,cv::Scalar(0));//垂直梯度矩阵

	cv::Mat kernel_H = (cv::Mat_<float>(3,3) << 0, 0, 0, 0, 1, -1, 0, 0, 0); //求水平梯度所使用的卷积核(赋初始值)
	cv::Mat kernel_V = (cv::Mat_<float>(3,3) << 0, 0, 0, 0, 1, 0, 0, -1, 0); //求垂直梯度所使用的卷积核(赋初始值)

	cv::filter2D(image_gray,gradiant_H,gradiant_H.depth(),kernel_H);
	cv::filter2D(image_gray,gradiant_V,gradiant_V.depth(),kernel_V);

	cv::Mat gradMag_mat(image.rows,image.rows,CV_32F,cv::Scalar(0));
	cv::add(cv::abs(gradiant_H),cv::abs(gradiant_V),gradMag_mat);//水平与垂直滤波结果的绝对值相加,可以得到近似梯度大小

	////如果要显示梯度大小这个图,因为gradMag_mat深度是CV_32F,所以需要先转换为CV_8U
	//cv::Mat testMat;
	//gradMag_mat.convertTo(testMat,CV_8U,1,0);
	//cv::imshow("Image Show Window2",testMat);

	//计算能量线
	cv::Mat energyMat(image.rows,image.cols,CV_32F,cv::Scalar(0));//累计能量矩阵
	cv::Mat traceMat(image.rows,image.cols,CV_32F,cv::Scalar(0));//能量最小轨迹矩阵
	calculateEnergy(gradMag_mat,energyMat,traceMat); 

	//找出最小能量线
	cv::Mat minTrace(image.rows,1,CV_32F,cv::Scalar(0));//能量最小轨迹矩阵中的最小的一条的轨迹
	getMinEnergyTrace(energyMat,traceMat,minTrace);

	//显示最小能量线
	cv::Mat tmpImage(image.rows,image.cols,image.type());
	image.copyTo(tmpImage);
	for (int i = 0;i < image.rows;i++)
	{
		int k = minTrace.at<float>(i,0);
		tmpImage.at<cv::Vec3b>(i,k)[0] = 0;
		tmpImage.at<cv::Vec3b>(i,k)[1] = 0;
		tmpImage.at<cv::Vec3b>(i,k)[2] = 255;
	}
	cv::imshow("Image Show Window (缩小)",tmpImage);

	//删除一列
	cv::Mat image2(image.rows,image.cols-1,image.type());
	cv::Mat beDeletedLine(image.rows,1,CV_8UC3);//记录被删掉的那一列的值
	delOneCol(image,image2,minTrace,beDeletedLine);
	cv::imshow("Image Show Window",image2);

	image2.copyTo(outImage);
	minTrace.copyTo(outMinTrace);
	beDeletedLine.copyTo(outDeletedLine);
}
Exemple #2
0
static PyObject *UtilsC_expectation_A(PyObject *self, PyObject *args){
    PyObject *Y,*y_tilde,*m_A,*m_H,*X,*Gamma,*Sigma_A,*sigma_epsilone,*Sigma_H,*PL,*mu_MK,*sigma_MK,*q_Z;
    PyArrayObject *q_Zarray,*mu_MKarray,*sigma_MKarray,*PLarray,*Yarray,*y_tildearray,*m_Aarray,*m_Harray,*sigma_epsilonearray,*Sigma_Harray,*Xarray,*Gammaarray,*Sigma_Aarray;
    int j,J,m,m2,d,D,nCond,Nrep,nr,K,k;
    PyArg_ParseTuple(args, "OOOOOOOOOOOOOiiiii",&q_Z,&mu_MK,&sigma_MK,&PL,&sigma_epsilone,&Gamma,&Sigma_H,&Y,&y_tilde,&m_A,&m_H,&Sigma_A,&X,&J,&D,&nCond,&Nrep,&K);
    Xarray   = (PyArrayObject *) PyArray_ContiguousFromObject(X,PyArray_INT,3,3);
    Sigma_Harray = (PyArrayObject *) PyArray_ContiguousFromObject(Sigma_H,PyArray_FLOAT64,2,2);
    m_Aarray = (PyArrayObject *) PyArray_ContiguousFromObject(m_A,PyArray_FLOAT64,2,2);
    mu_MKarray = (PyArrayObject *) PyArray_ContiguousFromObject(mu_MK,PyArray_FLOAT64,2,2);
    sigma_MKarray = (PyArrayObject *) PyArray_ContiguousFromObject(sigma_MK,PyArray_FLOAT64,2,2);
    PLarray = (PyArrayObject *) PyArray_ContiguousFromObject(PL,PyArray_FLOAT64,2,2);
    m_Harray = (PyArrayObject *) PyArray_ContiguousFromObject(m_H,PyArray_FLOAT64,1,1);
    y_tildearray = (PyArrayObject *) PyArray_ContiguousFromObject(y_tilde,PyArray_FLOAT64,2,2);
    sigma_epsilonearray = (PyArrayObject *) PyArray_ContiguousFromObject(sigma_epsilone,PyArray_FLOAT64,1,1);
    Yarray   = (PyArrayObject *) PyArray_ContiguousFromObject(Y,PyArray_FLOAT64,2,2);
    Gammaarray   = (PyArrayObject *) PyArray_ContiguousFromObject(Gamma,PyArray_FLOAT64,2,2);
    Sigma_Aarray = (PyArrayObject *) PyArray_ContiguousFromObject(Sigma_A,PyArray_FLOAT64,3,3);
    q_Zarray = (PyArrayObject *) PyArray_ContiguousFromObject(q_Z,PyArray_FLOAT64,3,3); 
    npy_float64 *H,*tmp,*tmpT,*GGamma,*tmp2,*X_tilde,*Sigma_Aj,*Delta,*tmpDD,*tmpD,*tmpNrep,*tmpNrep2,*tmpnCond,*tmpnCond2;
    npy_float64 *yy_tilde,*SSigma_H,*Sigma_A0;
    int *XX,*XXT;
    tmpnCond = malloc(sizeof(npy_float64)*nCond);
    tmpnCond2 = malloc(sizeof(npy_float64)*nCond);
    SSigma_H = malloc(sizeof(npy_float64)*D*D);
    Delta = malloc(sizeof(npy_float64)*nCond*nCond);
    Sigma_A0 = malloc(sizeof(npy_float64)*nCond*nCond);
    Sigma_Aj = malloc(sizeof(npy_float64)*nCond*nCond);
    X_tilde = malloc(sizeof(npy_float64)*nCond*D);
    H = malloc(sizeof(npy_float64)*D);
    tmp = malloc(sizeof(npy_float64)*Nrep*D);
    tmpD = malloc(sizeof(npy_float64)*D);
    tmpDD = malloc(sizeof(npy_float64)*D*D);
    tmpNrep = malloc(sizeof(npy_float64)*Nrep);
    tmpNrep2 = malloc(sizeof(npy_float64)*Nrep);
    tmpT = malloc(sizeof(npy_float64)*Nrep*D);
    GGamma = malloc(sizeof(npy_float64)*Nrep*Nrep);
    tmp2 = malloc(sizeof(npy_float64)*Nrep*D);
    yy_tilde = malloc(sizeof(npy_float64)*Nrep);
    XX = malloc(sizeof(int)*D*Nrep);
    XXT = malloc(sizeof(int)*D*Nrep);

    for (d=0;d<Nrep;d++){
        for (nr=0;nr<Nrep;nr++){
            GGamma[d*Nrep + nr] = GetValue(Gammaarray,nr,d);
        }
    }
    for (d=0;d<D;d++){
        H[d] = GetValue1D(m_Harray,d);
        for (nr=0;nr<D;nr++){
            SSigma_H[d*D+nr] = GetValue(Sigma_Harray,d,nr);
        }
    }
    for (m=0;m<nCond;m++){
        for (m2=0;m2<nCond;m2++){
            for (d=0;d<D;d++){
                for (nr=0;nr<Nrep;nr++){
                    XX[nr*D + d] = GetValue3DInt(Xarray,m2,nr,d);
                    XXT[d*Nrep + nr] = GetValue3DInt(Xarray,m,nr,d);
                }
            }
            prodMat4(H,XXT,tmpNrep,1,Nrep,D);
            prodMat2(tmpNrep,GGamma,tmpNrep2,1,Nrep,Nrep);
            prodMat4(tmpNrep2,XX,tmpD,1, D, Nrep);
            Sigma_A0[m*nCond + m2] = prodMatScal(tmpD,H,D);
            prodMat4(SSigma_H,XXT,tmp,D,Nrep,D);
            prodMat2(tmp,GGamma,tmpT,D,Nrep,Nrep);
            prodMat4(tmpT,XX,tmpDD,D, D, Nrep);
            Sigma_A0[m*nCond + m2] += traceMat(tmpDD,D);
            Delta[m*nCond+m2] = 0;
        }
    }
    for (j=0;j<J;j++){
        if (GetValue1D(sigma_epsilonearray,j) < eps) GetValue1D(sigma_epsilonearray,j) = eps;
        for (nr=0;nr<Nrep;nr++){
            yy_tilde[nr] = GetValue(y_tildearray,nr,j);
        }
        for (m=0;m<nCond;m++){
            for (d=0;d<D;d++){
                for (nr=0;nr<Nrep;nr++){
                    XX[nr*D + d] = GetValue3DInt(Xarray,m,nr,d);
                }
            }
            prodMat2(GGamma,yy_tilde,tmpNrep,Nrep,1,Nrep);
            prodMat4(tmpNrep,XX,tmpD,1,D,Nrep);
            for (d=0;d<D;d++){ 
                X_tilde[m*D+d] = tmpD[d] / GetValue1D(sigma_epsilonearray,j);
            }
            for (m2=0;m2<nCond;m2++){
                Sigma_Aj[m*nCond + m2] = Sigma_A0[m*nCond + m2] / GetValue1D(sigma_epsilonearray,j);
            }
        }
        prodMat2(X_tilde,H,tmpnCond,nCond,1,D);
        for (k=0;k<K;k++){
            for (m=0;m<nCond;m++){
        //         Delta[m*nCond+m] = GetValue3D(q_Zarray,m,k,j) / (GetValue(sigma_MKarray,m,k) + eps);
                Delta[m*nCond+m] = GetValue3D(q_Zarray,m,k,j) / GetValue(sigma_MKarray,m,k);
                tmpnCond2[m] = GetValue(mu_MKarray,m,k);
            }
            prodaddMat(Delta,tmpnCond2,tmpnCond,nCond,1,nCond);
            addMatfast(Delta,Sigma_Aj,nCond,nCond);
        }
        invMat(Sigma_Aj,nCond);
        for (m=0;m<nCond;m++){
            for (m2=0;m2<nCond;m2++){
                GetValue3D(Sigma_Aarray,m,m2,j) = Sigma_Aj[m*nCond + m2];
            }
        }
        prodMat2(Sigma_Aj,tmpnCond,tmpnCond2,nCond,1,nCond);
        for (m=0;m<nCond;m++){
            GetValue(m_Aarray,j,m) = tmpnCond2[m];
        }
    }

    free(X_tilde);
    free(Sigma_Aj);
    free(Sigma_A0);
    free(Delta);
    free(tmpDD);
    free(tmpD);
    free(tmpNrep);
    free(tmpNrep2);
    free(tmpnCond);
    free(tmpnCond2);
    free(H);
    free(tmp);
    free(tmpT);
    free(GGamma);
    free(tmp2);
    free(yy_tilde);
    free(XX);
    free(XXT);

    Py_DECREF(q_Zarray);
    Py_DECREF(Sigma_Harray);
    Py_DECREF(Sigma_Aarray);
    Py_DECREF(m_Aarray);
    Py_DECREF(m_Harray);
    Py_DECREF(Xarray);
    Py_DECREF(Yarray);
    Py_DECREF(sigma_MKarray);
    Py_DECREF(mu_MKarray);
    Py_DECREF(PLarray);
    Py_DECREF(y_tildearray);
    Py_DECREF(Gammaarray);
    Py_DECREF(sigma_epsilonearray);
    Py_INCREF(Py_None);
    return Py_None;
}   
Exemple #3
0
static PyObject *UtilsC_maximization_sigma_noise(PyObject *self, PyObject *args){
    PyObject *Y,*m_A,*m_H,*X,*Sigma_A,*sigma_epsilone,*Sigma_H,*PL,*Gamma;
    PyArrayObject *Yarray,*m_Aarray,*m_Harray,*sigma_epsilonearray,*Sigma_Harray,*Xarray,*Sigma_Aarray,*PLarray,*Gammaarray;
    int j,J,m,m2,d,D,nCond,Nrep,nr;
    PyArg_ParseTuple(args, "OOOOOOOOOiiii",&Gamma,&PL,&sigma_epsilone,&Sigma_H,&Y,&m_A,&m_H,&Sigma_A,&X,&J,&D,&nCond,&Nrep);
    Xarray   = (PyArrayObject *) PyArray_ContiguousFromObject(X,PyArray_INT,3,3);
    m_Aarray = (PyArrayObject *) PyArray_ContiguousFromObject(m_A,PyArray_FLOAT64,2,2);
    PLarray = (PyArrayObject *) PyArray_ContiguousFromObject(PL,PyArray_FLOAT64,2,2);
    m_Harray = (PyArrayObject *) PyArray_ContiguousFromObject(m_H,PyArray_FLOAT64,1,1);
    sigma_epsilonearray = (PyArrayObject *) PyArray_ContiguousFromObject(sigma_epsilone,PyArray_FLOAT64,1,1);
    Yarray   = (PyArrayObject *) PyArray_ContiguousFromObject(Y,PyArray_FLOAT64,2,2);
    Sigma_Harray   = (PyArrayObject *) PyArray_ContiguousFromObject(Sigma_H,PyArray_FLOAT64,2,2);
    Sigma_Aarray = (PyArrayObject *) PyArray_ContiguousFromObject(Sigma_A,PyArray_FLOAT64,3,3);
    Gammaarray   = (PyArrayObject *) PyArray_ContiguousFromObject(Gamma,PyArray_FLOAT64,2,2);
    npy_float64 *S,*H,*Htilde,*Yj,*PLj,*Sigma_A0,*m_Aj,*tmpnCondnCond,*tmpnCond,*tmpD,*tmp,*tmpDD,*SSigma_H,*GGamma;
    int *XX,*XXT;

    Sigma_A0 = malloc(sizeof(npy_float64)*nCond*nCond);
    H = malloc(sizeof(npy_float64)*D);
    m_Aj = malloc(sizeof(npy_float64)*nCond);
    Yj = malloc(sizeof(npy_float64)*Nrep);
    PLj = malloc(sizeof(npy_float64)*Nrep);
    S = malloc(sizeof(npy_float64)*Nrep);
    tmpnCondnCond = malloc(sizeof(npy_float64)*nCond*nCond);
    tmpnCond = malloc(sizeof(npy_float64)*nCond);
    tmpD = malloc(sizeof(npy_float64)*D);
    tmpDD = malloc(sizeof(npy_float64)*D*D);
    tmp = malloc(sizeof(npy_float64)*D*Nrep);
    Htilde = malloc(sizeof(npy_float64)*nCond*nCond);
    XX = malloc(sizeof(int)*D*Nrep);
    XXT = malloc(sizeof(int)*D*Nrep);
    SSigma_H = malloc(sizeof(npy_float64)*D*D);
    GGamma = malloc(sizeof(npy_float64)*Nrep*Nrep);

    npy_float64 *SQ, *YGamma;
    SQ = malloc(sizeof(npy_float64)*Nrep);
    YGamma = malloc(sizeof(npy_float64)*Nrep);

    for (d=0;d<Nrep;d++){
        for (nr=0;nr<Nrep;nr++){
            GGamma[d*Nrep + nr] = GetValue(Gammaarray,nr,d);
        }
    }

    for (d=0;d<D;d++){
        H[d] = GetValue1D(m_Harray,d);
        for (nr=0;nr<D;nr++){
            SSigma_H[d*D+nr] = GetValue(Sigma_Harray,d,nr);
        }
    }
    for (m=0;m<nCond;m++){
        for (m2=0;m2<nCond;m2++){
            for (d=0;d<D;d++){
                for (nr=0;nr<Nrep;nr++){
                    XX[nr*D + d] = GetValue3DInt(Xarray,m2,nr,d);
                    XXT[d*Nrep + nr] = GetValue3DInt(Xarray,m,nr,d);
                }
            }
            prodMat4(H,XXT,S,1,Nrep,D);
            prodMat4(S,XX,tmpD,1, D, Nrep);
            Htilde[m*nCond + m2] = prodMatScal(tmpD,H,D);
            prodMat4(SSigma_H,XXT,tmp,D,Nrep,D);
            prodMat4(tmp,XX,tmpDD,D, D, Nrep);
            Htilde[m*nCond + m2] += traceMat(tmpDD,D);
        }
    }
    for (j=0;j<J;j++){
        for (nr=0;nr<Nrep;nr++){
            S[nr] = 0;
            Yj[nr] = GetValue(Yarray,nr,j);
            PLj[nr] = GetValue(PLarray,nr,j);
            //      printf("Yj[%d] = %f\n",nr, Yj[nr]);
            //      printf("PLj[%d] = %f\n",nr, PLj[nr]);
            //       getchar();
        }
        for (m=0;m<nCond;m++){
            for (d=0;d<D;d++){
                for (nr=0;nr<Nrep;nr++){
                    XX[nr*D + d] = GetValue3DInt(Xarray,m,nr,d);
                }
            }
            prodaddMatAlpha(XX,H,S,Nrep,1,D,GetValue(m_Aarray,j,m));
            prodMat2(S,GGamma,SQ,1,Nrep,Nrep);
            
            for (m2=0;m2<nCond;m2++){
                Sigma_A0[m*nCond+m2] = GetValue3D(Sigma_Aarray,m,m2,j);
            }
            m_Aj[m] = GetValue(m_Aarray,j,m);
        }
        SubMatfastVect(PLj,Yj,Nrep);
        //   for (nr=0;nr<Nrep;nr++){
        //       printf("Yj[%d] = %f\n",nr, Yj[nr]);
        //       getchar();
        //   }
        
        GetValue1D(sigma_epsilonearray,j) = -2*prodMatScal(SQ,Yj,Nrep);
        prodMat2(Sigma_A0,Htilde,tmpnCondnCond,nCond,nCond,nCond);
        GetValue1D(sigma_epsilonearray,j) += traceMat(tmpnCondnCond,nCond);
        prodMat2(m_Aj,Htilde,tmpnCond,1,nCond,nCond);
        GetValue1D(sigma_epsilonearray,j) += prodMatScal(tmpnCond,m_Aj,nCond);
        prodMat2(Yj,GGamma,YGamma,1,Nrep,Nrep);
        GetValue1D(sigma_epsilonearray,j) += prodMatScal(YGamma,Yj,Nrep);
        GetValue1D(sigma_epsilonearray,j) /= Nrep;
        //   printf("sigma_epsilonearray[%d] = %f\n",j, GetValue1D(sigma_epsilonearray,j));
        //   getchar();
    }

    free(Sigma_A0);
    free(H);
    free(m_Aj);
    free(Yj);
    free(PLj);
    free(S);
    free(tmpnCondnCond);
    free(tmpnCond);
    free(tmpD);
    free(tmpDD);
    free(tmp);
    free(Htilde);
    free(XX);
    free(XXT);
    free(SSigma_H);
    free(GGamma);
    free(SQ);
    free(YGamma);

    Py_DECREF(Yarray);
    Py_DECREF(m_Aarray);
    Py_DECREF(m_Harray);
    Py_DECREF(sigma_epsilonearray);
    Py_DECREF(Sigma_Harray);
    Py_DECREF(Xarray);
    Py_DECREF(Sigma_Aarray);
    Py_DECREF(PLarray);
    Py_DECREF(Gammaarray);

    Py_INCREF(Py_None);
    return Py_None;
}