Skip to content

Commit 849eb70

Browse files
committed
First commit
1 parent 3054618 commit 849eb70

File tree

4 files changed

+398
-0
lines changed

4 files changed

+398
-0
lines changed

modules/ximgproc/include/opencv2/ximgproc.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "ximgproc/brightedges.hpp"
5959
#include "ximgproc/run_length_morphology.hpp"
6060
#include "ximgproc/edgepreserving_filter.hpp"
61+
#include "ximgproc/color_match.hpp"
6162

6263

6364
/** @defgroup ximgproc Extended Image Processing
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
#ifndef __OPENCV_COLOR_MATCH_HPP__
6+
#define __OPENCV_COLOR_MATCH_HPP__
7+
#ifdef __cplusplus
8+
9+
#include <opencv2/core.hpp>
10+
11+
namespace cv {
12+
namespace ximgproc {
13+
14+
//! @addtogroup ximgproc_filters
15+
//! @{
16+
17+
/**
18+
* @brief creates a quaternion image.
19+
*
20+
* @param img Source 8-bit, 32-bit or 64-bit image, with 3-channel image.
21+
* @param qimg result CV_64FC4 a quaternion image( 4 chanels zero channel and B,G,R).
22+
*/
23+
CV_EXPORTS_W void createQuaternionImage(InputArray img, OutputArray qimg);
24+
25+
/**
26+
* @brief calculates conjugate of a quaternion image.
27+
*
28+
* @param qimg quaternion image.
29+
* @param qcimg conjugate of qimg
30+
*/
31+
CV_EXPORTS_W void qconj(InputArray qimg, OutputArray qcimg);
32+
/**
33+
* @brief divides each element by its modulus.
34+
*
35+
* @param qimg quaternion image.
36+
* @param qnimg conjugate of qimg
37+
*/
38+
CV_EXPORTS_W void qunitary(InputArray qimg, OutputArray qnimg);
39+
/**
40+
* @brief Calculates the per-element quaternion product of two arrays
41+
*
42+
* @param src1 quaternion image.
43+
* @param src2 quaternion image.
44+
* @param dst product dst(I)=src1(I) . src2(I)
45+
*/
46+
CV_EXPORTS_W void qmultiply(InputArray src1, InputArray src2, OutputArray dst);
47+
/**
48+
* @brief Performs a forward or inverse Discrete quaternion Fourier transform of a 2D quaternion array.
49+
*
50+
* @param img quaternion image.
51+
* @param qimg quaternion image in dual space.
52+
* @param flags quaternion image in dual space. only DFT_INVERSE flags is supported
53+
* @param sideLeft true the hypercomplex exponential is to be multiplied on the left (false on the right ).
54+
*/
55+
CV_EXPORTS_W void qdft(InputArray img, OutputArray qimg, int flags, bool sideLeft);
56+
/**
57+
* @brief Compares a color template against overlapped color image regions.
58+
*
59+
* @param img Image where the search is running. It must be 3 channels image
60+
* @param templ Searched template. It must be not greater than the source image and have 3 channels
61+
* @param result Map of comparison results. It must be single-channel 64-bit floating-point
62+
*/
63+
CV_EXPORTS_W void colorMatchTemplate(InputArray img, InputArray templ, OutputArray result);
64+
65+
}
66+
}
67+
#endif
68+
#endif
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#include <iostream>
2+
#include <opencv2/core.hpp>
3+
#include <opencv2/core/utility.hpp>
4+
#include <opencv2/highgui.hpp>
5+
#include <opencv2/imgproc.hpp>
6+
#include <opencv2/ximgproc.hpp>
7+
#include <opencv2/ximgproc/color_match.hpp>
8+
9+
using namespace std;
10+
using namespace cv;
11+
12+
13+
14+
static void AddSlider(String sliderName, String windowName, int minSlider, int maxSlider, int valDefault, int *valSlider, void(*f)(int, void *), void *r)
15+
{
16+
createTrackbar(sliderName, windowName, valSlider, 1, f, r);
17+
setTrackbarMin(sliderName, windowName, minSlider);
18+
setTrackbarMax(sliderName, windowName, maxSlider);
19+
setTrackbarPos(sliderName, windowName, valDefault);
20+
}
21+
22+
struct SliderData {
23+
Mat img;
24+
int thresh;
25+
vector<Point> pRef;
26+
};
27+
28+
static void UpdateThreshImage(int , void *r)
29+
{
30+
SliderData *p = (SliderData*)r;
31+
Mat dst,labels,stats,centroids;
32+
33+
threshold(p->img, dst, p->thresh, 255, THRESH_BINARY);
34+
35+
connectedComponentsWithStats(dst, labels, stats, centroids, 8);
36+
if (centroids.rows < 10)
37+
{
38+
cout << "**********************************************************************************\n";
39+
for (int i = 0; i < static_cast<int>(p->pRef.size()); i++)
40+
{
41+
cout << p->pRef[i] << "\n";
42+
}
43+
cout << "---\n";
44+
for (int i = 0; i < centroids.rows; i++)
45+
{
46+
cout << dst.cols - centroids.at<double>(i, 0) << " ";
47+
cout << dst.rows - centroids.at<double>(i, 1) << "\n";
48+
}
49+
cout << "----------------------------------------------------------------------------------\n";
50+
}
51+
flip(dst, dst, -1);
52+
53+
imshow("Max Quaternion corr",dst);
54+
}
55+
56+
int main(int , char *[])
57+
{
58+
#define TESTMATCHING
59+
60+
#ifdef TESTMATCHING
61+
Mat imgLogo = imread("g:/lib/opencv/samples/data/opencv-logo.png", IMREAD_COLOR);
62+
Mat fruits = imread("g:/lib/opencv/samples/data/lena.jpg", IMREAD_COLOR);
63+
fruits = fruits ;
64+
// resize(fruits,fruits, Size(), 0.5, 0.5);
65+
Mat img,colorTemplate;
66+
imgLogo(Rect(0, 0, imgLogo.cols, 580)).copyTo(img);
67+
resize(img, colorTemplate, Size(), 0.05, 0.05);
68+
vector<Mat> colorMask(4);
69+
inRange(colorTemplate, Vec3b(255, 255, 255), Vec3b(255, 255, 255), colorMask[0]);
70+
// colorTemplate.setTo(Scalar(0,0,0), colorMask[0]);
71+
72+
inRange(colorTemplate, Vec3b(255, 0, 0), Vec3b(255, 0, 0), colorMask[0]);
73+
inRange(colorTemplate, Vec3b(0, 255, 0), Vec3b(0,255, 0), colorMask[1]);
74+
inRange(colorTemplate, Vec3b( 0, 0,255), Vec3b( 0, 0,255), colorMask[2]);
75+
colorMask[3] = Mat(colorTemplate.size(), CV_8UC3, Scalar(255));
76+
SliderData ps;
77+
RNG r;
78+
for (int i = 0; i < 16; i++)
79+
{
80+
Point p(i / 4 * 130 + 10+r.uniform(-10,10), (i % 4) * 130 + 10+r.uniform(-10, 10));
81+
Mat newLogo= colorTemplate.clone();
82+
if (i % 6 != 5)
83+
{
84+
newLogo.setTo(Scalar(r.uniform(0, 256), r.uniform(0, 256), r.uniform(0, 256)), colorMask[i % 4]);
85+
newLogo.setTo(Scalar(r.uniform(0, 256), r.uniform(0, 256), r.uniform(0, 256)), colorMask[(i + 1) % 4]);
86+
}
87+
else
88+
ps.pRef.push_back(p);
89+
newLogo.copyTo(fruits(Rect(p.x, p.y, newLogo.cols, newLogo.rows)));
90+
91+
}
92+
#else
93+
Mat fruits = imread("c2.png", IMREAD_COLOR);
94+
Mat colorTemplate = imread("c1.png", IMREAD_COLOR);
95+
Mat img= colorTemplate;
96+
#endif
97+
imshow("Image", fruits);
98+
imshow("opencv_logo", colorTemplate);
99+
100+
if (img.empty())
101+
{
102+
cout << "Cannot load image file\n";
103+
return 0;
104+
}
105+
Mat imgcorr;
106+
ximgproc::colorMatchTemplate(fruits, colorTemplate, imgcorr);
107+
imshow("quaternion correlation real", imgcorr);
108+
normalize(imgcorr, imgcorr,1,0,NORM_MINMAX);
109+
imgcorr.convertTo(ps.img, CV_8U, 255);
110+
imshow("quaternion correlation", imgcorr);
111+
AddSlider("Level", "quaternion correlation", 0, 255, ps.thresh, &ps.thresh, UpdateThreshImage, &ps);
112+
int code = 0;
113+
while (code != 27)
114+
{
115+
code = waitKey(50);
116+
}
117+
118+
FileStorage fs("corr.yml", FileStorage::WRITE);
119+
fs<<"Image"<< imgcorr;
120+
fs.release();
121+
waitKey(0);
122+
return 0;
123+
}

0 commit comments

Comments
 (0)