在图像中定位多个斑点

时间:2012-12-03 20:19:32

标签: image-processing myro

对于计算机科学课,我应该编写一个程序,在这样的图片中找到未知数量的外星人的位置和大小:http://www-bcf.usc.edu/~stejada/csci101/Pix/MARS2.jpg。我目前有以下代码:

#include "Myro.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include "alien.h"
#include <vector>

using std::cout;
using std::cin;
using std::endl;
using std::vector;

void trackBlob(PicturePtr &aPic, int x, int y);
int BlobSize(PicturePtr &aPic, int x, int y, vector<int>& xLocs, vector<int>& yLocs);

vector<alien> found;
int returned;

int main()
{
returned = 0;
cout << "Pick a picture to search 0-5, or choose 6 to quit" << endl;
int selection;
cin >> selection;
PicturePtr thePic;

// Choose a picture to search
bool selecting = true;
while (selecting) {
    switch (selection) {
        case 0:
            thePic = makePicture("MARS.jpg");
            selecting = false;
            break;
        case 1:
            thePic = makePicture("MARS1.jpg");
            selecting = false;
            break;
        case 2:
            thePic = makePicture("MARS2.jpg");
            selecting = false;
            break;
        case 3:
            thePic = makePicture("MARS3.jpg");
            selecting = false;
            break;
        case 4:
            thePic = makePicture("MARS4.jpg");
            selecting = false;
            break;
        case 5:
            thePic = makePicture("MARS6.jpg");
            selecting = false;
            break;
        case 6:
            cout << "Terminating." << endl;
            return 0;
        default:
            cout << "Invalid input. Please try again." << endl << endl;
            break;
    }// end switch
} // end while

// Find the aliens
cout << endl;
cout << "Pic size: " << getHeight(thePic) * getWidth(thePic) << endl << endl;
int numGreen = 0;
for (int i = 0; i < getWidth(thePic); i++) {
    for (int j = 0; j < getHeight(thePic); j++) {
        Pixel pix;
        pix = getPixel(thePic,i,j);

        // Check for alien color
        if (pix.R >= 100 && pix.R <= 120 && pix.G >= 240 && pix.G <= 255 && pix.B >= 1 && pix.B <= 15) {
            trackBlob(thePic,i,j);
            numGreen++;
        }
        else if (pix.R >= 165 && pix.R <= 185 && pix.G >= 215 && pix.G <= 235 && pix.B >= 65 && pix.B <= 85) {
            trackBlob(thePic,i,j);
            numGreen++;
        }
        else if (pix.R >= 130 && pix.R <= 155 && pix.G >= 210 && pix.G <= 230 && pix.B >= 145 && pix.B <= 175) {
            trackBlob(thePic,i,j);
            numGreen;
        }
        else if (pix.R >= 27 && pix.R <= 47 && pix.G <= 265 && pix.G >= 245 && pix.B >= 1 && pix.B <= 10) {
            trackBlob(thePic,i,j);
            numGreen++;
        }
        else if (pix.R >= 240 && pix.R <= 255 && pix.G <= 240 && pix.G >= 255 && pix.B >= 25 && pix.B <= 45) {
            trackBlob(thePic,i,j);
            numGreen++;
        }
        else if (pix.R >= 235 && pix.R <= 255 && pix.G <= 245 && pix.G >= 210 && pix.B >= 200 && pix.B <= 230) {
            trackBlob(thePic,i,j);
            numGreen++;
        }
        else if (pix.R <= 240 && pix.R >= 210 && pix.G <=255 && pix.G >= 240 && pix.B >= 1 && pix.B <= 10) {
            trackBlob(thePic,i,j);
            numGreen++;
        }
    }
}
cout << endl << endl;
cout << "Number of Aliens: " << found.size() << endl;
for (int n = 0; n < found.size(); n++) {
    cout << "Position of Alien #" << n << ": (" << found[n].x << "," << found[n].y << ")" << endl;
    cout << "Size of Alien #" << n << ": " << found[n].size << endl;
}

cout << endl << endl << "Number pixels tried: " << numGreen << endl << endl;
cout << "Times blob returned: " << returned << endl;
show(thePic);
return 0;
}

