public static PackedImage threshold(PackedImage img, int level) {
		PackedImage outImg = new PackedImage(img.rows(), img.cols());
		int sum;
		
		for (int r = 0; r < img.rows(); r++) {
		    for (int c = 0; c < img.cols(); c++) {
		    	sum = img.at(r, c, 0) + img.at(r, c, 1) + img.at(r, c, 2);
		    	if (sum/3 < level) { // if average of RGB values is below the level, change corresponding output image pixel to black
		    		outImg.set(r, c, 0);
		    	}
		    	else { // else change to white
		    		outImg.set(r, c, 0, 255);
			    	outImg.set(r, c, 1, 255);
			    	outImg.set(r, c, 2, 255);
		    	}
		    }
		}
		return outImg;
	}