开发者

How to create openCV image of certain (R, G, B) color and get that color name?

开发者 https://www.devze.com 2023-01-28 22:11 出处:网络
I need to create an image filled with some (R,G,B) color. And 开发者_高级运维get that color name like some (R,G,B) = black or red and so on. Can we do such thing with openCV, and how to do it?Static c

I need to create an image filled with some (R,G,B) color. And 开发者_高级运维get that color name like some (R,G,B) = black or red and so on. Can we do such thing with openCV, and how to do it?


Static color map class with static methods (both answers combined). Simply copy and use.

#pragma once

#include <opencv/cv.hpp>
#include <opencv/cv.h>

class Color
{
public:
    static cv::Scalar aliceblue;
    static cv::Scalar antiquewhite;
    static cv::Scalar aqua;
    static cv::Scalar aquamarine;
    static cv::Scalar azure;
    static cv::Scalar beige;
    static cv::Scalar bisque;
    static cv::Scalar black;
    static cv::Scalar blanchedalmond;
    static cv::Scalar blue;
    static cv::Scalar blueviolet;
    static cv::Scalar brown;
    static cv::Scalar burlywood;
    static cv::Scalar cadetblue;
    static cv::Scalar chartreuse;
    static cv::Scalar chocolate;
    static cv::Scalar coral;
    static cv::Scalar cornflowerblue;
    static cv::Scalar cornsilk;
    static cv::Scalar crimson;
    static cv::Scalar cyan;
    static cv::Scalar darkblue;
    static cv::Scalar darkcyan;
    static cv::Scalar darkgoldenrod;
    static cv::Scalar darkgray;
    static cv::Scalar darkgreen;
    static cv::Scalar darkgrey;
    static cv::Scalar darkkhaki;
    static cv::Scalar darkmagenta;
    static cv::Scalar darkolivegreen;
    static cv::Scalar darkorange;
    static cv::Scalar darkorchid;
    static cv::Scalar darkred;
    static cv::Scalar darksalmon;
    static cv::Scalar darkseagreen;
    static cv::Scalar darkslateblue;
    static cv::Scalar darkslategray;
    static cv::Scalar darkslategrey;
    static cv::Scalar darkturquoise;
    static cv::Scalar darkviolet;
    static cv::Scalar deeppink;
    static cv::Scalar deepskyblue;
    static cv::Scalar dimgray;
    static cv::Scalar dimgrey;
    static cv::Scalar dodgerblue;
    static cv::Scalar firebrick;
    static cv::Scalar floralwhite;
    static cv::Scalar forestgreen;
    static cv::Scalar fuchsia;
    static cv::Scalar gainsboro;
    static cv::Scalar ghostwhite;
    static cv::Scalar gold;
    static cv::Scalar goldenrod;
    static cv::Scalar gray;
    static cv::Scalar green;
    static cv::Scalar greenyellow;
    static cv::Scalar grey;
    static cv::Scalar honeydew;
    static cv::Scalar hotpink;
    static cv::Scalar indianred;
    static cv::Scalar indigo;
    static cv::Scalar ivory;
    static cv::Scalar khaki;
    static cv::Scalar lavender;
    static cv::Scalar lavenderblush;
    static cv::Scalar lawngreen;
    static cv::Scalar lemonchiffon;
    static cv::Scalar lightblue;
    static cv::Scalar lightcoral;
    static cv::Scalar lightcyan;
    static cv::Scalar lightgoldenrodyellow;
    static cv::Scalar lightgray;
    static cv::Scalar lightgreen;
    static cv::Scalar lightgrey;
    static cv::Scalar lightpink;
    static cv::Scalar lightsalmon;
    static cv::Scalar lightseagreen;
    static cv::Scalar lightskyblue;
    static cv::Scalar lightslategray;
    static cv::Scalar lightslategrey;
    static cv::Scalar lightsteelblue;
    static cv::Scalar lightyellow;
    static cv::Scalar lime;
    static cv::Scalar limegreen;
    static cv::Scalar linen;
    static cv::Scalar magenta;
    static cv::Scalar maroon;
    static cv::Scalar mediumaquamarine;
    static cv::Scalar mediumblue;
    static cv::Scalar mediumorchid;
    static cv::Scalar mediumpurple;
    static cv::Scalar mediumseagreen;
    static cv::Scalar mediumslateblue;
    static cv::Scalar mediumspringgreen;
    static cv::Scalar mediumturquoise;
    static cv::Scalar mediumvioletred;
    static cv::Scalar midnightblue;
    static cv::Scalar mintcream;
    static cv::Scalar mistyrose;
    static cv::Scalar moccasin;
    static cv::Scalar navajowhite;
    static cv::Scalar navy;
    static cv::Scalar oldlace;
    static cv::Scalar olive;
    static cv::Scalar olivedrab;
    static cv::Scalar orange;
    static cv::Scalar orangered;
    static cv::Scalar orchid;
    static cv::Scalar palegoldenrod;
    static cv::Scalar palegreen;
    static cv::Scalar paleturquoise;
    static cv::Scalar palevioletred;
    static cv::Scalar papayawhip;
    static cv::Scalar peachpuff;
    static cv::Scalar peru;
    static cv::Scalar pink;
    static cv::Scalar plum;
    static cv::Scalar powderblue;
    static cv::Scalar purple;
    static cv::Scalar red;
    static cv::Scalar rosybrown;
    static cv::Scalar royalblue;
    static cv::Scalar saddlebrown;
    static cv::Scalar salmon;
    static cv::Scalar sandybrown;
    static cv::Scalar seagreen;
    static cv::Scalar seashell;
    static cv::Scalar sienna;
    static cv::Scalar silver;
    static cv::Scalar skyblue;
    static cv::Scalar slateblue;
    static cv::Scalar slategray;
    static cv::Scalar slategrey;
    static cv::Scalar snow;
    static cv::Scalar springgreen;
    static cv::Scalar steelblue;
    static cv::Scalar tan;
    static cv::Scalar teal;
    static cv::Scalar thistle;
    static cv::Scalar tomato;
    static cv::Scalar turquoise;
    static cv::Scalar violet;
    static cv::Scalar wheat;
    static cv::Scalar white;
    static cv::Scalar whitesmoke;
    static cv::Scalar yellow;
    static cv::Scalar yellowgreen;
};