void trackBlob(PicturePtr &aPic, int x, int y) {
// Just to be safe
Pixel aPixel = getPixel(aPic,x,y);
//if (aPixel.R == 0 && aPixel.G == 0 && aPixel.B == 0)
    //return;

// Find the size of the alien
vector<int> xLocs;
vector<int> yLocs;
int size = BlobSize(aPic,x,y,xLocs,yLocs);
cout << "Blob size: " << size << endl;
returned++;

// Make sure it's big enough to be an alien
if (size < 5) {
    return;
}

alien newAlien;

// Find the position
newAlien.x = 0;
for (int i = 0; i < xLocs.size(); i++) {
    newAlien.x += xLocs[i];
}
newAlien.x = newAlien.x/xLocs.size();

newAlien.y = 0;
for (int j = 0; j < yLocs.size(); j++) {
    newAlien.y += yLocs[j];
}
newAlien.y = newAlien.y/yLocs.size();

found.push_back(newAlien);
}

int BlobSize(PicturePtr &aPic, int x, int y, vector<int>& xLocs, vector<int>& yLocs)
{
if (x >= getWidth(aPic) || x < 0)
    return 0;

Pixel pix = getPixel(aPic,x,y);

// Just to be safe
//if (pix.R == 0 && pix.G == 0 && pix.B == 0)
    //return 0;

bool isAlien = false;
if (pix.R >= 100 && pix.R <= 120 && pix.G >= 240 && pix.G <= 255 && pix.B >= 1 && pix.B <= 15)
    isAlien = true;
else if (pix.R >= 165 && pix.R <= 185 && pix.G >= 215 && pix.G <= 235 && pix.B >= 65 && pix.B <= 85)
    isAlien = true;
else if (pix.R >= 130 && pix.R <= 155 && pix.G >= 210 && pix.G <= 230 && pix.B >= 145 && pix.B <= 175)
    isAlien = true;
else if (pix.R >= 27 && pix.R <= 47 && pix.G <= 265 && pix.G >= 245 && pix.B >= 1 && pix.B <= 10)
    isAlien = true;
else if (pix.R >= 240 && pix.R <= 255 && pix.G <= 240 && pix.G >= 255 && pix.B >= 25 && pix.B <= 45)
    isAlien = true;
else if (pix.R >= 235 && pix.R <= 255 && pix.G <= 245 && pix.G >= 210 && pix.B >= 200 && pix.B <= 230)
    isAlien = true;
else if (pix.R <= 240 && pix.R >= 210 && pix.G <=255 && pix.G >= 240 && pix.B >= 1 && pix.B <= 10)
    isAlien = true;

if (!isAlien)
    return 0;

// Store the location in the position vectors
xLocs.push_back(x);
yLocs.push_back(y);

// Make sure the pixel doesn't get counted again
setPixelColor(aPic,x,y,0,0,0);

int size = 0;

for (int i = 0; i <= 2; i++) {
    for (int j = 0; j <= 2; j++) {
        if (i == 0 && j == 0) {
            break;
        }
        size += BlobSize(aPic,x + (i-1), y + (j-1),xLocs,yLocs);
    }
}

return 1 + size;
}

当我运行代码时,它说它发现了几百个“外星人”。如果有帮助,它找到的每个都比最后一个大。我已经盯着这几天了,不知道出了什么问题。任何帮助将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:4)

首先,您应该删除BlobSize()中的注释,如下所示:

// So pixels aren't counted twice
if (pix.R == 0 && pix.G == 0 && pix.B == 0)
    return 0;

由于BlobSize()将计算的像素更改为黑色,因此您需要在代码中保留这些行,这样就不会将像素计算两次。

另外,BlobSize()末尾的for循环有点错误。它应该是这样的:

for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
        // skip pixel at this location
        if (i == 0 && j == 0) {
            continue;
        }
        size += BlobSize(aPic,x + (i-1), y + (j-1),xLocs,yLocs);
    }
}

之前的方式是检查当前位置的像素上方和左侧的像素,查看i == 0 && j == 0,然后跳过检查第一列。该程序将在其他6个像素(包括当前位置的像素)上运行BlobSize()。

顺便说一下,很好地使用了向量和递归。对不起,如果我回答得太晚了,你已经把它打开了。因为延期,我周日开始进入。