当前位置:网站首页>Opencv learning notes -day3 (mat object and creation related operations mat:: clone(), mat:: copyto(), mat:: zeros(), mat:: ones(), scalar()...)
Opencv learning notes -day3 (mat object and creation related operations mat:: clone(), mat:: copyto(), mat:: zeros(), mat:: ones(), scalar()...)
2022-06-30 08:47:00 【Chasing foot dream】
OpenCV Learning notes
day3-Mat Object and creation
Mat The basic operation of class
Mat class :Mat::clone(),Mat::copyTo(),Mat::zeros(),Mat::ones(),Scalar()
One , Copy
function :
Shallow copy - Do not copy data but create matrix headers , Data sharing
example 1
Mat a = imread("1.jpg");
clone It's a complete deep copy , Apply for new space in memory
// Definition
Mat Mat::clone() const
{
Mat m;
copyTo(m);
return m;
}
example 2
Mat m1 = image.clone();
//clone It's a complete deep copy , Apply for new space in memory , And image Independent
// Definition
void GpuMat::copyTo(OutputArray dst, InputArray mask) const
{
copyTo(dst, mask, Stream::Null());
}
OpenCV in image.copyTo() There are two forms :
1、image.copyTo(imageROI), The effect is to turn image Paste your content into imageROI;
2、image.copyTo(imageROI,mask), The effect is to turn mask and image After overlapping, put mask The pixel value in is 0(black) The point corresponding to image The point in the becomes transparent , And keep other points .
mask: The mask . The mask is made by 0 and 1 Make up a binary image . When a mask is applied to a function ,1 Value area is processed , Shielded 0 The value area is not included in the calculation . By the specified data value 、 Data range 、 Finite or infinite value 、 Region of interest and annotation file to define image mask , Any combination of the above options can also be used as input to establish a mask .
example 3
image.copyTo(m2);// hold image Copy the contents of to m2 in
Two , Create a blank image
function :
Mat::zeros() Returns an array of zeros of the specified size and type
Mat::ones() Returns the specified size and type 1 Array of
// Definition
static MatExpr zeros(Size size, int type);
static MatExpr ones(Size size, int type);
type Can be any predefined type , The structure of the predefined types is as follows :
CV_<bit_depth>(S|U|F)C<number_of_channels>
1)bit_depth— Number of bits — representative 8bite,16bites,32bites,64bites— Let's give you an example – for instance , Such as
If you create a store now – Grayscale image of Mat object , The size of this image is wide 100, high 100, that , Now this grayscale image has 10000 Pixels , The memory space occupied by each pixel is 8bite,8 position – So it corresponds to CV_8
2)S|U|F
S– representative —signed int— There's symbolic plastic
U– representative –unsigned int– Unsigned shaping
F– representative –float--------- Single precision floating point
3)C<number_of_channels>
representative — The number of channels in a picture , such as :
1– Grayscale picture –grayImg— yes – Single channel image
2–RGB Color images --------- yes –3 Channel image
3– belt Alph The tunnel RGB Images – yes --4 Channel image
example 4
Mat m3 = Mat::zeros(Size(8, 8), CV_8UC1);
// Create a matrix ;CV_8UC1 8-bit unsigned single channel CV_8UC1 single channel
// Initialize to 1 At this time, only single channel can be selected
//if Use 3 If the channel looks at the complete results below, there will be errors - Only the first channel is initialized to 1
Mat m3 = Mat::ones(Size(8, 8), CV_8UC1);//8*8 8 Bit unsigned single channel
// Initialize to 1, Use 3 passageway
Mat m3 = Mat::ones(Size(8, 8), CV_8UC3);//8*8 8 Bit unsigned 3 passageway
example 5
std::cout << "width: " << m3.cols << std::endl;// Output width
std::cout << "height: " <<m3.rows << std::endl;// Output width
std::cout << "channels: " << m3.channels()<< std::endl;// Number of output channels
Code
quickopencv.h
#pragma once
#include <opencv2\highgui.hpp>
#include <opencv2\imgproc.hpp>
using namespace cv;
// Defining classes
class QuickDemo{
public:
void colorSpace_Demo(Mat &image);// Color space conversion function 2021-12-24
void mat_creation_demo(Mat &image);//Mat Object and creation 2021-12-27
};
OpencvTest.cpp
#include <iostream>
#include <opencv2\highgui.hpp>
#include <opencv2\imgproc.hpp>
#include<quickopencv.h>
using namespace cv;
using namespace std;
int main()
{
Mat scr = imread("D:\\ Study \\OpenCV Study \\pictures\\image\\1.jpg");// Open a picture
if (!scr.data == 1)// Sentenced to empty
return -1;
namedWindow(" window 1", WINDOW_NORMAL);// establish WINDOW_FREERATIO window
imshow(" window 1",scr);// Show in the created window
QuickDemo qd;
//qd.colorSpace_Demo(scr);// Color conversion
//waitKey(0);// Time delay 0-> Constant delay 1-> Time delay 1ms
qd.mat_creation_demo(scr);//Mat Class
waitKey(0);
return 0;
QuickDemo.cpp
#include <opencv2\highgui.hpp>
#include <opencv2\imgproc.hpp>
#include<quickopencv.h>
#include <iostream>
// The name of the function :void QuickDemo::mat_creation_demo(Mat &image)
// Functional specifications :Mat Object and creation
// Parameter description :Mat &image
// The function returns : nothing
// Modification time :2021 year 12 month 29 Japan
// To prepare notes :
void QuickDemo::mat_creation_demo(Mat &image)
{
// Create a blank image
//8*8 8 Bit unsigned 1 passageway All initialized to 0
Mat m3 = Mat::zeros(Size(8, 8), CV_8UC1);// matrix ;size:8*8 CV_8UC1: 8-bit unsigned single channel Initialize to 0--- Show results 1
//Mat m3 = Mat::zeros(Size(8, 8), CV_8UC3);//CV_8UC3 Octet unsigned 3 passageway -- Show results 2
// Initialize to 1 At this time, only single channel can be selected if Use 3 The channel will have errors in the following results
//Mat m3 = Mat::ones(Size(8, 8), CV_8UC1);//szie:8*8 CV_8UC1:8 Bit unsigned single channel Initialize to 0-- Show results 3
//Mat m3 = Mat::ones(Size(8, 8), CV_8UC3);//size:8*8 CV_8UC3:8 Bit unsigned 3 passageway Initialize to 1-- Show results 4 Only the first channel can be initialized
std::cout << "width: " << m3.cols << std::endl;// Output width
std::cout << "height: " <<m3.rows << std::endl;// Output width
std::cout << "channels: " << m3.channels()<< std::endl;// Number of output channels
std::cout << m3 << std::endl;// Output m3
}
Show results 1
Show results 2
Show results 3
Show results 4- Only the first channel can be initialized 
3、 ... and , Assignment operation
function :
// Definition
Scalar_<_Tp>::Scalar_(_Tp v0, _Tp v1, _Tp v2, _Tp v3)
{
this->val[0] = v0;
this->val[1] = v1;
this->val[2] = v2;
this->val[3] = v3;
}
example 6
m4 = Scalar(127,127,127);// to 3 Each channel is assigned a value grayscale
//127 Is the grayscale value
m4 = Scalar(255, 0, 0);
//300*300 3 The grayscale of the channel (127,127,127),
// Blue (255,0,0), green (0,255,0), yellow (0,0,255)
Code
quickopencv.h
#pragma once
#include <opencv2\highgui.hpp>
#include <opencv2\imgproc.hpp>
using namespace cv;
// Defining classes
class QuickDemo{
public:
void colorSpace_Demo(Mat &image);// Color space conversion function 2021-12-24
void mat_creation_demo(Mat &image);//Mat Object and creation 2021-12-27
};
OpencvTest.cpp
#include <iostream>
#include <opencv2\highgui.hpp>
#include <opencv2\imgproc.hpp>
#include<quickopencv.h>
using namespace cv;
using namespace std;
int main()
{
Mat scr = imread("D:\\ Study \\OpenCV Study \\pictures\\image\\1.jpg");// Open a picture
if (!scr.data == 1)// Sentenced to empty
return -1;
namedWindow(" window 1", WINDOW_NORMAL);// establish WINDOW_FREERATIO window
imshow(" window 1",scr);// Show in the created window
QuickDemo qd;
//qd.colorSpace_Demo(scr);// Color conversion
//waitKey(0);// Time delay 0-> Constant delay 1-> Time delay 1ms
qd.mat_creation_demo(scr);//Mat Class
waitKey(0);
return 0;
QuickDemo.cpp
void QuickDemo::mat_creation_demo(Mat &image)
{
// Assignment operation
Mat m4=Mat::zeros(Size(300,300),CV_8UC3);
//m4 = 127;// Only the first channel can be assigned at this time 127 It's grayscale
//-- Show results 5
//Scalar() function
m4 = Scalar(127,127,127);// to 3 Each channel is assigned a value grayscale -- result 6
//m4 = Scalar(255, 0, 0);//300*300 3 The grayscale of the channel (127,127,127), Blue (255,0,0), green (0,255,0), yellow (0,0,255)-- Show results 7
imshow("m4", m4);// It can be used imshow The output shows
//Mat m5 = m4;//m5 Yes m4 //***
//m5 = Scalar(0, 255, 255);// At this time, the m4 The color of is yellow
//imshow("m4",m4);//m4 The output is displayed in yellow -- Show results 8
std::cout << "width: " << m4.cols << std::endl;// Output width
std::cout << "height: " <<m4.rows << std::endl;// Output width
std::cout << "channels: " << m4.channels()<< std::endl;// Number of output channels
std::cout << m4 << std::endl;// Output m
}
result 5– Only the first channel is assigned 
result 6.1– Use Scalar(),3 Each channel is assigned a value 
result 6.2–300*300 3 Grayscale image of the channel 
Show results 7.1
Show results 7.2
Show results 7.3
Show results 8
边栏推荐
- Mmcv expanding CUDA operator beginner level chapter
- 一次cpu 跌底排查
- Flink Exception -- No ExecutorFactory found to execute the application
- Deploy the cow like customer network project on the ECS
- Flink sql -- No factory implements ‘org.apache.flink.table.delegation.ExecutorFactory‘.
- [nvme2.0b 14-7] set features (Part 1)
- Tidb v6.0.0 (DMR): initial test of cache table - tidb Book rush
- VIM from dislike to dependence (21) -- cross file search
- Flink SQL custom connector
- How to format an UTC date to use the Z (Zulu) zone designator in php?
猜你喜欢

el-input 限制只能输数字

Build a docker image of Henkel database from 0

QT connection to Shentong database

Tidb 6.0: making Tso more efficient tidb Book rush

C accesses mongodb and performs CRUD operations

增强for循环的增删操作 & 迭代器删除集合元素

Interference source current spectrum test of current probe

Detectron2 source code reading 3-- encapsulating dataset with mapper

C#访问MongoDB并执行CRUD操作

电流探头电路分析
随机推荐
C accesses mongodb and performs CRUD operations
codeforces每日5题(均1700)-第三天
文件上传 upload 组件 on-success 事件,添加自定义参数
Pytorch BERT
layer.open 当传值为数组或值太长时处理方法
Detectron2 source code reading 2--- using the configurable decorator to build the dataloader
Redis design and Implementation (I) | data structure & object
Vite project require syntax compatibility problem solving require is not defined
Interference source current spectrum test of current probe
Icon resources
Viteproject require Syntax Compatibility Problem Solving require is not defined
[untitled]
【NVMe2.0b 14】NVMe Admin Command Set
Redis design and Implementation (V) | sentinel sentry
Flink SQL 自定义 Connector
Understanding society at the age of 14 - reading notes on "happiness at work"
Bind threads to run on a specific CPU logical kernel
Design specification for smart speakers v1.0
c#获取当前的时间戳
技术管理进阶——管理者如何进行梯队设计及建设