


The road to growth of OpenCV (3): imitating the magic wand tool in PhotoShop
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
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);
For more OpenCV growth path (3): imitating the magic wand tool in PhotoShop, please pay attention to the PHP Chinese website for related articles!

Copying and pasting layer styles in Photoshop is a key trick to improve productivity. Let's dive into how to do it, and the various details and techniques you may encounter in the process. When we talk about copying and pasting layer styles in Photoshop, the first thing we need to understand is that layer styles refer to effects applied to layers, such as shadows, glows, bevels, and reliefs. Mastering this feature not only saves time, but also ensures consistent design. To copy the style of a layer, right-click the layer you want to copy and select Copy Layer Style. This will copy all the styles of the layer into the clipboard. Next, select the target layer you want to apply these styles, right-click it, and select Paste

The steps to synthesize two pictures and achieve seamless connection in Photoshop include: 1. Adjust the brightness and contrast to make the tones of the two pictures consistent; 2. Use masks and blending mode to erase the edges to achieve a natural transition. This method requires repeated adjustments and trial and error, paying attention to detail processing and color consistency to achieve the best results.

Adjusting photos with underexposed photos in Photoshop can use the "Exposure", "Brightness/Contrast" and "Curve" adjustment tools: 1. The "Exposure" adjustment is used to initially improve the overall exposure; 2. The "Brightness/Contrast" adjustment can improve the brightness and contrast at the same time; 3. The "Curve" adjustment allows precise control of different brightness ranges, which is suitable for refinement adjustments.

Coloring black and white photos in Adobe Photoshop can use layer masks, color tuning tools, and brush tools. The specific steps include: 1. Open a black and white photo and create a new layer; 2. Use "Hue/Saturation" to adjust the layer to add colors, and accurately control the application area of the color through the layer mask; 3. Use the brush tool to draw on the layer mask to further adjust the color; 4. Keep the original image details and add new colors by setting the layer's blending mode to "Color".

The key to designing a minimalist style poster is to convey the message through simple elements and tones. The steps are as follows: 1. Select the A3 size (297x420mm), and the resolution is 300dpi. 2. Use monochromatic or complementary tones, cool tones such as blue and white. 3. Add simple sans serif fonts and geometric figures, pay attention to white space and golden point typesetting. 4. Optimize details, such as text size, spacing and layer style effects.

Making GlitchEffect in Photoshop can be achieved through the following steps: 1. Open and copy the picture. 2. Use the channel mixer to disrupt color balance. 3. Add perturbation filters to increase randomness, adjust transparency or use masks to control the effect. 4. Create a digital stripe effect through the threshold adjustment layer and overlay multiple threshold layers to adjust transparency. 5. Use color balance to adjust the overall tone and add personalized colors. The art of making failures is full of experimental and randomness, and moderate and orderly management of layers is key.

Making a double exposure effect is a very cool trick in Photoshop. Let's explore how to achieve this effect, as well as the problems and solutions you may encounter in the process. To create a double exposure effect in Photoshop, you first need to select two or more images, and the elements of these images will be fused together in the final effect. When selecting pictures, it is recommended to choose pictures with high contrast and rich details, so that the effect after fusion will be more obvious and attractive. Let's start with a simple example, suppose we have two pictures: one is a portrait and the other is a night view of the city. Our goal is to blend the night view of the city with portraits to create a dreamy double exposure effect. First, we need to import both images

There are three main ways to create 3D three-dimensional text effects in Photoshop: 1) Use 3D tools, 2) Layer styles, and 3) Manual drawing. First, when using the 3D tool, create a new document and enter text, select the text layer and select "New 3D Highlight Effect from Selection", and then adjust the rotation, zoom, and position. Secondly, adjust the Depth, Size, and Softening parameters through the Bevel and Emboss options in the layer style to simulate the 3D effect. Finally, manual drawing methods require more skill and time, but with complete control over the effects.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 English version
Recommended: Win version, supports code prompts!

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
