8000 objdetect: added ColorNames features · opencv/opencv@8bef2ba · GitHub
[go: up one dir, main page]

Skip to content

Commit 8bef2ba

Browse files
committed
objdetect: added ColorNames features
1 parent 9f7793c commit 8bef2ba

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

modules/objdetect/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,22 @@ if(HAVE_QUIRC)
1717
ocv_include_directories(${QUIRC_INCLUDE})
1818
ocv_target_link_libraries(${the_module} quirc)
1919
endif()
20+
21+
set(FNAME "color_names.yaml.gz")
22+
set(DEST "${CMAKE_BINARY_DIR}/downloads/colornames")
23+
ocv_download(
24+
FILENAME "${FNAME}"
25+
HASH "7dbf9cbb3a0688251b536a7101f2acfd"
26+
URL
27+
"https://raw.githubusercontent.com/mshabunin/opencv_3rdparty/ee6a9bab05b623473f62b5b662c47d5dc6c0cacd/"
28+
DESTINATION_DIR "${DEST}"
29+
ID "colornames"
30+
RELATIVE_URL
31+
STATUS res
32+
)
33+
if(NOT res)
34+
message(FATAL_ERROR "ColorNames table failed to download")
35+
else()
36+
install(FILES "${DEST}/${FNAME}" DESTINATION "${OPENCV_OTHER_INSTALL_PATH}/colornames" COMPONENT libs)
37+
endif()
38+

modules/objdetect/include/opencv2/objdetect.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,19 @@ struct CV_EXPORTS_W HOGDescriptor
702702
};
703703
//! @}
704704

705+
//! @addtogroup objdetect_colornames
706+
//! @{
707+
class CV_EXPORTS_W ColorNamesFeatures
708+
{
709+
public:
710+
typedef Vec<uchar, 3> PixType;
711+
typedef Vec<float, 10> FeatureType;
712+
virtual ~ColorNamesFeatures() {}
713+
CV_WRAP virtual void compute(InputArray image_patch, OutputArray feature_vector) = 0;
714+
static Ptr<ColorNamesFeatures> create(const std::string & table_file);
715+
};
716+
//! @}
717+
705718
//! @addtogroup objdetect_qrcode
706719
//! @{
707720

modules/objdetect/src/color_names.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html.
4+
5+
#include "precomp.hpp"
6+
7+
namespace cv {
8+
9+
class ColorNamesImpl : public ColorNamesFeatures
10+
{
11+
public:
12+
ColorNamesImpl();
13+
bool read(const std::string & table_file);
14+
void compute(InputArray image_patch, OutputArray feature_vector) CV_OVERRIDE;
15+
private:
16+
Mat table;
17+
const int LEN = 32768;
18+
};
19+
20+
ColorNamesImpl::ColorNamesImpl()
21+
{
22+
}
23+
24+
bool ColorNamesImpl::read(const std::string & table_file)
25+
{
26+
FileStorage fs(table_file, FileStorage::READ);
27+
if (!fs.isOpened())
28+
return false;
29+
FileNode node = fs["ColorNames"];
30+
if (node.empty())
31+
return false;
32+
table = node.mat();
33+
if ((table.size() != Size(10, LEN)) || (table.type() != CV_32FC1))
34+
{
35+
table.release();
36+
return false;
37+
}
38+
return true;
39+
}
40+
41+
void ColorNamesImpl::compute(InputArray image_patch, OutputArray feature_vector)
42+
{
43+
CV_CheckType(image_patch.type(), image_patch.type() == CV_8UC3, "BGR image expected");
44+
Mat image_patch_rgb555;
45+
cvtColor(image_patch, image_patch_rgb555, COLOR_RGB2BGR555);
46+
// TODO: why RGB555 is 8UC2?
47+
Mat patch(image_patch.size(), CV_16UC1, image_patch_rgb555.data);
48+
CV_CheckType(patch.type(), patch.type() == CV_16UC1, "Internal error");
49+
50+
feature_vector.create(image_patch.size(), CV_32FC(10));
51+
Mat feature_mat = feature_vector.getMat();
52+
53+
Mat ftable(Size(1, LEN), CV_32FC(10), table.data);
54+
for (int i = 0; i < patch.rows; i++)
55+
{
56+
for (int j = 0; j < patch.cols; j++)
57+
{
58+
typedef uint16_t Pix;
59+
typedef Vec<float, 10> Feat;
60+
const Pix pix = patch.at<Pix>(i, j);
61+
const Feat val = ftable.at<Feat>(pix);
62+
feature_mat.at<Feat>(i, j) = val;
63+
}
64+
}
65+
}
66+
67+
Ptr<ColorNamesFeatures> ColorNamesFeatures::create(const std::string & table_file)
68+
{
69+
cv::Ptr<ColorNamesImpl> res = std::make_shared<ColorNamesImpl>();
70+
if (!res->read(table_file))
71+
return NULL;
72+
return res;
73+
}
74+
75+
} // cv::
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html.
4+
5+
#include "test_precomp.hpp"
6+
7+
using namespace std;
8+
9+
namespace opencv_test{namespace{
10+
11+
TEST(ColorNames, test)
12+
{
13+
const std::string fname = findDataFile("cascadeandhog/color_names.yaml.gz");
14+
ASSERT_FALSE(fname.empty());
15+
Ptr<ColorNamesFeatures> cnames = ColorNamesFeatures::create(fname);
16+
ASSERT_TRUE(cnames);
17+
const Size SZ(100, 100);
18+
Mat img = cvtest::randomMat(theRNG(), SZ, CV_8UC3, 0, 255, false);
19+
img.at<ColorNamesFeatures::PixType>(99, 99) = {255, 255, 255};
20+
Mat features;
21+
cnames->compute(img, features);
22+
ASSERT_EQ(features.type(), CV_32FC(10));
23+
ASSERT_EQ(features.size(), SZ);
24+
const float last_item = features.at<ColorNamesFeatures::FeatureType>(99, 99)[0];
25+
ASSERT_NEAR(last_item, 0.0087778, 0.00001);
26+
}
27+
28+
}} // opencv_test::<anonymous>::

0 commit comments

Comments
 (0)
0