Image processing 在图像中定位多个斑点
对于一门计算机科学课,我应该写一个程序,在这样一张图片中找到未知数量外星人的位置和大小:。我目前拥有以下代码:Image processing 在图像中定位多个斑点,image-processing,myro,Image Processing,Myro,对于一门计算机科学课,我应该写一个程序,在这样一张图片中找到未知数量外星人的位置和大小:。我目前拥有以下代码: #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
#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;
}
#包括“Myro.h”
#包括
#包括
#包括
#包括“alien.h”
#包括
使用std::cout;
使用std::cin;
使用std::endl;
使用std::vector;
void trackBlob(PicturePtr&aPic,int x,int y);
int BlobSize(PicturePtr和aPic、int x、int y、vector和xLocs、vector和yloc);
发现病媒;
返回int;
int main()
{
返回=0;
cout选择;
图片接受图片;
//选择要搜索的图片
布尔选择=真;
同时(选择){
开关(选择){
案例0:
thePic=makePicture(“MARS.jpg”);
选择=假;
打破
案例1:
thePic=makePicture(“MARS1.jpg”);
选择=假;
打破
案例2:
thePic=makePicture(“MARS2.jpg”);
选择=假;
打破
案例3:
thePic=makePicture(“MARS3.jpg”);
选择=假;
打破
案例4:
thePic=makePicture(“MARS4.jpg”);
选择=假;
打破
案例5:
thePic=makePicture(“MARS6.jpg”);
选择=假;
打破
案例6:
cout首先,您应该删除BlobSize()中的注释,如下所示:
由于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);
}
}
for(int i=-1;i首先,您应该删除BlobSize()中的注释,如下所示:
由于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);
}
}
for(int i=-1;我能为示例输入添加一个自包含的小示例吗?你是指分析一张图片的输出示例吗?我指的是一个自包含的代码,也是那些MARS.JPG图片中的一张,包括你得到的确切结果和你期望得到的结果。你发布的代码取决于一些“alien.h”、“Myro.h”“alien”类只有三个变量—(位置)x、y和大小。Myro库是访问图片的方式,因此无法真正集成到代码中。PicturePtr是图片对象。大多数操作都是通过从图片中的坐标获取像素来完成的,主要是RGB值。使用此图片:,程序成功地将alien上的几乎所有像素更改为b缺少,但列出了找到的214个外星人。它应该只有一个。外星人的大小从300到1400不等。输入图片位于bcf.usc.edu/~stejada/csci101/Pix/MARS1.jpg,我得到一个docs.google.com/open?id=0B79nx_A6upx6bdv4rtl3bvjwta的输出图像。对于这个过程:对于图像中的每个像素,检查它是否是我感兴趣的颜色教育署署长。如果禁用,则移动到下一个像素。如果启用,则跟踪斑点。递归检查每个周围像素并返回1+所有周围像素的大小。如果禁用,则返回0。每次检查像素时,其颜色都会更改为黑色,以防再次检查。是否可以添加一个包含示例i的小型示例nput?你是指分析一张图片的输出示例吗?我指的是一个自包含的代码,也是那些MARS.JPG图片中的一张,包括你得到的确切结果和你期望得到的结果。你发布的代码取决于一些“alien.h”、“Myro.h”等,它们不是自包含的。“alien”类只有三个变量—(位置)x、y和大小。Myro库是访问图片的方式,因此无法真正集成到代码中。PicturePtr是图片对象。大多数操作都是通过从图片中的坐标获取像素来完成的,主要是RGB值。使用此图片:,程序成功地将alien上的几乎所有像素更改为b缺少,但列出了找到的214个外星人。它应该只有一个。外星人的大小从300到1400不等。输入图片位于bcf.usc.edu/~stejada/csci101/Pix/MARS1.jpg,我得到一个docs.google.com/open?id=0B79nx_A6upx6bdv4rtl3bvjwta的输出图像。对于这个过程:对于图像中的每个像素,检查它是否是我感兴趣的颜色教育署署长。如果禁用,则移动到下一个像素。如果启用,则跟踪斑点。递归检查每个周围像素并返回1+所有周围像素的大小。如果禁用,则返回0。每次检查像素时,其颜色都会更改为黑色,以防再次检查。此外,我会检查我的解决方案,但我没有y我们的alien.h文件,我懒得重写它。另外,我本想检查我的解决方案,但我没有你的alien.h文件,我懒得重写它。