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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28780947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 21:03:56  来源:igfitidea点击:

Opencv create new image using cv::Mat

c++opencvimage-processing

提问by Lusy7

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);

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.

现在我的问题是,如果我在第二个代码中使用 scalar(0) 而不是 scalar(0,0,0),该代码不起作用。1.为什么会发生这种情况,因为两者都创建了一个 Mat 图像结构。2. const cv:Scalar &_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) 结构。

回答by mcchu

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

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

  1. Width: 301 pixels
  2. Height: 260 pixels
  3. Each pixel value (intensity) is 0 ~ 255: an 8-bit unsigned integer
  4. Supports all RGB colors: 3 channels
  5. Initial color: black = (B, G, R) = (0, 0, 0)
  1. 宽度:301 像素
  2. 高度:260 像素
  3. 每个像素值(强度)为 0 ~ 255:一个 8 位无符号整数
  4. 支持所有RGB颜色:3通道
  5. 初始颜色:黑色 = (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 8Umeans the 8-bit Usigned integer, C3means 3 Channels for RGB color, and Scalar(0, 0, 0)is the initial value for each pixel. Similarly,

8U装置中的8ü符号整数,C3是指图3C为RGB颜色hannels,并且Scalar(0, 0, 0)是针对每个像素的初始值。相似地,

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

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

是画一条线grHistogram从点pt1至点pt2。线的颜色为白色(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_8UC1and you only need to specify the intensity for one channel, Scalar(0)for example.

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

因为您需要指定Mat. 它是彩色图像CV_8UC3还是灰度图像CV_8UC1?它们是不同的。如果您Scalar(255)CV_8UC3图像上使用,您的程序可能不会像您想象的那样工作。

What is the purpose of const cv:Scalar &_s ?

const cv:Scalar &_s 的目的是什么?

cv::Scalaris 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_8UC1grayscale image. Avoid mixing them together.

cv::Scalar用于指定每个像素的强度值。例如,如果类型为,Scalar(255, 0, 0)则为蓝色且Scalar(0, 0, 0)为黑色CV_8UC3。或者Scalar(0)是黑色的,如果它是一个CV_8UC1灰度图像。避免将它们混合在一起。

回答by hashan gunathilaka

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