8. OpenCvSharp image grayscale processing (component method, maximum value method, average method, weighted average method) - c#OpenCvSharp study notes

OpenCvSharp image grayscale processing (component method, maximum method, average method, weighted average method)

insert image description here

Project Overview

The project realizes image grayscale processing based on OpenCvSharp, including the realization of grayscale processing methods such as component method, maximum value method, average method, and weighted average method. The opened image can be processed and saved to the local disk.

Source code is at the bottom of this article

0. Principles and formulas

Component method:

That is to extract the luminance value of each component channel and display it according to the corresponding grayscale value; for example, a color image (with 3 channels, the luminance value range of each channel: 0-255), the luminance of one of the channels The value is extracted to form a black and white image (the brightest part is 255, showing white; the darkest part is 0, showing black).
insert image description here
In the formula, GRAYk(i, j) (k=1~3) is the brightness value of the pixel at the image (i, j) coordinates, corresponding to the B channel, G channel, and R channel respectively. Note that opencv is a BGR channel, not a commonly used RGB channel.

Maximum method:

The gray value of each pixel is selected from the maximum value of the brightness values ​​of the three channels.
insert image description here
In the formula, Max is the maximum value function.

Average method:

The gray value of each pixel is the average value of the brightness values ​​of the three channels.
insert image description here

Weighted average method:

The gray value of each pixel is based on the highest sensitivity of the human eye to green and the lowest sensitivity to blue to establish the weight of different channels. The weight value of the B channel is 0.114, the weight value of the G channel is 0.578, and the weight value of the R channel is 0.299.
insert image description here

1 Basic steps and interface design

1.1 Citation

using OpenCvSharp;using OpenCvSharp.Extensions;

1.2 Layout, rename, adjust shape and font of Picturebox, Label, Button and other controls to form the following interface:

insert image description here

2 function realization

2.1 Initialize variables

        Mat Img1 = new Mat();//用 Mat类定义图片1
        Mat Img2 = new Mat();//用 Mat类定义图片2
        Mat  ImgCvt = new Mat();
        Bitmap bitmap;//Bitmap类定义picturebox2要显示的图片
        string pathname;//定义图片打开路径
        

2.2 Component method

Traverse the brightness value of a certain channel of each pixel of srcImg, and assign it to dstImg, where 0, 1, and 2 in [] correspond to B, G, and R channels respectively.
insert image description here

 private void button2_Click_1(object sender, EventArgs e)
        {
    
    
            if (pictureBox1.Image == null)//判断图片是否已打开
            {
    
    
                MessageBox.Show("没有打开图片");
                return;
            }
            //   
            // Mat srcImg = Cv2.ImRead(pathname);//读取路径下的图片

            Mat srcImg = Cv2.ImRead(pathname);//读取路径下的图片
            Mat dstImg = new Mat(srcImg.Size(), MatType.CV_8U);
            //Cv2.Threshold(image, image, 45570, 65535, ThresholdTypes.Binary);
            int height = srcImg.Rows;
            int width = srcImg.Cols;
            for (int row = 0; row < height; row++)
            {
    
    
                for (int col = 0; col < width; col++)
                {
    
    
                    dstImg.At<byte>(row, col) = (byte)srcImg.At<Vec3b>(row, col)[2];


                }
            }
            bitmap = BitmapConverter.ToBitmap(dstImg); //把Mat格式的图片转换成Bitmap
            pictureBox2.Image = bitmap;


        }

2.3 Maximum value method, average method, weighted average method

Other equal component methods: Just replace the formulas in the two for loops

 dstImg.At<byte>(row, col) = (byte)Math.Max(srcImg.At<Vec3b>(row, col)[0] , Math.Max(srcImg.At<Vec3b>(row, col)[1], srcImg.At<Vec3b>(row, col)[2]));//最大值法

 dstImg.At<byte>(row, col) = (byte)((srcImg.At<Vec3b>(row, col)[0]+ srcImg.At<Vec3b>(row, col)[1]+ srcImg.At<Vec3b>(row, col)[2])/3);//平均值法

dstImg.At<byte>(row, col) = (byte)(srcImg.At<Vec3b>(row, col)[0]*0.144 + srcImg.At<Vec3b>(row, col)[1]*0.578 + srcImg.At<Vec3b>(row, col)[2]*0.299);//加权平均法
                }

Source code: https://download.csdn.net/download/sunsoldeir1/87156764

Guess you like

Origin blog.csdn.net/sunsoldeir1/article/details/128043049