在OpenCV中调用抓取之后,我想获取种子点的所有相邻像素,这些像素在掩码中标记为GC_PR_FGD或GC_FGD。我想到要使用类似这样的搜索算法:
vector<Point> pixels_in_component;
vector<Point> neighbours;
Point starting_point;
void GCApplication::reducedPixels(){
neighbours.push_back(starting_point);
while(!neighbours.empty()){
Point p = neighbours.back();
neighbours.pop_back();
pixels_in_component.push_back(p);
double x = p.x;
double y = p.y;
if(x > 0) //Left
{
uchar left_val = mask.at<uchar>(y,x-1);
if(left_val == GC_FGD || left_val == GC_PR_FGD)
neighbours.push_back(Point(y,x-1));
}
if(y>0 && x>0) //upLeft
{
uchar upLeft_val = mask.at<uchar>(y-1,x-1);
if(upLeft_val == GC_FGD || upLeft_val == GC_PR_FGD)
neighbours.push_back(Point(y-1,x-1));
}
if(y>0) //up
{
uchar up_val = mask.at<uchar>(y-1,x);
if(up_val == GC_FGD || up_val == GC_PR_FGD)
neighbours.push_back(Point(y-1,x));
}
if(y>0 && x<mask.cols-1) //upright
{
uchar upRight_val = mask.at<uchar>(y-1,x+1);
if(upRight_val == GC_FGD || upRight_val == GC_PR_FGD)
neighbours.push_back(Point(y-1,x+1));
}
}
CV_Assert( !mask.empty() );
mask.setTo( GC_BGD );
for(vector<Point>::iterator it = pixels_in_component.begin(); it != pixels_in_component.end(); it++){
mask.at<uchar>(it->y,it->x) = GC_FGD;
}
}
但是问题是它没有检测到所有相邻像素。实际上,它检测到的很少。我正在使用here中的grabCut。