Difference between revisions of "Example RGB to Gray"

From BoofCV
Jump to navigationJump to search
(Created page with "<center> <gallery widths=200px heights=300px> File:Rgb_to_gray_original.jpg | Original Input image File:Rgb_to_gray_unweighted.jpg | Unweighted File:Rgb_to_gray_weighted.jpg |...")
 
m
Line 12: Line 12:


Example Code:
Example Code:
* [https://github.com/lessthanoptimal/BoofCV/blob/v0.24/examples/src/boofcv/examples/imageprocessing/ExampleRgbToGray.java ExampleRgbToGray.java ]
* [https://github.com/lessthanoptimal/BoofCV/blob/v0.37/examples/src/main/java/boofcv/examples/imageprocessing/ExampleRgbToGray.java ExampleRgbToGray.java ]


Concepts:
Concepts:
Line 30: Line 30:
  */
  */
public class ExampleRgbToGray {
public class ExampleRgbToGray {
public static void main(String[] args) {
public static void main( String[] args ) {
// load the image and convert it into a BoofCV data type
// load the image and convert it into a BoofCV data type
BufferedImage buffered = UtilImageIO.loadImage(UtilIO.pathExample("segment/berkeley_man.jpg"));
BufferedImage buffered = UtilImageIO.loadImage(UtilIO.pathExample("segment/berkeley_man.jpg"));
Planar<GrayU8> color = ConvertBufferedImage.convertFrom(buffered,true, ImageType.pl(3,GrayU8.class));
Planar<GrayU8> color = ConvertBufferedImage.convertFrom(buffered, true, ImageType.pl(3, GrayU8.class));


// Declare storage space for converted gray scale images
// Declare storage space for converted gray scale images
GrayU8 weighted = new GrayU8(color.width,color.height);
GrayU8 weighted = new GrayU8(color.width, color.height);
GrayU8 unweighted = new GrayU8(color.width,color.height);
GrayU8 unweighted = new GrayU8(color.width, color.height);


// Now run a benchmark to demonstrate the speed differences between the two approaches.  Both are very fast...
// Now run a benchmark to demonstrate the speed differences between the two approaches.  Both are very fast...
Line 45: Line 45:
startTime = System.nanoTime();
startTime = System.nanoTime();
for (int i = 0; i < N; i++) {
for (int i = 0; i < N; i++) {
ColorRgb.rgbToGray_Weighted(color,weighted); // weigh the bands based on how human vision sees each color
ColorRgb.rgbToGray_Weighted(color, weighted); // weigh the bands based on how human vision sees each color
}
}
double weightedFPS = N/((System.nanoTime()-startTime)*1e-9);
double weightedFPS = N/((System.nanoTime() - startTime)*1e-9);


startTime = System.nanoTime();
startTime = System.nanoTime();
for (int i = 0; i < N; i++) {
for (int i = 0; i < N; i++) {
ConvertImage.average(color,unweighted); // this equally averages all the bands together
ConvertImage.average(color, unweighted); // this equally averages all the bands together
}
}
double unweightedFPS = N/((System.nanoTime()-startTime)*1e-9);
double unweightedFPS = N/((System.nanoTime() - startTime)*1e-9);


System.out.println("FPS  averaged over "+N+" images");
System.out.println("FPS  averaged over " + N + " images");
System.out.println("      (higher is better)");
System.out.println("      (higher is better)");
System.out.println();
System.out.println();
System.out.printf("  weighted    %8.2f\n",weightedFPS);
System.out.printf("  weighted    %8.2f\n", weightedFPS);
System.out.printf("  unweighted  %8.2f\n",unweightedFPS);
System.out.printf("  unweighted  %8.2f\n", unweightedFPS);
System.out.println();
System.out.println();
System.out.printf("Unweighted is %6.1f times faster.\n",(unweightedFPS/weightedFPS));
System.out.printf("Unweighted is %6.1f times faster.\n", (unweightedFPS/weightedFPS));
System.out.println();
System.out.println();
System.out.println("WARNING:  This is a poorly implemented microbenchmark " +
System.out.println("WARNING:  This is a poorly implemented microbenchmark " +
"and results might not be accurate or consistent.");
"and results might not be accurate or consistent.");


// Display the results
// Display the results
ListDisplayPanel gui = new ListDisplayPanel();
ListDisplayPanel gui = new ListDisplayPanel();
gui.addImage(weighted,"Weighted");
gui.addImage(weighted, "Weighted");
gui.addImage(unweighted,"Unweighted");
gui.addImage(unweighted, "Unweighted");
gui.addImage(buffered,"RGB");
gui.addImage(buffered, "RGB");


ShowImages.showWindow(gui,"RGB to Gray", true);
ShowImages.showWindow(gui, "RGB to Gray", true);
}
}
}
}
</syntaxhighlight>
</syntaxhighlight>

Revision as of 19:29, 21 December 2020

There are different methods for converting color images into gray scale images. For RGB images. the standard method in BoofCV is just to compute an average across red, green, and blue bands. Human vision doesn't work that way and it's very common to compute a weighted average, gray = 0.299*r + 0.587*g + 0.114*b. The example below demonstrates how to perform both methods. The unweighted technique is about 4x faster, but normally this step isn't the bottle neck in your vision system.

Example Code:

Concepts:

  • Color Conversion
  • RGB

Example Code

/**
 * Example which demonstrates two different ways to convert RGB images to gray scale images.  The two methods
 * are weighted and unweighted.  The weighted method mimics how human vision works, while the unweighted is
 * much faster.  Typically the unweighted method appears more washed out than the weighted method, but still works
 * very well when passed to other computer vision algorithms.
 *
 * @author Peter Abeles
 */
public class ExampleRgbToGray {
	public static void main( String[] args ) {
		// load the image and convert it into a BoofCV data type
		BufferedImage buffered = UtilImageIO.loadImage(UtilIO.pathExample("segment/berkeley_man.jpg"));
		Planar<GrayU8> color = ConvertBufferedImage.convertFrom(buffered, true, ImageType.pl(3, GrayU8.class));

		// Declare storage space for converted gray scale images
		GrayU8 weighted = new GrayU8(color.width, color.height);
		GrayU8 unweighted = new GrayU8(color.width, color.height);

		// Now run a benchmark to demonstrate the speed differences between the two approaches.  Both are very fast...
		System.out.println("Running benchmark.  Should take a few seconds on a modern computer.\n");
		long startTime;
		int N = 2000;
		startTime = System.nanoTime();
		for (int i = 0; i < N; i++) {
			ColorRgb.rgbToGray_Weighted(color, weighted); // weigh the bands based on how human vision sees each color
		}
		double weightedFPS = N/((System.nanoTime() - startTime)*1e-9);

		startTime = System.nanoTime();
		for (int i = 0; i < N; i++) {
			ConvertImage.average(color, unweighted); // this equally averages all the bands together
		}
		double unweightedFPS = N/((System.nanoTime() - startTime)*1e-9);

		System.out.println("FPS  averaged over " + N + " images");
		System.out.println("      (higher is better)");
		System.out.println();
		System.out.printf("  weighted    %8.2f\n", weightedFPS);
		System.out.printf("  unweighted  %8.2f\n", unweightedFPS);
		System.out.println();
		System.out.printf("Unweighted is %6.1f times faster.\n", (unweightedFPS/weightedFPS));
		System.out.println();
		System.out.println("WARNING:  This is a poorly implemented microbenchmark " +
				"and results might not be accurate or consistent.");

		// Display the results
		ListDisplayPanel gui = new ListDisplayPanel();
		gui.addImage(weighted, "Weighted");
		gui.addImage(unweighted, "Unweighted");
		gui.addImage(buffered, "RGB");

		ShowImages.showWindow(gui, "RGB to Gray", true);
	}
}