OpenCV学习 第一天
下面就来一波 图文并茂加代码
第一步是加载图片
加载图片使用的是 imread() 第一个参数就是图片的路径,第二参数是读取的方式,也就是一个枚举类型
例如 灰度 IMREAD_GRAYSCALE = 0
Mat src = imread("G:/kobe.jpeg", IMREAD_GRAYSCALE);//load the image
@param filename Name of file to be loaded.
@param flags Flag that can take values of cv::ImreadModes
CV_EXPORTS_W Mat imread( const String& filename, int flags = IMREAD_COLOR );
enum ImreadModes {
IMREAD_UNCHANGED = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
IMREAD_GRAYSCALE = 0, //!< If set, always convert image to the single channel grayscale image.
IMREAD_COLOR = 1, //!< If set, always convert image to the 3 channel BGR color image.
IMREAD_ANYDEPTH = 2, //!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
IMREAD_ANYCOLOR = 4, //!< If set, the image is read in any possible color format.
IMREAD_LOAD_GDAL = 8, //!< If set, use the gdal driver for loading the image.
IMREAD_REDUCED_GRAYSCALE_2 = 16, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
IMREAD_REDUCED_COLOR_2 = 17, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
IMREAD_REDUCED_GRAYSCALE_4 = 32, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
IMREAD_REDUCED_COLOR_4 = 33, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
IMREAD_REDUCED_GRAYSCALE_8 = 64, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
IMREAD_REDUCED_COLOR_8 = 65 //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.
};
第二步是处理图片
所谓掩膜其实就是一个矩阵,然后根据这个矩阵重新计算图片中像素的值。
掩膜主要有以下用途:
提取感兴趣区,用预先制作的感兴趣区掩模与待处理图像相乘,得到感兴趣区图像,感兴趣区内图像值保持不变,而区外图像值都为0。
屏蔽作用,用掩模对图像上某些区域作屏蔽,使其不参加处理或不参加处理参数的计算,或仅对屏蔽区作处理或统计。
结构特征提取,用相似性变量或图像匹配方法检测和提取图像中与掩模相似的结构特征。
特殊形状图像的制作
增加对比度,使用掩膜
第一种实现
int cols = (src.cols-1) * src.channels()
int rows = src.rows;
int offset = src.channels();//get the channel
res = Mat::zeros(src.size(), src.type());//reset the picture array and point to the same type and size
//I(i,j) = 5*I(i,j)-[I(i-1,j)+I(i+1,j)+I(i,j-1)+I(i,j+1)]
//increased the contract
//ignore the first bit
for (int row = 1; row < rows-1; row++)
{
const uchar* current = src.ptr<uchar>(row - 1);//return the src row-1 address
const uchar* previous = src.ptr<uchar>(row);//return the src row address
const uchar* next = src.ptr<uchar>(row + 1);//return the src row+1 address
uchar* output = res.ptr<uchar>(row);//return the res row address
for (int col = offset; col < cols; col++)
{
output[col] = saturate_cast<uchar>(5 * current[col] - (current[col- offset] + current[col + offset] + previous[col]+ next[col]));
}
}
第二种实现 使用OpenCV自带的的 filter2D()
Mat other;
Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D(src,other,src.depth(),kernel);
filter2D()的参数列表
@param src input image.
@param dst output image of the same size and the same number of channels as src.
@param ddepth desired depth of the destination image, see @ref filter_depths "combinations"
@param kernel convolution kernel (or rather a correlation kernel), a single-channel floating point
matrix; if you want to apply different kernels to different channels, split the image into
separate color planes using split and process them individually.
@param anchor anchor of the kernel that indicates the relative position of a filtered point within
the kernel; the anchor should lie within the kernel; default value (-1,-1) means that the anchor
is at the kernel center.
@param delta optional value added to the filtered pixels before storing them in dst.
@param borderType pixel extrapolation method, see cv::BorderTypes
@sa sepFilter2D, dft, matchTemplate
*/
CV_EXPORTS_W void filter2D( InputArray src, OutputArray dst, int ddepth,
InputArray kernel, Point anchor = Point(-1,-1),
double delta = 0, int borderType = BORDER_DEFAULT );
第三步是显示图片
调整显示窗口使用 namedWindow() ,第一个参数是窗口名字,第二个参数窗口调整模式,默认是自动
@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);
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.
};
显示图片使用的是 imshow()
@param winname Name of the window.
@param mat Image to be shown.
*/
CV_EXPORTS_W void imshow(const String& winname, InputArray mat);
第四步是保存图片
保存图片使用的函数是 imwrite()
使用方法如下:
@param filename Name of the file.
@param img Image to be saved.
@param params Format-specific parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ... .) see cv::ImwriteFlags
CV_EXPORTS_W bool imwrite( const String& filename, InputArray img,
const std::vector<int>& params = std::vector<int>());
其中这里使用saturate cast<uchar>
使用函数saturate_cast(像素值) 这个函数的作用就是确保RGB的值在0~255之间。
template<> inline uchar saturate_cast<uchar>(int v)
{ return (uchar)((unsigned)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); }
代码下载链接:https://download.csdn.net/download/weixin_38452632/10683606