//In the cpp file add:

cv::Scalar Color::aliceblue(255, 248, 240);
cv::Scalar Color::antiquewhite(215, 235, 250);
cv::Scalar Color::aqua(255, 255, 0);
cv::Scalar Color::aquamarine(212, 255, 127);
cv::Scalar Color::azure(255, 255, 240);
cv::Scalar Color::beige(220, 245, 245);
cv::Scalar Color::bisque(196, 228, 255);
cv::Scalar Color::black(0, 0, 0);
cv::Scalar Color::blanchedalmond(205, 235, 255);
cv::Scalar Color::blue(255, 0, 0);
cv::Scalar Color::blueviolet(226, 43, 138);
cv::Scalar Color::brown(42, 42, 165);
cv::Scalar Color::burlywood(135, 184, 222);
cv::Scalar Color::cadetblue(160, 158, 95);
cv::Scalar Color::chartreuse(0, 255, 127);
cv::Scalar Color::chocolate(30, 105, 210);
cv::Scalar Color::coral(80, 127, 255);
cv::Scalar Color::cornflowerblue(237, 149, 100);
cv::Scalar Color::cornsilk(220, 248, 255);
cv::Scalar Color::crimson(60, 20, 220);
cv::Scalar Color::cyan(255, 255, 0);
cv::Scalar Color::darkblue(139, 0, 0);
cv::Scalar Color::darkcyan(139, 139, 0);
cv::Scalar Color::darkgoldenrod(11, 134, 184);
cv::Scalar Color::darkgray(169, 169, 169);
cv::Scalar Color::darkgreen(0, 100, 0);
cv::Scalar Color::darkgrey(169, 169, 169);
cv::Scalar Color::darkkhaki(107, 183, 189);
cv::Scalar Color::darkmagenta(139, 0, 139);
cv::Scalar Color::darkolivegreen(47, 107, 85);
cv::Scalar Color::darkorange(0, 140, 255);
cv::Scalar Color::darkorchid(204, 50, 153);
cv::Scalar Color::darkred(0, 0, 139);
cv::Scalar Color::darksalmon(122, 150, 233);
cv::Scalar Color::darkseagreen(143, 188, 143);
cv::Scalar Color::darkslateblue(139, 61, 72);
cv::Scalar Color::darkslategray(79, 79, 47);
cv::Scalar Color::darkslategrey(79, 79, 47);
cv::Scalar Color::darkturquoise(209, 206, 0);
cv::Scalar Color::darkviolet(211, 0, 148);
cv::Scalar Color::deeppink(147, 20, 255);
cv::Scalar Color::deepskyblue(255, 191, 0);
cv::Scalar Color::dimgray(105, 105, 105);
cv::Scalar Color::dimgrey(105, 105, 105);
cv::Scalar Color::dodgerblue(255, 144, 30);
cv::Scalar Color::firebrick(34, 34, 178);
cv::Scalar Color::floralwhite(240, 250, 255);
cv::Scalar Color::forestgreen(34, 139, 34);
cv::Scalar Color::fuchsia(255, 0, 255);
cv::Scalar Color::gainsboro(220, 220, 220);
cv::Scalar Color::ghostwhite(255, 248, 248);
cv::Scalar Color::gold(0, 215, 255);
cv::Scalar Color::goldenrod(32, 165, 218);
cv::Scalar Color::gray(128, 128, 128);
cv::Scalar Color::green(0, 128, 0);
cv::Scalar Color::greenyellow(47, 255, 173);
cv::Scalar Color::grey(128, 128, 128);
cv::Scalar Color::honeydew(240, 255, 240);
cv::Scalar Color::hotpink(180, 105, 255);
cv::Scalar Color::indianred(92, 92, 205);
cv::Scalar Color::indigo(130, 0, 75);
cv::Scalar Color::ivory(240, 255, 255);
cv::Scalar Color::khaki(140, 230, 240);
cv::Scalar Color::lavender(250, 230, 230);
cv::Scalar Color::lavenderblush(245, 240, 255);
cv::Scalar Color::lawngreen(0, 252, 124);
cv::Scalar Color::lemonchiffon(205, 250, 255);
cv::Scalar Color::lightblue(230, 216, 173);
cv::Scalar Color::lightcoral(128, 128, 240);
cv::Scalar Color::lightcyan(255, 255, 224);
cv::Scalar Color::lightgoldenrodyellow(210, 250, 250);
cv::Scalar Color::lightgray(211, 211, 211);
cv::Scalar Color::lightgreen(144, 238, 144);
cv::Scalar Color::lightgrey(211, 211, 211);
cv::Scalar Color::lightpink(193, 182, 255);
cv::Scalar Color::lightsalmon(122, 160, 255);
cv::Scalar Color::lightseagreen(170, 178, 32);
cv::Scalar Color::lightskyblue(250, 206, 135);
cv::Scalar Color::lightslategray(153, 136, 119);
cv::Scalar Color::lightslategrey(153, 136, 119);
cv::Scalar Color::lightsteelblue(222, 196, 176);
cv::Scalar Color::lightyellow(224, 255, 255);
cv::Scalar Color::lime(0, 255, 0);
cv::Scalar Color::limegreen(50, 205, 50);
cv::Scalar Color::linen(230, 240, 250);
cv::Scalar Color::magenta(255, 0, 255);
cv::Scalar Color::maroon(0, 0, 128);
cv::Scalar Color::mediumaquamarine(170, 205, 102);
cv::Scalar Color::mediumblue(205, 0, 0);
cv::Scalar Color::mediumorchid(211, 85, 186);
cv::Scalar Color::mediumpurple(219, 112, 147);
cv::Scalar Color::mediumseagreen(113, 179, 60);
cv::Scalar Color::mediumslateblue(238, 104, 123);
cv::Scalar Color::mediumspringgreen(154, 250, 0);
cv::Scalar Color::mediumturquoise(204, 209, 72);
cv::Scalar Color::mediumvioletred(133, 21, 199);
cv::Scalar Color::midnightblue(112, 25, 25);
cv::Scalar Color::mintcream(250, 255, 245);
cv::Scalar Color::mistyrose(225, 228, 255);
cv::Scalar Color::moccasin(181, 228, 255);
cv::Scalar Color::navajowhite(173, 222, 255);
cv::Scalar Color::navy(128, 0, 0);
cv::Scalar Color::oldlace(230, 245, 253);
cv::Scalar Color::olive(0, 128, 128);
cv::Scalar Color::olivedrab(35, 142, 107);
cv::Scalar Color::orange(0, 165, 255);
cv::Scalar Color::orangered(0, 69, 255);
cv::Scalar Color::orchid(214, 112, 218);
cv::Scalar Color::palegoldenrod(170, 232, 238);
cv::Scalar Color::palegreen(152, 251, 152);
cv::Scalar Color::paleturquoise(238, 238, 175);
cv::Scalar Color::palevioletred(147, 112, 219);
cv::Scalar Color::papayawhip(213, 239, 255);
cv::Scalar Color::peachpuff(185, 218, 255);
cv::Scalar Color::peru(63, 133, 205);
cv::Scalar Color::pink(203, 192, 255);
cv::Scalar Color::plum(221, 160, 221);
cv::Scalar Color::powderblue(230, 224, 176);
cv::Scalar Color::purple(128, 0, 128);
cv::Scalar Color::red(0, 0, 255);
cv::Scalar Color::rosybrown(143, 143, 188);
cv::Scalar Color::royalblue(225, 105, 65);
cv::Scalar Color::saddlebrown(19, 69, 139);
cv::Scalar Color::salmon(114, 128, 250);
cv::Scalar Color::sandybrown(96, 164, 244);
cv::Scalar Color::seagreen(87, 139, 46);
cv::Scalar Color::seashell(238, 245, 255);
cv::Scalar Color::sienna(45, 82, 160);
cv::Scalar Color::silver(192, 192, 192);
cv::Scalar Color::skyblue(235, 206, 135);
cv::Scalar Color::slateblue(205, 90, 106);
cv::Scalar Color::slategray(144, 128, 112);
cv::Scalar Color::slategrey(144, 128, 112);
cv::Scalar Color::snow(250, 250, 255);
cv::Scalar Color::springgreen(127, 255, 0);
cv::Scalar Color::steelblue(180, 130, 70);
cv::Scalar Color::tan(140, 180, 210);
cv::Scalar Color::teal(128, 128, 0);
cv::Scalar Color::thistle(216, 191, 216);
cv::Scalar Color::tomato(71, 99, 255);
cv::Scalar Color::turquoise(208, 224, 64);
cv::Scalar Color::violet(238, 130, 238);
cv::Scalar Color::wheat(179, 222, 245);
cv::Scalar Color::white(255, 255, 255);
cv::Scalar Color::whitesmoke(245, 245, 245);
cv::Scalar Color::yellow(0, 255, 255);
cv::Scalar Color::yellowgreen(50, 205, 154);


