Commit cbc1f345 authored by Tadej Borovšak's avatar Tadej Borovšak

Convert to C++

parent bdf75c0d
CXX := g++
CXXFLAGS := $(shell pkg-config --cflags opencv) -Wall -Wextra
LDFLAGS := $(shell pkg-config --libs opencv) -Wall -Wextra
APPS := \
mean-shift
all: $(APPS)
.PHONY: clean
clean:
rm -f *.o $(APPS)
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <math.h>
#define SHOW_STEPS 1
using namespace std;
using namespace cv;
static void
meshgrid(Rect const &r,
Mat &xx,
Mat &yy)
{
Mat x (1, r.width, CV_32F);
Mat y (r.height, 1, CV_32F);
for (int i = 0; i < r.width; i++)
x.at<float> (0, i) = r.x + i;
for (int i = 0; i < r.height; i++)
y.at<float> (i, 0) = r.y + i;
repeat (x, r.height, 1, xx);
repeat (y, 1, r.width, yy);
}
static void
mean_shift (Mat const &res,
Mat const &kernel,
Point &p)
{
Point old (-1, -1);
Size s = kernel.size ();
Point b = (s - Size (1, 1)) / 2;
// Adjust for border correction
p = p + b;
Mat tmp;
copyMakeBorder (res, tmp, b.y, b.y, b.x, b.x, BORDER_CONSTANT, 0);
#ifdef SHOW_STEPS
double m;
minMaxLoc (res, NULL, &m);
Mat out;
res.convertTo (out, CV_8U, 255 / m);
cvtColor (out, out, COLOR_GRAY2BGR);
#endif /* SHOW_STEPS */
Mat xx, yy, zz;
while (old != p)
{
old = p;
#ifdef SHOW_STEPS
out.at<Vec3b> (p - b) = Vec3b (0, 0, 255);
#endif /* SHOW_STEPS */
Rect r (p - b, s);
meshgrid (r, xx, yy);
zz = Mat (tmp, r).mul (kernel);
xx = xx.mul (zz);
yy = yy.mul (zz);
float den = sum (zz)[0];
p = Point (sum (xx)[0] / den, sum (yy)[0] / den);
}
p = p - b;
#ifdef SHOW_STEPS
imwrite ("test.png", out);
resize (out, out, Size (), 5, 5, INTER_CUBIC);
imshow ("test", out);
waitKey (0);
#endif /* SHOW_STEPS */
}
int
main (int argc,
char **argv)
{
int ksize = strtol (argv[1], NULL, 10) / 2 * 2 + 1;
Size size (100, 100);
Mat res = Mat::zeros (size, CV_32F);
res.at<float> (70, 50) = 1.0f;
res.at<float> (50, 70) = 0.5f;
GaussianBlur (res, res, Size (), 10);
#if 0
Mat kernel = Mat::ones (ksize, ksize, CV_32F);
#else
Mat kernel = getStructuringElement (MORPH_ELLIPSE, Size(ksize, ksize));
kernel.convertTo (kernel, CV_32F);
#endif
Point p (60, 40);
mean_shift (res, kernel, p);
cout << p << endl;
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment