Difference between revisions of "Example Image Enhancement"

From BoofCV
Jump to navigationJump to search
(Updated for v0.16)
m
 
(8 intermediate revisions by the same user not shown)
Line 10: Line 10:


Example Code:
Example Code:
* [https://github.com/lessthanoptimal/BoofCV/blob/v0.16/examples/src/boofcv/examples/enhance/ExampleImageEnhancement.java ExampleImageEnhancement.java]
* [https://github.com/lessthanoptimal/BoofCV/blob/v0.40/examples/src/boofcv/examples/enhance/ExampleImageEnhancement.java ExampleImageEnhancement.java]


Concepts:
Concepts:
* Visibility
* Visibility


Relevant Applets:
Relevant Examples:
* [[Applet Image Enhance| Image Enhancement]]
* [[Example_Image_Blur| Image Blur]]


= Example Code =
= Example Code =
Line 22: Line 22:
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
/**
/**
  * Demonstration of various ways an image can be "enhanced". Image enhancement typically refers to making it easier
  * Demonstration of various ways an image can be "enhanced". Image enhancement typically refers to making it easier
  * for people to view the image and pick out its details.
  * for people to view the image and pick out its details.
  *
  *
Line 28: Line 28:
  */
  */
public class ExampleImageEnhancement {
public class ExampleImageEnhancement {
static String imagePath = "enhance/dark.jpg";
// static String imagePath = "enhance/dull.jpg";
static ListDisplayPanel mainPanel = new ListDisplayPanel();


/**
/**
Line 34: Line 39:
*/
*/
public static void histogram() {
public static void histogram() {
// BufferedImage buffered = UtilImageIO.loadImage("../data/applet/enhance/dull.jpg");
BufferedImage buffered = UtilImageIO.loadImageNotNull(UtilIO.pathExample(imagePath));
BufferedImage buffered = UtilImageIO.loadImage("../data/applet/enhance/dark.jpg");
GrayU8 gray = ConvertBufferedImage.convertFrom(buffered, (GrayU8)null);
ImageUInt8 gray = ConvertBufferedImage.convertFrom(buffered,(ImageUInt8)null);
GrayU8 adjusted = gray.createSameShape();
ImageUInt8 adjusted = new ImageUInt8(gray.width, gray.height);


int histogram[] = new int[256];
int[] histogram = new int[256];
int transform[] = new int[256];
int[] transform = new int[256];


ListDisplayPanel panel = new ListDisplayPanel();
ListDisplayPanel panel = new ListDisplayPanel();


ImageStatistics.histogram(gray,histogram);
ImageStatistics.histogram(gray, 0, histogram);
EnhanceImageOps.equalize(histogram, transform);
EnhanceImageOps.equalize(histogram, transform);
EnhanceImageOps.applyTransform(gray, transform, adjusted);
EnhanceImageOps.applyTransform(gray, transform, adjusted);
panel.addImage(ConvertBufferedImage.convertTo(adjusted,null),"Global");
panel.addImage(ConvertBufferedImage.convertTo(adjusted, null), "Global");
 
EnhanceImageOps.equalizeLocal(gray, 50, adjusted, 256, null);
panel.addImage(ConvertBufferedImage.convertTo(adjusted, null), "Local");
 
panel.addImage(ConvertBufferedImage.convertTo(gray, null), "Original");
 
panel.setPreferredSize(new Dimension(gray.width, gray.height));
mainPanel.addItem(panel, "Histogram");
}
 
/**
* Same as above but on a color image.
*/
public static void histogramColor() {
BufferedImage buffered = UtilImageIO.loadImageNotNull(UtilIO.pathExample(imagePath));
Planar<GrayU8> color = ConvertBufferedImage.convertFrom(buffered, true, ImageType.PL_U8);
Planar<GrayU8> adjusted = color.createSameShape();
 
int[] histogram = new int[256];
int[] transform = new int[256];


EnhanceImageOps.equalizeLocal(gray, 50, adjusted, histogram, transform);
ListDisplayPanel panel = new ListDisplayPanel();
panel.addImage(ConvertBufferedImage.convertTo(adjusted,null),"Local");


panel.addImage(ConvertBufferedImage.convertTo(gray,null),"Original");
// Apply the correction to each color band independently. Alternatively, you could compute the adjustment
// on a gray scale image then apply the same transform to each band
for (int bandIdx = 0; bandIdx < color.getNumBands(); bandIdx++) {
ImageStatistics.histogram(color.getBand(bandIdx), 0, histogram);
EnhanceImageOps.equalize(histogram, transform);
EnhanceImageOps.applyTransform(color.getBand(bandIdx), transform, adjusted.getBand(bandIdx));
}
panel.addImage(ConvertBufferedImage.convertTo(adjusted, null, true), "Global");


panel.setPreferredSize(new Dimension(gray.width,gray.height));
GEnhanceImageOps.equalizeLocal(color, 50, adjusted, 256, null);
ShowImages.showWindow(panel,"Histogram");
panel.addImage(ConvertBufferedImage.convertTo(adjusted, null, true), "Local");
panel.addImage(buffered, "Original");
 
panel.setPreferredSize(new Dimension(color.width, color.height));
mainPanel.addItem(panel, "Histogram Color");
}
}


Line 62: Line 96:
*/
*/
public static void sharpen() {
public static void sharpen() {
// BufferedImage buffered = UtilImageIO.loadImage("../data/applet/enhance/dull.jpg");
BufferedImage buffered = UtilImageIO.loadImageNotNull(UtilIO.pathExample(imagePath));
BufferedImage buffered = UtilImageIO.loadImage("../data/applet/enhance/dark.jpg");
GrayU8 gray = ConvertBufferedImage.convertFrom(buffered, (GrayU8)null);
ImageUInt8 gray = ConvertBufferedImage.convertFrom(buffered,(ImageUInt8)null);
GrayU8 adjusted = gray.createSameShape();
ImageUInt8 adjusted = new ImageUInt8(gray.width, gray.height);




Line 71: Line 104:


EnhanceImageOps.sharpen4(gray, adjusted);
EnhanceImageOps.sharpen4(gray, adjusted);
panel.addImage(ConvertBufferedImage.convertTo(adjusted,null),"Sharpen-4");
panel.addImage(ConvertBufferedImage.convertTo(adjusted, null), "Sharpen-4");


EnhanceImageOps.sharpen8(gray, adjusted);
EnhanceImageOps.sharpen8(gray, adjusted);
panel.addImage(ConvertBufferedImage.convertTo(adjusted,null),"Sharpen-8");
panel.addImage(ConvertBufferedImage.convertTo(adjusted, null), "Sharpen-8");


panel.addImage(ConvertBufferedImage.convertTo(gray,null),"Original");
panel.addImage(ConvertBufferedImage.convertTo(gray, null), "Original");


panel.setPreferredSize(new Dimension(gray.width,gray.height));
panel.setPreferredSize(new Dimension(gray.width, gray.height));
ShowImages.showWindow(panel,"Sharpen");
mainPanel.addItem(panel, "Sharpen");
}
}


public static void main( String args[] )
/**
{
* Same as above but on a color image
*/
public static void sharpenColor() {
BufferedImage buffered = UtilImageIO.loadImageNotNull(UtilIO.pathExample(imagePath));
Planar<GrayU8> color = ConvertBufferedImage.convertFrom(buffered, true, ImageType.PL_U8);
Planar<GrayU8> adjusted = color.createSameShape();
 
ListDisplayPanel panel = new ListDisplayPanel();
 
GEnhanceImageOps.sharpen4(color, adjusted);
panel.addImage(ConvertBufferedImage.convertTo(adjusted, null, true), "Sharpen-4");
 
GEnhanceImageOps.sharpen8(color, adjusted);
panel.addImage(ConvertBufferedImage.convertTo(adjusted, null, true), "Sharpen-8");
 
panel.addImage(buffered, "Original");
 
panel.setPreferredSize(new Dimension(color.width, color.height));
mainPanel.addItem(panel, "Sharpen Color");
}
 
public static void main( String[] args ) {
histogram();
histogram();
histogramColor();
sharpen();
sharpen();
sharpenColor();
ShowImages.showWindow(mainPanel, "Enhancement", true);
}
}
}
}
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 12:28, 17 January 2022

Demonstration of different image enhancement operations. If an image is excessively dark or light the visibility of features can be improved by adjusting its histogram. Another technique makes the edges in an image more pronounced by.

Example Code:

Concepts:

  • Visibility

Relevant Examples:

Example Code

/**
 * Demonstration of various ways an image can be "enhanced". Image enhancement typically refers to making it easier
 * for people to view the image and pick out its details.
 *
 * @author Peter Abeles
 */
public class ExampleImageEnhancement {

	static String imagePath = "enhance/dark.jpg";
//	static String imagePath = "enhance/dull.jpg";

	static ListDisplayPanel mainPanel = new ListDisplayPanel();

	/**
	 * Histogram adjustment algorithms aim to spread out pixel intensity values uniformly across the allowed range.
	 * This if an image is dark, it will have greater contrast and be brighter.
	 */
	public static void histogram() {
		BufferedImage buffered = UtilImageIO.loadImageNotNull(UtilIO.pathExample(imagePath));
		GrayU8 gray = ConvertBufferedImage.convertFrom(buffered, (GrayU8)null);
		GrayU8 adjusted = gray.createSameShape();

		int[] histogram = new int[256];
		int[] transform = new int[256];

		ListDisplayPanel panel = new ListDisplayPanel();

		ImageStatistics.histogram(gray, 0, histogram);
		EnhanceImageOps.equalize(histogram, transform);
		EnhanceImageOps.applyTransform(gray, transform, adjusted);
		panel.addImage(ConvertBufferedImage.convertTo(adjusted, null), "Global");

		EnhanceImageOps.equalizeLocal(gray, 50, adjusted, 256, null);
		panel.addImage(ConvertBufferedImage.convertTo(adjusted, null), "Local");

		panel.addImage(ConvertBufferedImage.convertTo(gray, null), "Original");

		panel.setPreferredSize(new Dimension(gray.width, gray.height));
		mainPanel.addItem(panel, "Histogram");
	}

	/**
	 * Same as above but on a color image.
	 */
	public static void histogramColor() {
		BufferedImage buffered = UtilImageIO.loadImageNotNull(UtilIO.pathExample(imagePath));
		Planar<GrayU8> color = ConvertBufferedImage.convertFrom(buffered, true, ImageType.PL_U8);
		Planar<GrayU8> adjusted = color.createSameShape();

		int[] histogram = new int[256];
		int[] transform = new int[256];

		ListDisplayPanel panel = new ListDisplayPanel();

		// Apply the correction to each color band independently. Alternatively, you could compute the adjustment
		// on a gray scale image then apply the same transform to each band
		for (int bandIdx = 0; bandIdx < color.getNumBands(); bandIdx++) {
			ImageStatistics.histogram(color.getBand(bandIdx), 0, histogram);
			EnhanceImageOps.equalize(histogram, transform);
			EnhanceImageOps.applyTransform(color.getBand(bandIdx), transform, adjusted.getBand(bandIdx));
		}
		panel.addImage(ConvertBufferedImage.convertTo(adjusted, null, true), "Global");

		GEnhanceImageOps.equalizeLocal(color, 50, adjusted, 256, null);
		panel.addImage(ConvertBufferedImage.convertTo(adjusted, null, true), "Local");
		panel.addImage(buffered, "Original");

		panel.setPreferredSize(new Dimension(color.width, color.height));
		mainPanel.addItem(panel, "Histogram Color");
	}

	/**
	 * When an image is sharpened the intensity of edges are made more extreme while flat regions remain unchanged.
	 */
	public static void sharpen() {
		BufferedImage buffered = UtilImageIO.loadImageNotNull(UtilIO.pathExample(imagePath));
		GrayU8 gray = ConvertBufferedImage.convertFrom(buffered, (GrayU8)null);
		GrayU8 adjusted = gray.createSameShape();


		ListDisplayPanel panel = new ListDisplayPanel();

		EnhanceImageOps.sharpen4(gray, adjusted);
		panel.addImage(ConvertBufferedImage.convertTo(adjusted, null), "Sharpen-4");

		EnhanceImageOps.sharpen8(gray, adjusted);
		panel.addImage(ConvertBufferedImage.convertTo(adjusted, null), "Sharpen-8");

		panel.addImage(ConvertBufferedImage.convertTo(gray, null), "Original");

		panel.setPreferredSize(new Dimension(gray.width, gray.height));
		mainPanel.addItem(panel, "Sharpen");
	}

	/**
	 * Same as above but on a color image
	 */
	public static void sharpenColor() {
		BufferedImage buffered = UtilImageIO.loadImageNotNull(UtilIO.pathExample(imagePath));
		Planar<GrayU8> color = ConvertBufferedImage.convertFrom(buffered, true, ImageType.PL_U8);
		Planar<GrayU8> adjusted = color.createSameShape();

		ListDisplayPanel panel = new ListDisplayPanel();

		GEnhanceImageOps.sharpen4(color, adjusted);
		panel.addImage(ConvertBufferedImage.convertTo(adjusted, null, true), "Sharpen-4");

		GEnhanceImageOps.sharpen8(color, adjusted);
		panel.addImage(ConvertBufferedImage.convertTo(adjusted, null, true), "Sharpen-8");

		panel.addImage(buffered, "Original");

		panel.setPreferredSize(new Dimension(color.width, color.height));
		mainPanel.addItem(panel, "Sharpen Color");
	}

	public static void main( String[] args ) {
		histogram();
		histogramColor();
		sharpen();
		sharpenColor();
		ShowImages.showWindow(mainPanel, "Enhancement", true);
	}
}