Python 三维图像中的对象数
我试图计算3D图像中对象的数量,我在Python中发现了以下代码:Python 三维图像中的对象数,python,c++,image-processing,recursion,Python,C++,Image Processing,Recursion,我试图计算3D图像中对象的数量,我在Python中发现了以下代码: def count_objects(image): width, height = len(image[0]), len(image) objects_members = [(x, y) for x in range(width) for y in range(height) if image[y][x] == 1] objects_count = 0 while objects_members
def count_objects(image):
width, height = len(image[0]), len(image)
objects_members = [(x, y) for x in range(width) for y in range(height) if image[y][x] == 1]
objects_count = 0
while objects_members != []:
remove_object(objects_members, objects_members.pop(0))
objects_count += 1
return objects_count
def remove_object(objects_members, start_point):
x, y = start_point
connex = [(x - 1, y), (x, y - 1),
(x + 1, y), (x, y + 1)]
for point in connex:
try:
objects_members.remove(point)
remove_object(objects_members, point)
except ValueError:
pass
if __name__ == "__main__":
image = []
while True:
line = input()
if line == "":
break
image.append([int(pixel) for pixel in line])
print(count_objects(image))
该方法识别属于某个对象(设置为1)的像素,然后删除其中一个、其邻居和其邻居的邻居(删除对象),并重复该操作,直到删除所有像素
我试图在C++中转换这个代码,但是我的递归函数似乎并没有停止。以下是我的代码(RemoveObject函数):
void RemoveObject(向量,int起点)
{
int*索引邻居;
无符号字符*邻居;
邻居=邻居26(图像,起点);//26 Connex邻居
IndexNeighbor=IndexNeighbory26(起点);//26 Connex Neighbory26的索引
向量Connex;
对于(int i=0;我怀疑您希望通过引用传递向量。非常感谢,它起了作用。但现在的问题是,该函数导致堆栈溢出,当我在小3D图像上尝试时,它工作正常,但当我在稍大一点的图像上尝试时,它会导致堆栈溢出。有解决方案吗??
void RemoveObject(vector<int> Vector, int start_point)
{
int *IndexNeighbor;
unsigned char *Neighbor;
Neighbor=Neighborhood26(Image, start_point); //26-Connex neighborhood
IndexNeighbor=IndexNeighborhood26(start_point);// the index of 26-Connex neighborhood
vector<int> Connex;
for(int i=0; i<26;i++)
{
bool isIN=false;
if((int)Neighbor[i]==1)
{
for(int k=0; k<Vector.size();k++)
{if(IndexNeighbor[i]==Vector[k]){isIN=true;}}
}
if(isIN==true){Connex.push_back(IndexNeighbor[i]);}
}
if(Connex.size()==0){cout<<"Vector empty"<<endl;}
else
{
for(int j=0; j<Connex.size(); j++)
{
Vector.erase(std::remove(Vector.begin(), Vector.end(), Connex[j]), Vector.end());
RemoveObject(Vector, Connex[j]);
}
}
}