Opencv function

一、randu()生成单个均匀分布的随机数或随机数数组。

/** @brief Generates a single uniformly-distributed random number or an array of random numbers.

Non-template variant of the function fills the matrix dst with uniformly-distributed
random numbers from the specified range:
\f[\texttt{low} _c  \leq \texttt{dst} (I)_c <  \texttt{high} _c\f]
@param dst output array of random numbers; the array must be pre-allocated.
@param low inclusive lower boundary of the generated random numbers.生成的随机数的下限(包括下限)。
@param high exclusive upper boundary of the generated random numbers.
@sa RNG, randn, theRNG,生成的随机数的高上限。
*/
CV_EXPORTS_W void randu(InputOutputArray dst, InputArray low, InputArray high);
Mat r(3, 3, CV_8UC3);
	randu(r, Scalar::all(0), Scalar::all(255));

二、randn()用正态分布的随机数填充数组。

Fills the array with normally distributed random numbers.

The function cv::randn fills the matrix dst with normally distributed random numbers with the specified
mean vector and the standard deviation matrix. The generated random numbers are clipped to fit the
value range of the output array data type.
@param dst output array of random numbers; the array must be pre-allocated and have 1 to 4 channels.
@param mean mean value (expectation) of the generated random numbers.
@param stddev standard deviation of the generated random numbers; it can be either a vector (in
which case a diagonal standard deviation matrix is assumed) or a square matrix.
@sa RNG, randu
*/
CV_EXPORTS_W void randn(InputOutputArray dst, InputArray mean, InputArray stddev);

三、namewindow(),设置窗口的格式;

The function namedWindow creates a window that can be used as a placeholder for images and
trackbars. Created windows are referred to by their names.

If a window with the same name already exists, the function does nothing.

You can call cv::destroyWindow or cv::destroyAllWindows to close the window and de-allocate any associated
memory usage. For a simple program, you do not really have to call these functions because all the
resources and windows of the application are closed automatically by the operating system upon exit.

@note

Qt backend supports additional flags:
 -   **WINDOW_NORMAL or WINDOW_AUTOSIZE:** WINDOW_NORMAL enables you to resize the
     window, whereas WINDOW_AUTOSIZE adjusts automatically the window size to fit the
     displayed image (see imshow ), and you cannot change the window size manually.
 -   **WINDOW_FREERATIO or WINDOW_KEEPRATIO:** WINDOW_FREERATIO adjusts the image
     with no respect to its ratio, whereas WINDOW_KEEPRATIO keeps the image ratio.
 -   **WINDOW_GUI_NORMAL or WINDOW_GUI_EXPANDED:** WINDOW_GUI_NORMAL is the old way to draw the window
     without statusbar and toolbar, whereas WINDOW_GUI_EXPANDED is a new enhanced GUI.
By default, flags == WINDOW_AUTOSIZE | WINDOW_KEEPRATIO | WINDOW_GUI_EXPANDED

@param winname Name of the window in the window caption that may be used as a window identifier.
@param flags Flags of the window. The supported flags are: (cv::WindowFlags)
 */
CV_EXPORTS_W void namedWindow(const String& winname, int flags = WINDOW_AUTOSIZE);
/*---------------------------------------------flags --------------------------------------------------*/
enum WindowFlags {
       WINDOW_NORMAL     = 0x00000000, //!< the user can resize the window (no constraint) / also use to switch a fullscreen window to a normal size.
       WINDOW_AUTOSIZE   = 0x00000001, //!< the user cannot resize the window, the size is constrainted by the image displayed.
       WINDOW_OPENGL     = 0x00001000, //!< window with opengl support.

       WINDOW_FULLSCREEN = 1,          //!< change the window to fullscreen.
       WINDOW_FREERATIO  = 0x00000100, //!< the image expends as much as it can (no ratio constraint).
       WINDOW_KEEPRATIO  = 0x00000000, //!< the ratio of the image is respected.
       WINDOW_GUI_EXPANDED=0x00000000, //!< status bar and tool bar
       WINDOW_GUI_NORMAL = 0x00000010, //!< old fashious way
    };

四、指定窗口大小:resizewindow()

/** @brief Resizes window to the specified size

@note

-   The specified window size is for the image area. Toolbars are not counted.
-   Only windows created without cv::WINDOW_AUTOSIZE flag can be resized.

@param winname Window name.//要改变的窗口名字;
@param width The new window width.
@param height The new window height.
 */
CV_EXPORTS_W void resizeWindow(const String& winname, int width, int height);
/** @overload
@param winname Window name.
@param size The new window size.
*/**重载resizeWindow**
CV_EXPORTS_W void resizeWindow(const String& winname, const cv::Size& size);

五、移动窗口位置moveWindow()

/** @brief Moves window to the specified position

@param winname Name of the window.
@param x The new x-coordinate of the window.**窗口的新坐标x值,屏幕的左上角为原点**;
@param y The new y-coordinate of the window.**窗口的新坐标y值,屏幕的左上角为原点**
 */
CV_EXPORTS_W void moveWindow(const String& winname, int x, int y);

六、防止数据溢出saturate_cast(data)

saturate_cast(data)主要是为了防止数据溢出;

#include<iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
	cout << sizeof(short) << endl;//两个字节,最大正数,(2^16)/2-1=32767;最小:-32767
	cout << sizeof(uchar) << endl;//1个字节,无符号,最大值(2^4)-1=255;最小0;
	uchar a = saturate_cast<uchar>(-100); // a = 0 (UCHAR_MIN)
	short b = saturate_cast<short>(33333.33333); // b = 32767 (SHRT_MAX)
}

将给定的数据限制在其数据类型的取值范围内(data>max;取值为Max,data<min,取值min)

七、cvRound()将浮点数四舍五入到最接近的整数

八、cvFlood()将浮点数向下取整;

九、cvCeil()将浮点数向上取整

将浮点数四舍五入至不大于原始整数的最接近整数。

#include<iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
	float a = 3.5;

	float b = cvRound(a);//四舍五入取整数
	float c = cvFloor(a);//向下取整数
	float d = cvCeil(a);//向上取整数

	cout << b << endl;
	cout << c << endl;
	cout << d << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值