Table of contents
Source code (the comments are very detailed, read slowly):
Knowledge explanation:
Mask:
Mask is a matrix of the same size as the original image, used to control image processing. Each pixel value in the mask corresponds to a pixel position in the original image, and it can take different pixel values to represent different information.
In OpenCV, a mask is usually a single-channel image with pixel values typically ranging from 0 to 255. The pixel values in the mask determine whether the pixels at the corresponding positions participate in a specific image processing operation.
Mask operation refers to controlling the processing of the image or selecting the area of interest based on the pixel value of the mask during the image processing process. In the mask operation, for the positions in the mask with a non-zero pixel value, the corresponding pixels in the original image will be processed or selected, and for the positions in the mask with a zero pixel value, the corresponding pixels in the original image will be processed or selected. Ignore or obscure
Mat.SetTo()
Sets all elements of the matrix (Mat) to the given values.
public void SetTo(Scalar value);
Accepts an Scalar
object as a parameter, Scalar
which is an array containing multiple elements used to represent the channel values of image pixels. For example, for a grayscale image, Scalar
the object has only one element, representing the gray value; for a color image, Scalar
the object has three elements, representing the values of the blue channel, the green channel, and the red channel.
Rect
kind:
Defines the position and size of a rectangle, often used to specify a specific area of an image that needs to be processed.
public Rect(int x, int y, int width, int height);
eg:
Rect roi = new Rect(100, 100, 200, 150)
Parameter Description:
x
: X coordinate of the upper left corner of the rectangle.y
: Y coordinate of the upper left corner of the rectangle.width
: The width of the rectangle.height
:Height of rectangle
Mat.CopyTo()
Copy a Mat image ( src
) according to mask
the pixel value of the mask ( ) and save the result in another image ( dst
)
public void CopyTo(OutputArray dst, InputArray mask = null)
1.dst
is the target image, which is used to save the copied image data.
2.mask
is an optional parameter used to specify the mask. The mask is a src
matrix with the same size as the original image ( ), used to control the copy of the image.
If the parameter is provided mask
, mask
the pixel value at the corresponding position in will be src
copied to only if the pixel value at the corresponding position in is non-zero (non-0) dst
. If mask
the pixel value at the corresponding position in is zero (0), the pixel value at the corresponding position will not be copied, which is equivalent to being masked.
If mask
the parameter is not provided (that is, null
), then all pixel values will be copied to dst
, which is equivalent to no masking operation and complete copying is achieved.
Idea:
Read a raw image, then create a mask matrix, specifying the area to be processed by setting the pixel values of the mask. Then, the original image is copied according to the pixel value of the mask to obtain a processed result image, and finally the original image and the result image are displayed in the window.
Source code (the comments are very detailed, read slowly):
You need to install the "OpenCvSharp4" library, otherwise an error will be reported.
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 掩码操作
{
class Program
{
static void Main(string[] args)
{
// 一、读取原始图像
string imagePath = "C:\\Users\\CGW\\Desktop\\digits\\556.jpg"; //图像路径
Mat src = Cv2.ImRead(imagePath, ImreadModes.Color);
// 图像读取的错误处理:如果无法读取图像,会输出错误消息并结束程序。
if (src.Empty())
{
Console.WriteLine("无法读取图像:{0}", imagePath);
return;
}
// 二、创建掩码
// 使用Mat类的构造函数和MatType.CV_8UC1参数创建掩码(初始值为0,与原图像尺寸相同)
// 掩码的初始值被设置为全零,即所有像素值都为0,这意味着初始时不对图像进行任何处理。
Mat mask = new Mat(src.Size(), MatType.CV_8UC1, Scalar.All(0));
// 三、设置矩形区域为掩码中的非零值
// 设置矩形区域为掩码中的非零值(255),在掩码中指定了一个矩形区域,表示该区域需要进行处理。
Rect roi = new Rect(100, 100, 200, 150);
mask[roi].SetTo(new Scalar(255));
// 四、应用掩码
// 用Cv2.CopyTo()方法,将原始图像src按照掩码mask的像素值进行拷贝,得到一个新的Mat对象result
// 在拷贝过程中,只有掩码中对应位置为非零的像素值才会被拷贝到新的图像中,其他位置的像素值保持不变。
// 从而实现了对原始图像中指定区域的处理。
Mat result = new Mat();
src.CopyTo(result, mask);
// 显示原始图像和处理后的结果
Cv2.ImShow("Original Image", src);
Cv2.ImShow("Masked Image", result);
Cv2.WaitKey(0);
}
}
}