Opencv: Convert bgr images into Lab, gray space

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/Teddygogogo/article/details/84932648

Just get started Opencv, inadvertently found to rgb space conversion, I looked up some information, expand the space Lab of books.

There is also a simple and crude read pixel rgb, gray, Lab channel information method, img.at <uchar> (y, x).

Note that the image is stored sequentially opencv bgr instead rgb, while lab standard conversion required final mapping.


#include <opencv2/opencv.hpp>

// instructions to the compiler that you call the function in cv, std namespace
a using namespace CV;
a using namespace std;

int main()
{
    cv::Mat img_rgb, img_rgb1, img_gray, img_lab, img_canny;
    namedWindow("Window1", cv::WINDOW_AUTOSIZE);
    namedWindow("Window2", cv::WINDOW_AUTOSIZE);
    namedWindow("Window3", cv::WINDOW_AUTOSIZE);
    namedWindow("Window4", cv::WINDOW_AUTOSIZE);
    namedWindow("Window5", cv::WINDOW_AUTOSIZE);

    img_rgb = imread ( "D: / Desktop / pic / color card RGBCMYK.tif", -1);
    IF (img_rgb.empty ()) return -1;
    CV :: imshow ( "the Window1", img_rgb);
    // read take pixel (y, x) of the value bgr
    // read the first pixel (0,0) value of b
    std :: cout << "b thers is :" << (unsigned int) img_rgb.at < UCHAR> (0, 0) :: STD << endl;
    // read the first pixel (0,1) value of g
    std :: cout << "g thers is :" << (unsigned int) img_rgb .at <UCHAR> (0,. 1) STD :: << endl;
    // read the first pixel (0,2) value of r
    std :: cout << "r thers is :" << (unsigned int) img_rgb.at <uchar> (0 , 2) << std :: endl << std :: endl;


    :: CV cvtColor (img_rgb, img_gray, CV :: COLOR_BGR2GRAY );
    CV :: imshow ( "Window2", img_gray);
    // read the first row, first column pixel grayscale value
    std :: cout << " Thers iS Gray1: "<< (unsigned int) img_gray.at <UCHAR> (0, 0) :: STD << endl;
    // read the first row second column pixel tone value of
    std :: cout < < "gray2 thers is:" << (unsigned int) img_gray.at <uchar> (0, 1) << std :: endl << std :: endl;


    :: CV cvtColor (img_rgb, img_lab, CV :: COLOR_BGR2Lab );
    CV :: imshow ( "Window3", img_lab);
    // read the first pixel (0,0) value l needs to be mapped, the actual in the range [0,255], l is the theoretical range [0,100]
    STD :: COUT << "l Thers iS:" << (int) (img_lab.at <UCHAR> (0, 0) * 100/255) < <STD :: endl;
    // read the first pixel (0,1) of a value need to be mapped, the actual range is [0,255], a theoretical range is [-128,127]
    STD :: COUT < < "a Thers iS:" << (int) img_lab.at <UCHAR> (0,. 1) - 128 << STD :: endl;
    // read the first pixel (0,2) b-value, mapping required, the actual range is [0,255], b is the theoretical range [-128,127]
    STD :: COUT << "b Thers iS:" << (int) img_lab.at <UCHAR> (0, 2) - 128 << std :: endl << std::endl;


    :: CV cvtColor (img_lab, img_rgb1, CV :: COLOR_Lab2BGR) ;
    CV :: imshow ( "Window4", img_rgb1);
    // read the first pixel (0,0) value of l
    std :: cout << "B1 Thers iS:" << (int) img_rgb1.at <UCHAR> (0, 300) STD :: << endl;
    // read the first pixel (0,1) is a value
    std :: cout << "G1 Thers iS:" << (int) img_rgb1.at <UCHAR> (0, 301) STD :: << endl;
    // read the first pixel (0,2) of the b-value
    std: : cout << "r1 thers is: " << (int) img_rgb1.at <uchar> (0, 302) << std :: endl << std :: endl;


    cv::Canny(img_gray, img_canny, 10, 100, 3, true);
    cv::imshow("Window5", img_canny);
    std::cout << "pixel thers is :" << (unsigned int)img_canny.at<uchar>(0, 0) << std::endl;
    std::cout << "pixel thers is :" << (unsigned int)img_canny.at<uchar>(0, 1) << std::endl;
    std::cout << "pixel thers is :" << (unsigned int)img_canny.at<uchar>(0, 2) << std::endl;


    waitKey(0);
    destroyWindow("Window1");
    destroyWindow("Window2");
    destroyWindow("Window3");
    destroyWindow("Window4");
    destroyWindow("Window5");
}

Guess you like

Origin blog.csdn.net/Teddygogogo/article/details/84932648