Color histogram of image features

Source: Internet
Author: User
Tags ranges

Color space of OpenCV:

Color space RGB (red red, green Green, blue blue)

Value range of R: 0-255

Value range of G: 0-255

Value range of B: 0-255

Color space HSV (hue hue, saturation saturation, intensity brightness)

Value range of H: 0-179

Value range of S: 0-255

Value range of V: 0-255

Color Space HLs (hue hue, lightness brightness, saturation saturation)

Value range of H: 0-179

L range of values: 0-255

Value range of S: 0-255

—————————————————————————————————————————————————————

Knowing the range of pixels used in the various channels of the color space, we discuss the color histogram below


One-dimensional histogram:

For example, we only calculate the histogram of the S channel and have 30 bins.

<span Style= "FONT-SIZE:18PX;" > #include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> #include <opencv2\ Imgproc\imgproc.hpp>using namespace CV; #include <iostream>using namespace Std;int main (int argc, char** argv) { Mat src,hsv;if (argc! = 2 | |! ( src = imread (argv[1],1)). Data | | Src.channels ()! = 3) return-1;//Conversion of color space BGR to Hsvcvtcolor (SRC,HSV,CV_BGR2HSV);//divide H channel into 30 bin, divide s channel into 32binint hbins = 30 //int sbins = 32;//int histsize[] = {Hbins,sbins};int histsize[] = {hbins};//h value range 0-179float Hranges [] = {0,180};//s fetch Value range 0-255//float Sranges [] ={0,255};//const float* ranges [] = {hranges,sranges};const float* ranges [] = {hranges}; Matnd hist;//We are based on the image of the first Channel one-dimensional histogram int channels[] = {0};calchist (&hsv,1,channels,mat (), Hist,1,histsize,ranges,true, FALSE);//output histogram Cout<

Output, a 30-dimensional vector is definitely:


Explanation: The first number 60571is the number of pixels that represent h between [0,5] , and the second number 12194 is the number of pixels that represent h in the [6,11] point.

—————————————————————————————————————————————————————

Two-dimensional histogram:

<span style= "FONT-SIZE:18PX;" ><span style= "Font-family:microsoft yahei;font-size:14px;" ><span style= "Font-family:microsoft yahei;font-size:14px;" > #include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> #include <opencv2\ Imgproc\imgproc.hpp>using namespace CV; #include <iostream>using namespace Std;int main (int argc, char** argv) { Mat src,hsv;if (argc! = 2 | |! ( src = imread (argv[1],1)). Data | | Src.channels ()! = 3) return-1;//The conversion of the color space BGR to Hsvcvtcolor (SRC,HSV,CV_BGR2HSV);//The H channel is divided into 30 bins, the S channel is divided into 32binint hbins = 5; int sbins = 4;int histsize[] = {hbins,sbins};//h value range 0-179float Hranges [] = {0,180};//s value range 0-255float sranges [] ={0,2 56};const float* Ranges [] = {hranges,sranges}; Matnd hist;//We calculate the two-dimensional histogram int channels[] = {0,1};calchist (&hsv,1,channels,mat (), Hist,2,histsize, for the first and second channels of the image, Ranges,true,false);//output histogram Cout<


In our case, the output of the two-dimensional histogram is:


Now let's explain that this is a 5 row 4 column Two-dimensional histogram, the first row of the first column of the 128239, representing the values of H and S in [0,35]x[0,63], the second row in the first column of 18585 the values for H and S are in [36,71]x[0,63], and so on, how to verify? We just need to change the above procedure to a few numbers, for example we only calculate the histogram of H and S values in [36,71]x[0,63] :

<span style= "FONT-SIZE:18PX;" > #include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> #include <opencv2\ Imgproc\imgproc.hpp>using namespace CV; #include <iostream>using namespace Std;int main (int argc, char** argv) { Mat src,hsv;if (argc! = 2 | |! ( src = imread (argv[1],1)). Data | | Src.channels ()! = 3) return-1;//The conversion of the color space BGR to Hsvcvtcolor (SRC,HSV,CV_BGR2HSV);//Divide the H-Channel into 30 bins and divide the S-channel into 32binint hbins  = 1;int Sbins = 1;int histsize[] = {hbins,sbins};//h value range 0-179float Hranges [] = {36,72};//s range 0-255float sranges [] ={0,64};const float* ranges [] = {hranges,sranges}; Matnd hist;//We calculate the two-dimensional histogram int channels[] = {0,1};calchist (&hsv,1,channels,mat (), Hist,2,histsize, for the first and second channels of the image, Ranges,true,false);//output histogram Cout<

Output Result:


_______________________________________________________________________________________________________________ _________________

Understand the simple color histogram, the color histogram as a simple feature of the picture, do a simple image retrieval.

Color histogram of image features

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.