In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what are the basic operations of OpenCV images". In the daily operation, I believe that many people have doubts about the basic operation of OpenCV images. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the basic operations of OpenCV images?" Next, please follow the editor to study!
Foreword:
The main function of opencv is for image processing, so the concept of image runs through the whole opencv, and the core class related to it is Mat.
Pixels:
When the picture size is in pixels, each centimeter is equal to 28 pixels, for example, a picture with a length of 1515 centimeters is equal to the length of 420420 pixels. The number of different colors a pixel can express depends on bits per pixel (BPP).
Grayscale images: 8bpp=2 to the power of 8 = 256colors
High color: 16bpp=2 to the power of 16 = 65536 color
True color: 24bpps=2 to the 24th power = 16777216 color.
Image resolution:
Image resolution is the total number of pixels of the image, because the image is usually represented by a matrix, so the resolution is commonly used, mn said, note: n represents the number of rows (representing the number of pixels contained in a column), m represents the number of pixels contained in a row.
640X480 means that the length and width of the image are 640 and 480, respectively, and the total pixel is 640X480=307200 (300000 resolution in the camera).
800X600 means that the length and width of the image are 800 and 600 respectively, and the total pixel is 800X600=480000 (the 500000 resolution in the camera).
Image and matrix
An image is made up of pixels, which are actually points with coordinate location and color information. We think of the picture as consisting of several rows and columns of points. in reality, there is a RGB color system, and we can represent the point An of any point in the graph (in row m, column n) as
A [m < n] = [blue,green,red]
Parameter interpretation
M | the m line of point An in the image
N | the nth column of point An in the image
Blue | the first value of blue, tricolor (RGB)
Green | indicates green, the second value of tricolor (RGB)
Red | indicates red, the first value of RGB.
The corresponding luminance of each point can be understood as the value of rgb, unsigned 8-digit 3-D, then a pixel is a 3-dimensional array, corresponding to the value of RGB, and the data type in OpenCV is: cV_8u3C.
Suppose Mx NMagol Lij represents the j th row j column, corresponding to the above figure is M = 300 Magi N = 200.
Suppose Mx NMagol Lij represents the j th row j column, corresponding to the above figure is M = 300 Magi N = 200.
Reading and writing of pixel values
Many times, we need to read a pixel value, or set a pixel value; more often, we need to traverse all the pixels in the entire image. OpenCV provides a variety of methods to implement image traversal.
Method 1: at function
Cv::Mat grayim (600,800, CV_8UC1); / / iterate through all pixels and set the pixel value for (int I = 0; I
< grayim.rows; ++i) { for( int j = 0; j < grayim.cols; ++j ) { grayim.at(i,j) = (i+j)%255; } } imshow("grayim",grayim); cv::Mat colorim(600, 800, CV_8UC3); // 遍历所有像素,并设置像素值 for( int i = 0; i < colorim.rows; ++i) { for( int j = 0; j < colorim.cols; ++j ) { cv::Vec3b pixel; // 注意:opencv通道顺序,BGR,非RGB pixel[0] = i%255; // Blue pixel[1] = j%255; // Green pixel[2] = 0; // Red colorim.at(i,j) = pixel; } } imshow("colorim",colorim); waitKey(); 方法一:使用数据指针 cv::Mat grayim(600, 800, CV_8UC1); cv::Mat colorim(600, 800, CV_8UC3); //遍历所有像素,并设置像素值 for( int i = 0; i < grayim.rows; ++i) { //获取第 i 行首像素指针 uchar * p = grayim.ptr(i); //对第 i 行的每个像素(byte)操作 for( int j = 0; j < grayim.cols; ++j ) p[j] = (i+j)%255; } //遍历所有像素,并设置像素值 for( int i = 0; i < colorim.rows; ++i) { //获取第 i 行首像素指针 cv::Vec3b * p = colorim.ptr(i); for( int j = 0; j < colorim.cols; ++j ) { p[j][0] = i%255; //Blue p[j][1] = j%255; //Green p[j][2] = 0; //Red } } imshow("grayim",grayim); imshow("colorim",colorim); 实验效果 图像局部操作 选择单行/单列 示例:A矩阵的第i行,将这一行的所有元素都乘以2,然后赋值给第j行 A.row(j)= A.row(i)*2; 选择多行/多列 Range是OpencV中新增的类,该类有两个关键变量star和end。Range对象可以用来表示矩阵的多个连续的行或者多个连续的列。其表示的范围为从start到end,包含start。 // 创建一个单位阵Mat A = Mat::eye(10, 10, CV_32S);// 提取第 1 到 3 列(不包括 3)Mat B = A(Range::all(), Range(1, 3));// 提取B的第 5 至 9 行(不包括 9)// 其实等价于C = A(Range(5, 9), Range(1, 3))Mat C = B(Range(5, 9), Range::all());选择指定区域 图像中提取感兴趣区域(Region of interest)有两种方法: 方法-:使用构造函数 //创建宽度为 320,高度为 240 的 3 通道图像Mat img(Size(320, 240), CV_8UC3);//roi 是表示 img 中 Rect(10, 10, 100, 100)区域的对象Mat roi(img, Rect(10, 10, 100, 100)); 方法二:使用括号运算符 Mat roi2 = img(Rect(10, 10, 100, 100));//当然也可以使用Range对象来定义感兴趣区域,如下:// 用括号运算符Mat roi3 = img(Range(10, 100), Range(10, 100)); // 用构造函数Mat roi4(img, Range(10, 100), Range(10, 100));取对角线元素 矩阵的对角线元素可以使用cv::Mat就的diag()函数获取: Mat Mat::diag(int d) const 1.当d=0时,表示取主对角线; 当参数d>0 Yes, it means to take the secondary diagonal below the primary diagonal
two。 When dong1, it indicates that the element is below the main diagonal and close to the main polygon
3. When parameter d
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.