如何计算“;Arctan”;以及“;Pow”;IplImage

本文关键字:何计算 以及 Pow IplImage Arctan 计算 | 更新日期: 2023-09-27 18:28:59

我正在尝试使用OpenCvSharp计算garyscale图像的渐变幅值和方向。问题是"Pow"功能似乎不适合IplImage。我还想知道如何计算featureImage的tan-1(或arctan)。谢谢

        using (IplImage cvImage = new IplImage("grayImage.png", LoadMode.AnyDepth | 
                LoadMode.GrayScale))
        using (IplImage dstXImage = new IplImage(cvImage.Size, cvImage.Depth, cvImage.NChannels))
        using (IplImage dstYImage = new IplImage(cvImage.Size, cvImage.Depth, cvImage.NChannels))
        {
            float[] data = {    0, -1, -1, 2    };
            CvMat kernel = new CvMat(2, 2, MatrixType.F32C1, data);
            Cv.Sobel(cvImage, dstXImage, 1, 0, ApertureSize.Size1);
            Cv.Sobel(cvImage, dstYImage, 0, 1, ApertureSize.Size1);
            Cv.Normalize(dstXImage, dstXImage, 1.0, 0, NormType.L1);
            Cv.Filter2D(cvImage, dstXImage, kernel, new CvPoint(0, 0));
            Cv.Normalize(dstYImage, dstYImage, 1.0, 0, NormType.L1);
            Cv.Filter2D(cvImage, dstYImage, kernel, new CvPoint(0, 0));
            // to calculate gradient magnitude, sqrt[(dy)power 2  +  (dx)power 2]
            dstXImage.Mul(dstXImage, dstXImage);
            dstYImage.Mul(dstYImage, dstYImage);
            IplImage dstXYImage = new IplImage(cvImage.Size, cvImage.Depth, cvImage.NChannels);
            dstXImage.Add(dstYImage, dstXYImage);
            dstXYImage.Pow(dstXYImage, 1/2); //this line not working,output image is black page

            // to calculate gradient orientation, arctan(dy/dx)
            IplImage thetaImage = new IplImage(cvImage.Size, cvImage.Depth, cvImage.NChannels);
            dstYImage.Div(dstXImage, thetaImage); //afterwards need help to calculate arctan 

            using (new CvWindow("SrcImage", cvImage))
            using (new CvWindow("DstXImage", dstXImage))
            using (new CvWindow("DstYImage", dstYImage))
            using (new CvWindow("DstXYImage", dstXYImage))
            using (new CvWindow("thetaImage", thetaImage))
            {
                Cv.WaitKey(0);
            }

如何计算“;Arctan”;以及“;Pow”;IplImage

您可以根据自己的目的使用"cartToPolar"函数。此函数用于计算二维矢量的大小和角度。

震级(I)=sqrt(x(I)^2+y(I)^ 2),角度(I)=atan2(y(I),x(I))[*180/pi]

例如:

IplImage dstXYImage;
IplImage thetaImage;
CartToPolar(dstXImage, dstYImage, dstXYImage, thetaImage, true);