//In the header file add:

class Color
{
    public:
    static cv::Scalar ColorBlue;
    static cv::Scalar ColorRed;
    static cv::Scalar ColorGreen;
    static cv::Scalar ColorWhite;
    static cv::Scalar ColorBlack;
};

//In the cpp file add:

cv::Scalar Color::ColorBlue(255, 0, 0, 0);
cv::Scalar Color::ColorRed(0, 255, 0, 0);
cv::Scalar Color::ColorGreen(0, 0, 255, 0);
cv::Scalar Color::ColorWhite(255, 255, 255, 0);
cv::Scalar Color::ColorBlack(0, 0, 0, 0);


opencv doesn't create images with a color, easiest way is either a fill or just draw a filled rectangle with the chosen color.
See https://docs.opencv.org/trunk/dc/da5/tutorial_py_drawing_functions.html

There isn't (AFAIK) a function to return the color name, easiest way is to get a list of colors from somewhere and check the RGB values against the pixel. If you need a closest rather than exact match (rememebr there aren't names for all 244bit colors!) look at HSV color space


Here is a list of official css colors, translated to OpenCV BGR Scalar:

Just put in colors.h and never again worry about BGR - RGB - Color-Name translation..

#define COLORS_aliceblue  Scalar(255,248,240)
#define COLORS_antiquewhite  Scalar(215,235,250)
#define COLORS_aqua  Scalar(255,255,0)
#define COLORS_aquamarine  Scalar(212,255,127)
#define COLORS_azure  Scalar(255,255,240)
#define COLORS_beige  Scalar(220,245,245)
#define COLORS_bisque  Scalar(196,228,255)
#define COLORS_black  Scalar(0,0,0)
#define COLORS_blanchedalmond  Scalar(205,235,255)
#define COLORS_blue  Scalar(255,0,0)
#define COLORS_blueviolet  Scalar(226,43,138)
#define COLORS_brown  Scalar(42,42,165)
#define COLORS_burlywood  Scalar(135,184,222)
#define COLORS_cadetblue  Scalar(160,158,95)
#define COLORS_chartreuse  Scalar(0,255,127)
#define COLORS_chocolate  Scalar(30,105,210)
#define COLORS_coral  Scalar(80,127,255)
#define COLORS_cornflowerblue  Scalar(237,149,100)
#define COLORS_cornsilk  Scalar(220,248,255)
#define COLORS_crimson  Scalar(60,20,220)
#define COLORS_cyan  Scalar(255,255,0)
#define COLORS_darkblue  Scalar(139,0,0)
#define COLORS_darkcyan  Scalar(139,139,0)
#define COLORS_darkgoldenrod  Scalar(11,134,184)
#define COLORS_darkgray  Scalar(169,169,169)
#define COLORS_darkgreen  Scalar(0,100,0)
#define COLORS_darkgrey  Scalar(169,169,169)
#define COLORS_darkkhaki  Scalar(107,183,189)
#define COLORS_darkmagenta  Scalar(139,0,139)
#define COLORS_darkolivegreen  Scalar(47,107,85)
#define COLORS_darkorange  Scalar(0,140,255)
#define COLORS_darkorchid  Scalar(204,50,153)
#define COLORS_darkred  Scalar(0,0,139)
#define COLORS_darksalmon  Scalar(122,150,233)
#define COLORS_darkseagreen  Scalar(143,188,143)
#define COLORS_darkslateblue  Scalar(139,61,72)
#define COLORS_darkslategray  Scalar(79,79,47)
#define COLORS_darkslategrey  Scalar(79,79,47)
#define COLORS_darkturquoise  Scalar(209,206,0)
#define COLORS_darkviolet  Scalar(211,0,148)
#define COLORS_deeppink  Scalar(147,20,255)
#define COLORS_deepskyblue  Scalar(255,191,0)
#define COLORS_dimgray  Scalar(105,105,105)
#define COLORS_dimgrey  Scalar(105,105,105)
#define COLORS_dodgerblue  Scalar(255,144,30)
#define COLORS_firebrick  Scalar(34,34,178)
#define COLORS_floralwhite  Scalar(240,250,255)
#define COLORS_forestgreen  Scalar(34,139,34)
#define COLORS_fuchsia  Scalar(255,0,255)
#define COLORS_gainsboro  Scalar(220,220,220)
#define COLORS_ghostwhite  Scalar(255,248,248)
#define COLORS_gold  Scalar(0,215,255)
#define COLORS_goldenrod  Scalar(32,165,218)
#define COLORS_gray  Scalar(128,128,128)
#define COLORS_green  Scalar(0,128,0)
#define COLORS_greenyellow  Scalar(47,255,173)
#define COLORS_grey  Scalar(128,128,128)
#define COLORS_honeydew  Scalar(240,255,240)
#define COLORS_hotpink  Scalar(180,105,255)
#define COLORS_indianred  Scalar(92,92,205)
#define COLORS_indigo  Scalar(130,0,75)
#define COLORS_ivory  Scalar(240,255,255)
#define COLORS_khaki  Scalar(140,230,240)
#define COLORS_lavender  Scalar(250,230,230)
#define COLORS_lavenderblush  Scalar(245,240,255)
#define COLORS_lawngreen  Scalar(0,252,124)
#define COLORS_lemonchiffon  Scalar(205,250,255)
#define COLORS_lightblue  Scalar(230,216,173)
#define COLORS_lightcoral  Scalar(128,128,240)
#define COLORS_lightcyan  Scalar(255,255,224)
#define COLORS_lightgoldenrodyellow  Scalar(210,250,250)
#define COLORS_lightgray  Scalar(211,211,211)
#define COLORS_lightgreen  Scalar(144,238,144)
#define COLORS_lightgrey  Scalar(211,211,211)
#define COLORS_lightpink  Scalar(193,182,255)
#define COLORS_lightsalmon  Scalar(122,160,255)
#define COLORS_lightseagreen  Scalar(170,178,32)
#define COLORS_lightskyblue  Scalar(250,206,135)
#define COLORS_lightslategray  Scalar(153,136,119)
#define COLORS_lightslategrey  Scalar(153,136,119)
#define COLORS_lightsteelblue  Scalar(222,196,176)
#define COLORS_lightyellow  Scalar(224,255,255)
#define COLORS_lime  Scalar(0,255,0)
#define COLORS_limegreen  Scalar(50,205,50)
#define COLORS_linen  Scalar(230,240,250)
#define COLORS_magenta  Scalar(255,0,255)
#define COLORS_maroon  Scalar(0,0,128)
#define COLORS_mediumaquamarine  Scalar(170,205,102)
#define COLORS_mediumblue  Scalar(205,0,0)
#define COLORS_mediumorchid  Scalar(211,85,186)
#define COLORS_mediumpurple  Scalar(219,112,147)
#define COLORS_mediumseagreen  Scalar(113,179,60)
#define COLORS_mediumslateblue  Scalar(238,104,123)
#define COLORS_mediumspringgreen  Scalar(154,250,0)
#define COLORS_mediumturquoise  Scalar(204,209,72)
#define COLORS_mediumvioletred  Scalar(133,21,199)
#define COLORS_midnightblue  Scalar(112,25,25)
#define COLORS_mintcream  Scalar(250,255,245)
#define COLORS_mistyrose  Scalar(225,228,255)
#define COLORS_moccasin  Scalar(181,228,255)
#define COLORS_navajowhite  Scalar(173,222,255)
#define COLORS_navy  Scalar(128,0,0)
#define COLORS_oldlace  Scalar(230,245,253)
#define COLORS_olive  Scalar(0,128,128)
#define COLORS_olivedrab  Scalar(35,142,107)
#define COLORS_orange  Scalar(0,165,255)
#define COLORS_orangered  Scalar(0,69,255)
#define COLORS_orchid  Scalar(214,112,218)
#define COLORS_palegoldenrod  Scalar(170,232,238)
#define COLORS_palegreen  Scalar(152,251,152)
#define COLORS_paleturquoise  Scalar(238,238,175)
#define COLORS_palevioletred  Scalar(147,112,219)
#define COLORS_papayawhip  Scalar(213,239,255)
#define COLORS_peachpuff  Scalar(185,218,255)
#define COLORS_peru  Scalar(63,133,205)
#define COLORS_pink  Scalar(203,192,255)
#define COLORS_plum  Scalar(221,160,221)
#define COLORS_powderblue  Scalar(230,224,176)
#define COLORS_purple  Scalar(128,0,128)
#define COLORS_red  Scalar(0,0,255)
#define COLORS_rosybrown  Scalar(143,143,188)
#define COLORS_royalblue  Scalar(225,105,65)
#define COLORS_saddlebrown  Scalar(19,69,139)
#define COLORS_salmon  Scalar(114,128,250)
#define COLORS_sandybrown  Scalar(96,164,244)
#define COLORS_seagreen  Scalar(87,139,46)
#define COLORS_seashell  Scalar(238,245,255)
#define COLORS_sienna  Scalar(45,82,160)
#define COLORS_silver  Scalar(192,192,192)
#define COLORS_skyblue  Scalar(235,206,135)
#define COLORS_slateblue  Scalar(205,90,106)
#define COLORS_slategray  Scalar(144,128,112)
#define COLORS_slategrey  Scalar(144,128,112)
#define COLORS_snow  Scalar(250,250,255)
#define COLORS_springgreen  Scalar(127,255,0)
#define COLORS_steelblue  Scalar(180,130,70)
#define COLORS_tan  Scalar(140,180,210)
#define COLORS_teal  Scalar(128,128,0)
#define COLORS_thistle  Scalar(216,191,216)
#define COLORS_tomato  Scalar(71,99,255)
#define COLORS_turquoise  Scalar(208,224,64)
#define COLORS_violet  Scalar(238,130,238)
#define COLORS_wheat  Scalar(179,222,245)
#define COLORS_white  Scalar(255,255,255)
#define COLORS_whitesmoke  Scalar(245,245,245)
#define COLORS_yellow  Scalar(0,255,255)
#define COLORS_yellowgreen  Scalar(50,205,154)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号