关于OpenCV中常见函数用法总结

  • 关于OpenCV中常见函数用法总结
    • 一 一般Mat的赋值操作
    • 二 求Mat中的最大值以及最小值
    • 三 randn()函数给图像添加高斯噪声
    • 四 mean()函数的用法
    • 五 系统计时器
    • 六 矩阵之间的四则运算:gemm()函数
    • 七 利用OpenCV如何实现在图像指定位置处生成填充的圆形图案
    • 八 将相机中的Image类型转化为Mat类型

关于OpenCV中常见函数用法总结

一 一般Mat的赋值操作

  对于一般的Mat如何进行赋值,这里,只是因为经常会忘记,故而单独记录下来:
示例代码:

Mat mask=(Mat_(3,3)<<1,1,1,1,0,1,1,1,1,1);`

二 求Mat中的最大值以及最小值

示例代码:

#include
#include
using namespace std;
using namespace cv;
void main()
{
    cv::Mat img = (cv::Mat_<char>(3,3)<<1,2,3,4,8,6,5,19,30);
    double MaxValue, MinValue;
    cv::minMaxLoc(img, &MinValue,&MaxValue);
    cout << MinValue << "  " << MaxValue << "   " << endl;

}

  通过以上代码,可以找到Mat中的最大值以及最小值。

三 randn()函数给图像添加高斯噪声

 int length=600int  width=600;
 Mat Imgnoise = Mat::zeros(length, width, CV_8UC1);
 int meanShiftValue = 50;
 randn(Imgnoise, meanShiftValue, sigma);
 Imgnoise = Imgnoise + img - meanShiftValue;

说明:
  此处的meanShiftValue代表的是:噪声均值。sigma:代表标准差。由于 CV_8UC1的像素值范围为(0,255),所以此处像素值不能取负数,故而 meanShiftValue取的值必须要大于0.

四 mean()函数的用法

   之前一直想求一个矩阵中的所用值的均值,可是都没能成功使用mean()来取出最终结果,后来无意中使用Google搜索,成功解决了难题。
   示例代码

Mat MyMat;
cv::Scalar meanValue=cv::mean(MyMat);
float MyMeanValue = meanValue.val[0];
cout<<"MyMat中的所有元素的平均值为:"<<MyMeanValue<

   注:对于容器,此方法也同样适用。

五 系统计时器

代码:

   double t = (double)getTickCount();////开始计时
   //do something......
   t = ((double)getTickCount() - t) / getTickFrequency();//放在函数结尾,计算时间,单位为:秒
   cout << "时间== " << t << endl;//输出时间

注:
   //getTickcount函数:它返回从操作系统启动到当前所经的计时周期数
   //getTickFrequency函数:返回每秒的计时周期数

六 矩阵之间的四则运算:gemm()函数

   OpenCV 中计算矩阵之间的加减乘除:gemm()
   调用方法:

//Tra = C2_mat - Rot*C1_mat
gemm(Rot,C1_mat,-1,C2_mat,1,Tra);

关于OpenCV中常见函数用法总结_第1张图片

七 利用OpenCV如何实现在图像指定位置处生成填充的圆形图案

 例如:如果要生成如下的图案:
关于OpenCV中常见函数用法总结_第2张图片
 代码:

int main()
{
    int radius = 7.5;
    int width = 16;
    Mat circle = CoreAlgorithm::**circleImg**(radius, width);
    Mat image = Mat(1500, 1500, CV_8UC1, Scalar(255));
    Point2i offset = Point2i(0,0);//在图像空间内平移整个特征的平移量
    vector PointVec;
    PointVec.push_back(Point2i(100, 150));//1号
    PointVec.push_back(Point2i(600, 100));//2号
    PointVec.push_back(Point2i(600, 150));//4号
    PointVec.push_back(Point2i(50, 150));//5号
    PointVec.push_back(Point2i(150, 150));//6号
    PointVec.push_back(Point2i(100, 100));//7号
    PointVec.push_back(Point2i(100, 200));//8号
    PointVec.push_back(Point2i(550, 100));//9号
    PointVec.push_back(Point2i(650, 100));//10号
    for (size_t i = 0; i < PointVec.size(); i++)
    {
        if (!CoreAlgorithm::*copyCircleToImg*(circle, offset, PointVec[i], image))
            return 1;
    }
    cv::namedWindow("result", 1);
    cv::imshow("result", image);
    waitKey();
}

 至于其中用到的两个主要函数为:

主要调用的两个函数:
//radius  圆半径,(单位像素);width,生成模板图像大小,正方形
Mat CoreAlgorithm::circleImg(int radius, int width)
{
    Mat img = Mat(width, width, CV_8UC1, Scalar(255));
    for (int i = 0; i < img.rows; i++)
    {
        for (int j = 0; jif (sqrt(pow(i - (width - 1) / 2.0, 2) + pow(j - (width - 1) / 2.0, 2))>radius)
                continue;
            else
                img.at(i, j) = (uchar)0;
        }
    }
    return img;
}
//////////实现将圆重复绘制在指定位置
bool copyCircleToImg(const Mat circleImg, Point2i offset, Point2i location, Mat &writeImg)
{
    location.x += offset.x;
    location.y += offset.y;
    if (location.x - (circleImg.cols - 1) / 2.0<0 || location.y - (circleImg.rows - 1) / 2.0<0 ||
        location.x + (circleImg.cols - 1) / 2.0>writeImg.cols || location.y + (circleImg.rows - 1) / 2.0>writeImg.rows)
        return false;
    Point2i circleImgstart = Point2i(location.x - (circleImg.cols - 1) / 2, location.y - (circleImg.rows - 1)/2);
    for (int i = 0; i < circleImg.rows; i++)
    {
        for (int j = 0; j < circleImg.cols; j++)
        {
            writeImg.at(circleImgstart.y + i, circleImgstart.x + j) = circleImg.at(i, j);
        }
    }
    return true;
}

  主要的实现思想是先生成一个圆形模板,而后将圆形模板在指定位置处不断复制即可。

八 将相机中的Image类型转化为Mat类型

   已知相机中Image.h中的相关属性定义为:

            /**
             * Construct an Image object with the specified arguments.
             *
             * @param rows Rows in the image.
             * @param cols Columns in the image.
             * @param format Pixel format.
             * @param bayerFormat Format of the Bayer tiled raw image.
             */
            Image(
                    unsigned int    rows,
                    unsigned int    cols,
                    PixelFormat     format,
                    BayerTileFormat bayerFormat = NONE );

            /**
             * Get the number of rows in the image.
             *
             * @return The number of rows.
             */
            virtual unsigned int GetRows() const;
            /**
             * Get the number of columns in the image.
             *
             * @return The number of columns.
             */
            virtual unsigned int GetCols() const;

            /**
             * Get a pointer to the data associated with the image. This function
             * is considered unsafe. The pointer returned could be invalidated if
             * the buffer is resized or released. The pointer may also be
             * invalidated if the Image object is passed to
             * Camera::RetrieveBuffer(). It is recommended that a Image::DeepCopy()
             * be performed if a seperate copy of the Image data is required
             * for further processing.
             *
             * @return A pointer to the image data.
             */
            virtual unsigned char* GetData();

   而Mat属性为:

  Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP);

   因而由image转化为mat的语句如下:

    Image img;
    cv::Mat image = cv::Mat(img.GetRows(), img.GetCols(), CV_8UC1, (void*)img.GetData());

  个人公众号:

这里写图片描述

你可能感兴趣的:(3DCVer)