Color extraction and its implementation in Lab color space

Source: Internet
Author: User

This period of time in the lamp detection, some of the lights in the evening yellow brightness is weak, only with gray degree is not enough, by comparison in the RGB, HSV, Lab color space for color extraction, found that the effect of lab color model is the best. Here's how the lab works and its code implementations.

The lab color model consists of three features, one of which is brightness (L), and A and B are two color channels. A includes the color from dark green (low luminance value) to gray (medium luminance value) to bright pink (high luminance value), B from bright blue (low brightness value) to gray (medium luminance value) to yellow (high luminance value). As a result, this color is blended to produce a bright color. (This section of Baidu, haha)

RGB conversion to Lab color space, this http://blog.csdn.net/shamaozi/article/details/6221029 is introduced. Among them, the most essential formula is as follows:

L = Y1 = (13933 * R + 46871 * G + 4732 * B) div 2^16
A = 377 * (14503 * R-22218 * G + 7714 * B) div 2^24 + 128
b = * (12773 * R + 39695 * G-52468 * B) div 2^24 + 128

BGR conversion to lab, the following shows the L, a, b three channels. You can see the basic method of extracting color. Gray-scale image has no color, so in a and B channels in the value of 128; ( -127~127); Extract different colors you can select the appropriate thresholds on three channels.

The conversion code is as follows:

#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
#include <stdio.h>

using namespace Std;
using namespace CV;

int main ()
{

Mat srcimg = Imread ("0.jpg");
Mat rgbimg;

if (!srcimg.data)
{
cerr<< "Faild Read Image" <<endl;
return-1;
}

float Nscale = 1;//0.25;
Size sz;
Sz.width = (int) (Srcimg.cols * nscale);
Sz.height = (int) (srcimg.rows * nscale);
Rgbimg.create (SZ, Srcimg.type ());
Resize (SRCIMG,RGBIMG,SZ);

for (int i = 0; i < rgbimg.rows; ++i)
{
uchar* bdata = bgr_mv[0].ptr<uchar> (i);
uchar* Gdata = bgr_mv[1].ptr<uchar> (i);
uchar* Rdata = bgr_mv[2].ptr<uchar> (i);

uchar* Ldata = limg.ptr<uchar> (i);
uchar* adata = aimg.ptr<uchar> (i);
uchar* bdata = bimg.ptr<uchar> (i);


for (int j = 0; j < Rgbimg.cols; ++j)
{
L
LDATA[J] = (13933 * Rdata[j] + 46871*gdata[j] + 4732*bdata[j])/65536;
A
ADATA[J] = 377 * (14503 * rdata[j]-22218*gdata[j] + 7714*bdata[j])/16777216 + 128;
B
BDATA[J] = * (12773 * rdata[j] + 39695*gdata[j]-52468*bdata[j])/16777216 + 128;

L
LDATA[J] = (0.2126 * Rdata[j] + 0.7152*gdata[j] + 0.0722*bdata[j]);
A
ADATA[J] = 1.4749 * (0.2213 * rdata[j]-0.3390*gdata[j] + 0.1177*bdata[j]) + 128;
B
BDATA[J] = 0.6245 * (0.1949 * rdata[j] + 0.6057*gdata[j]-0.8006*bdata[j]) + 128;

}

Imshow ("B", bimg);
Imshow ("A", aimg);
Imshow ("L", limg);

Waitkey (0);
return 0;
}



Color extraction and its implementation in Lab color space

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.