Skip to content

Instantly share code, notes, and snippets.

@jameslittle230
Last active March 4, 2018 19:44
Show Gist options
  • Save jameslittle230/a734445bd2eea45ec955847aeeebb14c to your computer and use it in GitHub Desktop.
Save jameslittle230/a734445bd2eea45ec955847aeeebb14c to your computer and use it in GitHub Desktop.
HW5 Submission: Feature Detection
void R2Image::
Harris(double sigma)
{
std::cout << "Computing harris filter" << std::endl;
const R2Image self = *this;
R2Image *t1 = new R2Image(self); t1->SobelX(); t1->Square();
R2Image *t2 = new R2Image(self); t2->SobelY(); t2->Square();
R2Image *t3 = new R2Image(Width(), Height());
R2Image *t4 = new R2Image(Width(), Height());
// Set T3 to product of T1 and T2
for(int x=0; x<Width(); x++) {
for(int y=0; y<Height(); y++) {
double v = t1->Pixel(x, y)[0] * t2->Pixel(x, y).Red();
t3->Pixel(x, y).Reset(v, v, v, 1);
}
}
t1->Blur(2);
t2->Blur(2);
t3->Blur(2);
for(int x=0; x<Width(); x++) {
for(int y=0; y<Height(); y++) {
double t1v = t1->Pixel(x, y)[0];
double t2v = t2->Pixel(x, y)[0];
double t3v = t3->Pixel(x, y)[0];
double v = t1v * t2v - t3v * t3v - 0.04 * ((t1v + t2v) * (t1v + t2v));
v += 0.5;
t4->Pixel(x, y).Reset(v, v, v, 1);
}
}
std::cout << "Generating feature list" << std::endl;
std::vector<Feature> features;
for(int x=0; x<Width(); x++) {
for(int y=0; y<Height(); y++) {
R2Pixel p = t4->Pixel(x, y);
double v = p[0];
double sensitivity = 0.5;
if(v > sensitivity) {
features.push_back(Feature(x, y, p));
}
}
}
std::cout << "Marking best features" << std::endl;
std::sort(features.begin(), features.end());
std::reverse(features.begin(), features.end());
int ct=0, index=0;
while(ct < 150 && index < features.size()) {
bool skip = false;
Feature ft = features.at(index);
for(int i=0; i<index; i++) {
if(ft.closeTo(features.at(i))) {
skip = true;
break;
}
}
if(!skip) {
int m, n;
for(m=-4; m<=4; m++) {
for(n=-4; n<=4; n++) {
Pixel(ft.centerX+m, ft.centerY+n).Reset(1, 0, 0, 1);
}
}
ct++;
}
index++;
}
}
void R2Image::
Square()
{
int x, y;
double r, g, b;
for(x=0; x<width; x++) {
for(y=0; y<height; y++) {
r = Pixel(x, y).Red() * Pixel(x, y).Red();
g = Pixel(x, y).Green() * Pixel(x, y).Green();
b = Pixel(x, y).Blue() * Pixel(x, y).Blue();
Pixel(x, y).Reset(r, g, b, 1);
}
}
}
struct Feature
{
int centerX;
int centerY;
R2Pixel HarrisValue;
Feature()
{
centerX = -1;
centerY = -1;
}
Feature(int x, int y, R2Pixel val)
{
centerX = x;
centerY = y;
HarrisValue = val;
}
bool operator<(const Feature &feature) const
{
double valueIntensity = HarrisValue[0] + HarrisValue[1] + HarrisValue[2];
double featureIntensity = feature.HarrisValue[0] + feature.HarrisValue[1] + feature.HarrisValue[2];
return valueIntensity < featureIntensity;
}
bool closeTo(Feature newFeat) {
int xdist = abs(centerX - newFeat.centerX);
int ydist = abs(centerY - newFeat.centerY);
return (xdist < 20 && ydist < 20);
}
};
struct CoordinatePair
{
int x;
int y;
CoordinatePair()
{
x = -1;
y = -1;
}
CoordinatePair(int newX, int newY)
{
x = newX;
y = newY;
}
std::vector<CoordinatePair> neighbors()
{
std::vector<CoordinatePair> output;
output.push_back(CoordinatePair(x+1, y));
output.push_back(CoordinatePair(x, y+1));
output.push_back(CoordinatePair(x-1, y));
output.push_back(CoordinatePair(x, y-1));
return output;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment