Opencv:将bgr图片转换成Lab、gray空间

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/Teddygogogo/article/details/84932648

刚开始入门Opencv,无意间发现书本上有对rgb空间进行转换,我就查阅了一些资料,拓展到Lab的空间。

还有一种简单粗暴的读像素点rgb、gray、Lab通道信息的方法,img.at<uchar>(y,x)。

需要注意的是图像在opencv的存储顺序是bgr而不是rgb,同时lab的标准转换最后需要进行映射。


#include <opencv2/opencv.hpp>

//向编译器说明你所调用的函数处于cv、std命名空间
using namespace cv;
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/色卡RGBCMYK.tif", -1);
    if (img_rgb.empty()) return -1;
    cv::imshow("Window1", img_rgb);
    //读取像素点(y,x)的bgr值
    //读取第一个像素点(0,0)的b值
    std::cout << "b thers is :" << (unsigned int)img_rgb.at<uchar>(0, 0) << std::endl;
    //读取第一个像素点(0,1)的g值
    std::cout << "g thers is :" << (unsigned int)img_rgb.at<uchar>(0, 1) << std::endl;
    //读取第一个像素点(0,2)的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);
    //读取第一行第一列像素点的灰度值
    std::cout << "gray1 thers is :" << (unsigned int)img_gray.at<uchar>(0, 0) << std::endl;
    //读取第一行第二列像素点的灰度值
    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);
    //读取第一个像素点(0,0)的l值,需要进行映射,实际的范围是[0,255],理论l的范围是[0,100]
    std::cout << "l thers is :" << (int)(img_lab.at<uchar>(0, 0) * 100 / 255) << std::endl;
    //读取第一个像素点(0,1)的a值,需要进行映射,实际的范围是[0,255],理论a的范围是[-128,127]
    std::cout << "a thers is :" << (int)img_lab.at<uchar>(0, 1) - 128 << std::endl;
    //读取第一个像素点(0,2)的b值,需要进行映射,实际的范围是[0,255],理论b的范围是[-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);
    //读取第一个像素点(0,0)的l值
    std::cout << "b1 thers is :" << (int)img_rgb1.at<uchar>(0, 300) << std::endl;
    //读取第一个像素点(0,1)的a值
    std::cout << "g1 thers is :" << (int)img_rgb1.at<uchar>(0, 301) << std::endl;
    //读取第一个像素点(0,2)的b值
    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");
}

猜你喜欢

转载自blog.csdn.net/Teddygogogo/article/details/84932648