当前位置:网站首页>Opencv classic 100 questions

Opencv classic 100 questions

2022-07-06 00:21:00 It rained and windy last night


 Insert picture description here

Preface

Common knowledge records

1. CV_8UC1,CV_8UC2,CV_8UC3 Wait

The general image file format uses Unsigned 8bits Well ,CvMat The parameter type corresponding to the matrix is
CV_8UC1,CV_8UC2,CV_8UC3.
( final 1、2、3 Indicates the number of channels , for example RGB3 The channel is used CV_8UC3)

and float yes 32 Bit , Corresponding CvMat Data structure parameters are :CV_32FC1,CV_32FC2,CV_32FC3…
double yes 64bits, Corresponding CvMat Data structure parameters :CV_64FC1,CV_64FC2,CV_64FC3 etc. .

One 、 Channel switching

subject : cv2::imread() The coefficient of is BGR In the order of .
2.
code

// Read images , And then BGR Replace channel with RGB passageway .
//Mat  The image has no length or width , Only width and height , This should be understandable  width = cols height = rows
void MainWindow::test1ChannelSwap()
{
    
    Mat sourceImage = imread("../Image/imori.jpg");
    qDebug()<<"--->z MainWindow::test1ChannelSwap()1"<<sourceImage.rows<<sourceImage.cols;
    imshow("sourceImage",sourceImage);
    int width = sourceImage.cols;
    int height = sourceImage.rows;

    Mat newImage = Mat::zeros(height,width,CV_8UC3);
    for (int i = 0; i < width; i++)
    {
    
        for (int j = 0; j<height; j++)
        {
    
            newImage.at<cv::Vec3b>(i, j)[0] = sourceImage.at<cv::Vec3b>(i, j)[2];
            newImage.at<cv::Vec3b>(i, j)[1] = sourceImage.at<cv::Vec3b>(i, j)[1];
            newImage.at<cv::Vec3b>(i, j)[2] = sourceImage.at<cv::Vec3b>(i, j)[0];

        }
    }

    imshow("newImage",newImage);
}

Two 、 Graying

subject : Gray scale is a kind of expression method of image brightness , Calculate by the following formula : Y = 0.2126   R + 0.7152   G + 0.0722   B Y = 0.2126\ R + 0.7152\ G + 0.0722\ B Y=0.2126 R+0.7152 G+0.0722 B
code

void MainWindow::test2Grayscale()
{
    
    Mat sourceImage = imread("../Image/imori.jpg");
    qDebug()<<"--->z MainWindow::test1ChannelSwap()1"<<sourceImage.rows<<sourceImage.cols;
    imshow("sourceImage",sourceImage);
    int width = sourceImage.cols;
    int height = sourceImage.rows;
    Mat newImage = Mat::zeros(height,width,CV_8UC1);// Be careful , It's OK to use a channel when applying , Because it's just a grayscale image .
    for (int i = 0; i<height; i++)
    {
    
        for (int j = 0; j < width; j++)
        {
    
            newImage.at<uchar>(i,j) = 0.0126 * (float)sourceImage.at<Vec3b>(i,j)[2]+ 0.7152 * (float)sourceImage.at<Vec3b>(i,j)[1] + 0.0722 * (float)sourceImage.at<Vec3b>(i,j)[0];
        }
    }
    imshow("newImage",newImage);
}

Text

Reference resources

原网站

版权声明
本文为[It rained and windy last night]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060017123036.html