Home  >  Article  >  Web Front-end  >  The road to growth of OpenCV (3): imitating the magic wand tool in PhotoShop

The road to growth of OpenCV (3): imitating the magic wand tool in PhotoShop

高洛峰
高洛峰Original
2017-02-20 09:09:251541browse

The subject of this article is actually the conversion of the color space of the image. With the help of a color selection program, the usage of the color conversion function in OpenCV and some precautions are explained.

1. Several common color spaces:

RGB color space: RGB uses the additive color mixing method, because it describes the ratio of various "lights" to produce colors. Starting from darkness, light continuously superimposes to produce color. RGB describes the values ​​of red, green and blue light. Digital image storage generally uses RGB mode. It is worth noting that the storage order of the three channels in OpenCV is BGR.

HSV, HSI: These two color formats are defined based on the human eye's distinction between colors, where H (hue) represents hue, S (saturation) represents saturation, and V (value) represents Lightness, I (intensity) represents brightness.

Lab space: Uniform changes in the model correspond to uniform changes in perceived color, so we can imagine Lab as a point in the color space. The closer the adjacent points are, the closer they are to each other. The closer, so Lab space is often used to measure the similarity of two colors.

For more knowledge about color space, please refer to: http://en.wikipedia.org/wiki/Color_space

2. Color space conversion in OpenCV

In OpenCV The color conversion of the image is completed through the cvtColor function. cvtColor is defined in the opencv2/imgproc/imgproc.hpp header file. Its C++ interface is as follows:

void cvtColor( InputArray src, OutputArray dst, int code, int dstCn=0 )

src: Input image.

dst: Output image.

code: Color conversion type, such as: CV_BGR2Lab, CV_BGR2HSV, CV_HSV2BGR, CV_BGR2RGB.

dstCn: The channel number of the output image. If the default is 0, it means the number of channels of the input image.

Convert the image image from BGR to Lab: cvtColor(image,image,CV_BGR2Lab)

3. Simple magic wand program

First we define a colorDetect class:

class colorDetect{private:    int minDist; //minium acceptable distance    Vec3b target;//target color;    
    Mat result; //the resultpublic:
    colorDetect();    void SetMinDistance(int dist);    void SetTargetColor(uchar red,uchar green,uchar blue);    void SetTargetColor(Vec3b color); //set the target color    Mat process(const Mat& image); //main process};

The minDist is the threshold we define to limit the distance between two colors, which is equivalent to the threshold of the magic wand tool in PhotoShop.

target is the target color, which is equivalent to the seed color. result is the result of storage processing.

Process is the main processing program. Let’s look at the content of process.

Mat colorDetect::process(const Mat& image)
{    Mat ImageLab=image.clone();
    result.create(image.rows,image.cols,CV_8U);    
    //将image转换为Lab格式存储在ImageLab中    
    cvtColor(image,ImageLab,CV_BGR2Lab);    
    //将目标颜色由BGR转换为Lab    
    Mat temp(1,1,CV_8UC3);
    temp.at<Vec3b>(0,0)=target;//创建了一张1*1的临时图像并用目标颜色填充    
    cvtColor(temp,temp,CV_BGR2Lab);
    target=temp.at<Vec3b>(0,0);//再从临时图像的Lab格式中取出目标颜色

    // 创建处理用的迭代器    
    Mat_<Vec3b>::iterator it=ImageLab.begin<Vec3b>();    
    Mat_<Vec3b>::iterator itend=ImageLab.end<Vec3b>();    
    Mat_<uchar>::iterator itout=result.begin<uchar>();    
    while(it!=itend)
    {        
    //两个颜色值之间距离的计算        
    int dist=static_cast<int>(norm<int,3>(Vec3i((*it)[0]-target[0],
            (*it)[1]-target[1],(*it)[2]-target[2])));        
            if(dist<minDist)
            (*itout)=255;        
            else            
            (*itout)=0;
        it++;
        itout++;
    }    return result;
}


There are two points that need special attention in the program:

1. After converting the image to Lab space, the target color also needs to be converted. How to do it A temporary image is created.

2. The norm function is used to determine the distance between two colors. Its operation is norm(v). where v is a dim-dimensional vector. In the program, it is a three-dimensional appropriate amount, which is the result of subtracting two color values.

It is worth thinking about whether Vec3i((*it)[0]-target[0],(*it)[1]-target[1],(*it)[2]- What about replacing target[2]) with Vec3i((*it)-target)? The answer is no, because (*it)-target will automatically restrict the type of the subtraction result during the actual operation.

We can get an example effect after setting the target color and threshold like this:

cdet.SetTargetColor(150,150,150);
cdet.SetMinDistance(50);

The road to growth of OpenCV (3): imitating the magic wand tool in PhotoShop

For more OpenCV growth path (3): imitating the magic wand tool in PhotoShop, please pay attention to the PHP Chinese website for related articles!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn