コード例 #1
0
// This function is called at the end of "addToContourLast()". Function 
// "contourInterior()" returns volume of the closed contour's interior region 
// (including points on the contor itself), or -1 if contour is not closed yet.
// HINT: live-wire() adds to the contour "water-tight" paths of neighboring pixels 
int contourInterior()
{
	if (!closedContour || image.isEmpty()) return -1;
	int counter = 0;

	// write your code for computing correct mask (region)
	// which should have 3 for pixels OUTSIDE the contour and 0
	// for pixels INSIDE of the contour
	// HINT: "grow" the "exterior region" from a single exterior point (e.g. a corner of the image). 
	// Make sure that the region does not grow across the contour (points in list "contour")
	// ...
	Queue<Point> active;
	Point q(1,1);
	active.enqueue(q);
	region.reset(0);
	for (unsigned i=1; i<=contour.getLength(); i++)
		region[contour.retrieve(i)]=2;
	while (!active.isEmpty())
	{
		Point p = active.dequeue();
		region[p]=3;  
		counter++;
		for (int i=0; i<4; i++)
		{   
			Point j = p + shift[i]; 
			if (image.pointIn(j) && region[j]!=2 && region[j]!=3)
			{ 
				active.enqueue(j); 
				region[j]=3;  
			}                 
		}  
		if (view && counter%60==0) {draw(); Pause(20);}
	}
	for (unsigned i=1; i<=contour.getLength(); i++)
		region[contour.retrieve(i)]=0;

	return region.getWidth() * region.getHeight() - counter;
}
コード例 #2
0
// GUI calls this function when button "Clear" is pressed, or when new image is loaded
// THIS FUNCTION IS FULLY IMPLEMENTED, YOU DO NOT NEED TO CHANGE THE CODE IN IT
void reset_segm()
{
	cout << "resetting 'contour' and 'region'" << endl;

	// removing all region markings
	region.reset(image.getWidth(),image.getHeight(),0);

	// remove all points from the "contour"
	while (!contour.isEmpty()) contour.popBack();
	closedContour=false;

	// resetting 2D tables "dist" and "toParent" (erazing paths) 
	dist.reset(image.getWidth(),image.getHeight(),INFTY);
	toParent.reset(image.getWidth(),image.getHeight(),NONE);

	// recomputing "penalties" from an estimate of image contrast at each pixel p=(x,y)
	if (image.isEmpty()) {penalty.resize(0,0); return;} 
	Table2D<double> contrast = grad2(image);  //(implicit conversion of RGB "image" to "Table2D<double>") 
	// NOTE: function grad2() (see Math2D.h) computes (at each pixel) expression  Ix*Ix+Iy*Iy  where Ix and Iy
	// are "horizontal" and "vertical" derivatives of image intensity I(x,y) - the average of RGB values.
	// This expression describes the "rate of change" of intensity, or local image "contrast". 
	penalty = convert(contrast,&fn); // "&fn" - address of function "fn" (defined at the top of this file) 
	// "convert" (see Math2D.h) sets penalty at each pixel according to formula "penalty[x][y] = fn (contrast[x][y])" 
}