简体   繁体   English

Opencv使用cv :: Mat创建新图像

[英]Opencv create new image using cv::Mat

I'm new to opencv and i'm trying on some sample codes. 我是opencv的新手,我正在尝试一些示例代码。

in one code, Mat gr(row1,col1,CV_8UC1,scalar(0)); int x = gr.at<uchar> (row,col); 在一个代码中, Mat gr(row1,col1,CV_8UC1,scalar(0)); int x = gr.at<uchar> (row,col); Mat gr(row1,col1,CV_8UC1,scalar(0)); int x = gr.at<uchar> (row,col);

And in another one, 在另一个,

Mat grHistrogram(301,260,CV_8UC1,Scalar(0,0,0));
line(grHistrogram,pt1,pt2,Scalar(255,255,255),1,8,0);

Now my question is if i used scalar(0) instead of scalar(0,0,0) in second code, The code doesn't work. 现在我的问题是,如果我在第二个代码中使用标量(0)而不是标量(0,0,0),代码不起作用。 1.Why this happening since, Both create a Mat image structure. 为什么会发生这种情况,两者都会创建一个Mat图像结构。 2.what is the purpose of const cv:Scalar &_s. 2. const cv的目的是什么:Scalar&_s。

I search the Documentaion from Opencv site (opencv.pdf,opencv2refman.pdf) and Oreilly's Opencv book. 我从Opencv网站(opencv.pdf,opencv2refman.pdf)和Oreilly的Opencv书中搜索Documentaion。 But couldn't find a explained answer. 但找不到解释的答案。

I think i'm using the Mat(int _rows,int _cols,int _type,const cv:Scalar &_s) struct. 我想我正在使用Mat(int _rows,int _cols,int _type,const cv:Scalar&_s)struct。

First, you need the following information to create the image: 首先,您需要以下信息来创建图像:

  1. Width: 301 pixels 宽度:301像素
  2. Height: 260 pixels 高度:260像素
  3. Each pixel value (intensity) is 0 ~ 255: an 8-bit unsigned integer 每个像素值(强度)为0~255:8位无符号整数
  4. Supports all RGB colors: 3 channels 支持所有RGB颜色:3个通道
  5. Initial color: black = (B, G, R) = (0, 0, 0) 初始颜色:黑色=(B,G,R)=(0,0,0)

You can create the Image using cv::Mat : 您可以使用cv::Mat创建Image:

Mat grHistogram(260, 301, CV_8UC3, Scalar(0, 0, 0));

The 8U means the 8 -bit U signed integer, C3 means 3 C hannels for RGB color, and Scalar(0, 0, 0) is the initial value for each pixel. 8U表示8U有符号整数, C3表示RGB颜色为3 C hannels, Scalar(0, 0, 0)为每个像素的初始值。 Similarly, 同样的,

line(grHistrogram,pt1,pt2,Scalar(255,255,255),1,8,0);

is to draw a line on grHistogram from point pt1 to point pt2 . 是在grHistogram从点pt1到点pt2绘制一条线。 The color of line is white (255, 255, 255) with 1-pixel thickness, 8-connected line, and 0-shift. 线的颜色为白色(255,255,255),1像素厚度,8连线和0移位。

Sometimes you don't need a RGB-color image, but a simple grayscale image. 有时您不需要RGB彩色图像,而是需要简单的灰度图像。 That is, use one channel instead of three. 也就是说,使用一个通道而不是三个通道。 The type can be changed to CV_8UC1 and you only need to specify the intensity for one channel, Scalar(0) for example. 类型可以更改为CV_8UC1 ,您只需要指定一个通道的强度,例如Scalar(0)

Back to your problem, 回到你的问题,

Why this happening since, both create a Mat image structure? 为什么会发生这种情况,都会创建一个Mat图像结构?

Because you need to specify the type of the Mat . 因为您需要指定Mat的类型。 Is it a color image CV_8UC3 or a grayscale image CV_8UC1 ? 它是彩色图像CV_8UC3还是灰度图像CV_8UC1 They are different. 他们是不同的。 Your program may not work as you think if you use Scalar(255) on a CV_8UC3 image. 如果在CV_8UC3图像上使用Scalar(255) ,您的程序可能无法正常工作。

What is the purpose of const cv:Scalar &_s ? const cv的目的是什么:Scalar&_s?

cv::Scalar is use to specify the intensity value for each pixel. cv::Scalar用于指定每个像素的强度值。 For example, Scalar(255, 0, 0) is blue and Scalar(0, 0, 0) is black if type is CV_8UC3 . 例如,如果类型为CV_8UC3 ,则Scalar(255, 0, 0)为蓝色, Scalar(0, 0, 0)为黑色。 Or Scalar(0) is black if it's a CV_8UC1 grayscale image. 或者如果它是CV_8UC1灰度图像,则Scalar(0)为黑色。 Avoid mixing them together. 避免将它们混合在一起

You can create single channel image or multi channel image. 您可以创建单通道图像或多通道图像。

creating single channel image : Mat img(500, 1000, CV_8UC1, Scalar(70)); 创建单通道图像: Mat img(500, 1000, CV_8UC1, Scalar(70));

creating multi channel image : Mat img1(500, 1000, CV_8UC3, Scalar(10, 100, 150)); 创建多通道图像: Mat img1(500, 1000, CV_8UC3, Scalar(10, 100, 150));

you can see more example and detail from following page. 您可以从以下页面中查看更多示例和详细信息。 https://progtpoint.blogspot.com/2017/01/tutorial-3-create-image.html https://progtpoint.blogspot.com/2017/01/tutorial-3-create-image.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM