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

时间:2021-10-15 21:22:50

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 (行,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. 1.Why this happening since, Both create a Mat image structure. 2.what is the purpose of const cv:Scalar &_s.

现在我的问题是如果我用标量(0)而不是标量(0,0)在第二段代码中,代码就不能工作了。1。为什么会这样,两者都创建了一个Mat图像结构。2。const cv的目的是什么:标量&_s。

I search the Documentaion from Opencv site (opencv.pdf,opencv2refman.pdf) and Oreilly's Opencv book. But couldn't find a explained answer.

我从Opencv网站(Opencv .pdf,opencv2refman.pdf)和Oreilly的Opencv书中搜索文档。但找不到一个可以解释的答案。

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)结构。

2 个解决方案

#1


12  

First, you need the following information to create the image:

首先,创建图像需要以下信息:

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

You can create the Image using cv::Mat:

您可以使用cv:::Mat:

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

The 8U means the 8-bit Usigned integer, C3 means 3 Channels for RGB color, and Scalar(0, 0, 0) is the initial value for each pixel. Similarly,

8U表示8位上带符号整数,C3表示RGB颜色的3个通道,标量(0,0,0)表示每个像素的初始值。同样的,

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

is to draw a line on grHistogram from point pt1 to point pt2. The color of line is white (255, 255, 255) with 1-pixel thickness, 8-connected line, and 0-shift.

从点pt1到点pt2,在grHistogram上画一条线。线的颜色为白色(255,255,255),厚度1像素,8连通线,0移位。

Sometimes you don't need a RGB-color image, but a simple grayscale image. 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.

有时你不需要一个rgb颜色的图像,而是一个简单的灰度图像。也就是说,使用一个频道而不是三个频道。类型可以更改为CV_8UC1,您只需要指定一个通道的强度,例如标量(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. Is it a color image CV_8UC3 or a grayscale image CV_8UC1? They are different. Your program may not work as you think if you use Scalar(255) on a CV_8UC3 image.

因为您需要指定Mat的类型。它是一个彩色图像CV_8UC3还是灰度图像CV_8UC1?它们是不同的。如果在CV_8UC3映像上使用标量(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. For example, Scalar(255, 0, 0) is blue and Scalar(0, 0, 0) is black if type is CV_8UC3. Or Scalar(0) is black if it's a CV_8UC1 grayscale image. Avoid mixing them together.

标量用于指定每个像素的强度值。例如,如果类型为CV_8UC3,那么标量(255,0,0)是蓝色的,标量(0,0)是黑色的。或者标量(0)是黑色的,如果是CV_8UC1灰度图像。避免混合在一起。

#2


1  

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,标量(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

#1


12  

First, you need the following information to create the image:

首先,创建图像需要以下信息:

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

You can create the Image using cv::Mat:

您可以使用cv:::Mat:

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

The 8U means the 8-bit Usigned integer, C3 means 3 Channels for RGB color, and Scalar(0, 0, 0) is the initial value for each pixel. Similarly,

8U表示8位上带符号整数,C3表示RGB颜色的3个通道,标量(0,0,0)表示每个像素的初始值。同样的,

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

is to draw a line on grHistogram from point pt1 to point pt2. The color of line is white (255, 255, 255) with 1-pixel thickness, 8-connected line, and 0-shift.

从点pt1到点pt2,在grHistogram上画一条线。线的颜色为白色(255,255,255),厚度1像素,8连通线,0移位。

Sometimes you don't need a RGB-color image, but a simple grayscale image. 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.

有时你不需要一个rgb颜色的图像,而是一个简单的灰度图像。也就是说,使用一个频道而不是三个频道。类型可以更改为CV_8UC1,您只需要指定一个通道的强度,例如标量(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. Is it a color image CV_8UC3 or a grayscale image CV_8UC1? They are different. Your program may not work as you think if you use Scalar(255) on a CV_8UC3 image.

因为您需要指定Mat的类型。它是一个彩色图像CV_8UC3还是灰度图像CV_8UC1?它们是不同的。如果在CV_8UC3映像上使用标量(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. For example, Scalar(255, 0, 0) is blue and Scalar(0, 0, 0) is black if type is CV_8UC3. Or Scalar(0) is black if it's a CV_8UC1 grayscale image. Avoid mixing them together.

标量用于指定每个像素的强度值。例如,如果类型为CV_8UC3,那么标量(255,0,0)是蓝色的,标量(0,0)是黑色的。或者标量(0)是黑色的,如果是CV_8UC1灰度图像。避免混合在一起。

#2


1  

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,标量(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