Add Java and Python code for the following tutorials:
- Changing the contrast and brightness of an image!
- Operations with images
This commit is contained in:
+86
@@ -0,0 +1,86 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
|
||||
class BasicLinearTransforms {
|
||||
private byte saturate(double val) {
|
||||
int iVal = (int) Math.round(val);
|
||||
iVal = iVal > 255 ? 255 : (iVal < 0 ? 0 : iVal);
|
||||
return (byte) iVal;
|
||||
}
|
||||
|
||||
public void run(String[] args) {
|
||||
/// Read image given by user
|
||||
//! [basic-linear-transform-load]
|
||||
String imagePath = args.length > 0 ? args[0] : "../data/lena.jpg";
|
||||
Mat image = Imgcodecs.imread(imagePath);
|
||||
if (image.empty()) {
|
||||
System.out.println("Empty image: " + imagePath);
|
||||
System.exit(0);
|
||||
}
|
||||
//! [basic-linear-transform-load]
|
||||
|
||||
//! [basic-linear-transform-output]
|
||||
Mat newImage = Mat.zeros(image.size(), image.type());
|
||||
//! [basic-linear-transform-output]
|
||||
|
||||
//! [basic-linear-transform-parameters]
|
||||
double alpha = 1.0; /*< Simple contrast control */
|
||||
int beta = 0; /*< Simple brightness control */
|
||||
|
||||
/// Initialize values
|
||||
System.out.println(" Basic Linear Transforms ");
|
||||
System.out.println("-------------------------");
|
||||
try (Scanner scanner = new Scanner(System.in)) {
|
||||
System.out.print("* Enter the alpha value [1.0-3.0]: ");
|
||||
alpha = scanner.nextDouble();
|
||||
System.out.print("* Enter the beta value [0-100]: ");
|
||||
beta = scanner.nextInt();
|
||||
}
|
||||
//! [basic-linear-transform-parameters]
|
||||
|
||||
/// Do the operation newImage(i,j) = alpha*image(i,j) + beta
|
||||
/// Instead of these 'for' loops we could have used simply:
|
||||
/// image.convertTo(newImage, -1, alpha, beta);
|
||||
/// but we wanted to show you how to access the pixels :)
|
||||
//! [basic-linear-transform-operation]
|
||||
byte[] imageData = new byte[(int) (image.total()*image.channels())];
|
||||
image.get(0, 0, imageData);
|
||||
byte[] newImageData = new byte[(int) (newImage.total()*newImage.channels())];
|
||||
for (int y = 0; y < image.rows(); y++) {
|
||||
for (int x = 0; x < image.cols(); x++) {
|
||||
for (int c = 0; c < image.channels(); c++) {
|
||||
double pixelValue = imageData[(y * image.cols() + x) * image.channels() + c];
|
||||
/// Java byte range is [-128, 127]
|
||||
pixelValue = pixelValue < 0 ? pixelValue + 256 : pixelValue;
|
||||
newImageData[(y * image.cols() + x) * image.channels() + c]
|
||||
= saturate(alpha * pixelValue + beta);
|
||||
}
|
||||
}
|
||||
}
|
||||
newImage.put(0, 0, newImageData);
|
||||
//! [basic-linear-transform-operation]
|
||||
|
||||
//! [basic-linear-transform-display]
|
||||
/// Show stuff
|
||||
HighGui.imshow("Original Image", image);
|
||||
HighGui.imshow("New Image", newImage);
|
||||
|
||||
/// Wait until user press some key
|
||||
HighGui.waitKey();
|
||||
//! [basic-linear-transform-display]
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class BasicLinearTransformsDemo {
|
||||
public static void main(String[] args) {
|
||||
// Load the native OpenCV library
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
|
||||
new BasicLinearTransforms().run(args);
|
||||
}
|
||||
}
|
||||
+202
@@ -0,0 +1,202 @@
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Container;
|
||||
import java.awt.Image;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JCheckBox;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JSlider;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.core.CvType;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
|
||||
class ChangingContrastBrightnessImage {
|
||||
private static int MAX_VALUE_ALPHA = 500;
|
||||
private static int MAX_VALUE_BETA_GAMMA = 200;
|
||||
private static final String WINDOW_NAME = "Changing the contrast and brightness of an image demo";
|
||||
private static final String ALPHA_NAME = "Alpha gain (contrast)";
|
||||
private static final String BETA_NAME = "Beta bias (brightness)";
|
||||
private static final String GAMMA_NAME = "Gamma correction";
|
||||
private JFrame frame;
|
||||
private Mat matImgSrc = new Mat();
|
||||
private JLabel imgSrcLabel;
|
||||
private JLabel imgModifLabel;
|
||||
private JPanel controlPanel;
|
||||
private JPanel alphaBetaPanel;
|
||||
private JPanel gammaPanel;
|
||||
private double alphaValue = 1.0;
|
||||
private double betaValue = 0.0;
|
||||
private double gammaValue = 1.0;
|
||||
private JCheckBox methodCheckBox;
|
||||
private JSlider sliderAlpha;
|
||||
private JSlider sliderBeta;
|
||||
private JSlider sliderGamma;
|
||||
|
||||
public ChangingContrastBrightnessImage(String[] args) {
|
||||
String imagePath = args.length > 0 ? args[0] : "../data/lena.jpg";
|
||||
matImgSrc = Imgcodecs.imread(imagePath);
|
||||
if (matImgSrc.empty()) {
|
||||
System.out.println("Empty image: " + imagePath);
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
// Create and set up the window.
|
||||
frame = new JFrame(WINDOW_NAME);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
// Set up the content pane.
|
||||
Image img = HighGui.toBufferedImage(matImgSrc);
|
||||
addComponentsToPane(frame.getContentPane(), img);
|
||||
// Use the content pane's default BorderLayout. No need for
|
||||
// setLayout(new BorderLayout());
|
||||
// Display the window.
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
private void addComponentsToPane(Container pane, Image img) {
|
||||
if (!(pane.getLayout() instanceof BorderLayout)) {
|
||||
pane.add(new JLabel("Container doesn't use BorderLayout!"));
|
||||
return;
|
||||
}
|
||||
|
||||
controlPanel = new JPanel();
|
||||
controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.PAGE_AXIS));
|
||||
|
||||
methodCheckBox = new JCheckBox("Do gamma correction");
|
||||
methodCheckBox.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JCheckBox cb = (JCheckBox) e.getSource();
|
||||
if (cb.isSelected()) {
|
||||
controlPanel.remove(alphaBetaPanel);
|
||||
controlPanel.add(gammaPanel);
|
||||
performGammaCorrection();
|
||||
frame.revalidate();
|
||||
frame.repaint();
|
||||
frame.pack();
|
||||
} else {
|
||||
controlPanel.remove(gammaPanel);
|
||||
controlPanel.add(alphaBetaPanel);
|
||||
performLinearTransformation();
|
||||
frame.revalidate();
|
||||
frame.repaint();
|
||||
frame.pack();
|
||||
}
|
||||
}
|
||||
});
|
||||
controlPanel.add(methodCheckBox);
|
||||
|
||||
alphaBetaPanel = new JPanel();
|
||||
alphaBetaPanel.setLayout(new BoxLayout(alphaBetaPanel, BoxLayout.PAGE_AXIS));
|
||||
alphaBetaPanel.add(new JLabel(ALPHA_NAME));
|
||||
sliderAlpha = new JSlider(0, MAX_VALUE_ALPHA, 100);
|
||||
sliderAlpha.setMajorTickSpacing(50);
|
||||
sliderAlpha.setMinorTickSpacing(10);
|
||||
sliderAlpha.setPaintTicks(true);
|
||||
sliderAlpha.setPaintLabels(true);
|
||||
sliderAlpha.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
alphaValue = sliderAlpha.getValue() / 100.0;
|
||||
performLinearTransformation();
|
||||
}
|
||||
});
|
||||
alphaBetaPanel.add(sliderAlpha);
|
||||
|
||||
alphaBetaPanel.add(new JLabel(BETA_NAME));
|
||||
sliderBeta = new JSlider(0, MAX_VALUE_BETA_GAMMA, 100);
|
||||
sliderBeta.setMajorTickSpacing(20);
|
||||
sliderBeta.setMinorTickSpacing(5);
|
||||
sliderBeta.setPaintTicks(true);
|
||||
sliderBeta.setPaintLabels(true);
|
||||
sliderBeta.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
betaValue = sliderBeta.getValue() - 100;
|
||||
performLinearTransformation();
|
||||
}
|
||||
});
|
||||
alphaBetaPanel.add(sliderBeta);
|
||||
controlPanel.add(alphaBetaPanel);
|
||||
|
||||
gammaPanel = new JPanel();
|
||||
gammaPanel.setLayout(new BoxLayout(gammaPanel, BoxLayout.PAGE_AXIS));
|
||||
gammaPanel.add(new JLabel(GAMMA_NAME));
|
||||
sliderGamma = new JSlider(0, MAX_VALUE_BETA_GAMMA, 100);
|
||||
sliderGamma.setMajorTickSpacing(20);
|
||||
sliderGamma.setMinorTickSpacing(5);
|
||||
sliderGamma.setPaintTicks(true);
|
||||
sliderGamma.setPaintLabels(true);
|
||||
sliderGamma.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
gammaValue = sliderGamma.getValue() / 100.0;
|
||||
performGammaCorrection();
|
||||
}
|
||||
});
|
||||
gammaPanel.add(sliderGamma);
|
||||
|
||||
pane.add(controlPanel, BorderLayout.PAGE_START);
|
||||
JPanel framePanel = new JPanel();
|
||||
imgSrcLabel = new JLabel(new ImageIcon(img));
|
||||
framePanel.add(imgSrcLabel);
|
||||
imgModifLabel = new JLabel(new ImageIcon(img));
|
||||
framePanel.add(imgModifLabel);
|
||||
pane.add(framePanel, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
private void performLinearTransformation() {
|
||||
Mat img = new Mat();
|
||||
matImgSrc.convertTo(img, -1, alphaValue, betaValue);
|
||||
imgModifLabel.setIcon(new ImageIcon(HighGui.toBufferedImage(img)));
|
||||
frame.repaint();
|
||||
}
|
||||
|
||||
private byte saturate(double val) {
|
||||
int iVal = (int) Math.round(val);
|
||||
iVal = iVal > 255 ? 255 : (iVal < 0 ? 0 : iVal);
|
||||
return (byte) iVal;
|
||||
}
|
||||
|
||||
private void performGammaCorrection() {
|
||||
//! [changing-contrast-brightness-gamma-correction]
|
||||
Mat lookUpTable = new Mat(1, 256, CvType.CV_8U);
|
||||
byte[] lookUpTableData = new byte[(int) (lookUpTable.total()*lookUpTable.channels())];
|
||||
for (int i = 0; i < lookUpTable.cols(); i++) {
|
||||
lookUpTableData[i] = saturate(Math.pow(i / 255.0, gammaValue) * 255.0);
|
||||
}
|
||||
lookUpTable.put(0, 0, lookUpTableData);
|
||||
Mat img = new Mat();
|
||||
Core.LUT(matImgSrc, lookUpTable, img);
|
||||
//! [changing-contrast-brightness-gamma-correction]
|
||||
|
||||
imgModifLabel.setIcon(new ImageIcon(HighGui.toBufferedImage(img)));
|
||||
frame.repaint();
|
||||
}
|
||||
}
|
||||
|
||||
public class ChangingContrastBrightnessImageDemo {
|
||||
public static void main(String[] args) {
|
||||
// Load the native OpenCV library
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
|
||||
// Schedule a job for the event dispatch thread:
|
||||
// creating and showing this application's GUI.
|
||||
javax.swing.SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
new ChangingContrastBrightnessImage(args);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.core.Core.MinMaxLocResult;
|
||||
import org.opencv.core.CvType;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Rect;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
public class MatOperations {
|
||||
@SuppressWarnings("unused")
|
||||
public static void main(String[] args) {
|
||||
/* Snippet code for Operations with images tutorial (not intended to be run) */
|
||||
|
||||
// Load the native OpenCV library
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
|
||||
String filename = "";
|
||||
// Input/Output
|
||||
{
|
||||
//! [Load an image from a file]
|
||||
Mat img = Imgcodecs.imread(filename);
|
||||
//! [Load an image from a file]
|
||||
}
|
||||
{
|
||||
//! [Load an image from a file in grayscale]
|
||||
Mat img = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
|
||||
//! [Load an image from a file in grayscale]
|
||||
}
|
||||
{
|
||||
Mat img = new Mat(4, 4, CvType.CV_8U);
|
||||
//! [Save image]
|
||||
Imgcodecs.imwrite(filename, img);
|
||||
//! [Save image]
|
||||
}
|
||||
// Accessing pixel intensity values
|
||||
{
|
||||
Mat img = new Mat(4, 4, CvType.CV_8U);
|
||||
int y = 0, x = 0;
|
||||
{
|
||||
//! [Pixel access 1]
|
||||
byte[] imgData = new byte[(int) (img.total() * img.channels())];
|
||||
img.get(0, 0, imgData);
|
||||
byte intensity = imgData[y * img.cols() + x];
|
||||
//! [Pixel access 1]
|
||||
}
|
||||
{
|
||||
//! [Pixel access 5]
|
||||
byte[] imgData = new byte[(int) (img.total() * img.channels())];
|
||||
imgData[y * img.cols() + x] = (byte) 128;
|
||||
img.put(0, 0, imgData);
|
||||
//! [Pixel access 5]
|
||||
}
|
||||
|
||||
}
|
||||
// Memory management and reference counting
|
||||
{
|
||||
//! [Reference counting 2]
|
||||
Mat img = Imgcodecs.imread("image.jpg");
|
||||
Mat img1 = img.clone();
|
||||
//! [Reference counting 2]
|
||||
}
|
||||
{
|
||||
//! [Reference counting 3]
|
||||
Mat img = Imgcodecs.imread("image.jpg");
|
||||
Mat sobelx = new Mat();
|
||||
Imgproc.Sobel(img, sobelx, CvType.CV_32F, 1, 0);
|
||||
//! [Reference counting 3]
|
||||
}
|
||||
// Primitive operations
|
||||
{
|
||||
Mat img = new Mat(400, 400, CvType.CV_8UC3);
|
||||
{
|
||||
//! [Set image to black]
|
||||
byte[] imgData = new byte[(int) (img.total() * img.channels())];
|
||||
Arrays.fill(imgData, (byte) 0);
|
||||
img.put(0, 0, imgData);
|
||||
//! [Set image to black]
|
||||
}
|
||||
{
|
||||
//! [Select ROI]
|
||||
Rect r = new Rect(10, 10, 100, 100);
|
||||
Mat smallImg = img.submat(r);
|
||||
//! [Select ROI]
|
||||
}
|
||||
}
|
||||
{
|
||||
//! [BGR to Gray]
|
||||
Mat img = Imgcodecs.imread("image.jpg"); // loading a 8UC3 image
|
||||
Mat grey = new Mat();
|
||||
Imgproc.cvtColor(img, grey, Imgproc.COLOR_BGR2GRAY);
|
||||
//! [BGR to Gray]
|
||||
}
|
||||
{
|
||||
Mat dst = new Mat(), src = new Mat();
|
||||
//! [Convert to CV_32F]
|
||||
src.convertTo(dst, CvType.CV_32F);
|
||||
//! [Convert to CV_32F]
|
||||
}
|
||||
// Visualizing images
|
||||
{
|
||||
//! [imshow 1]
|
||||
Mat img = Imgcodecs.imread("image.jpg");
|
||||
HighGui.namedWindow("image", HighGui.WINDOW_AUTOSIZE);
|
||||
HighGui.imshow("image", img);
|
||||
HighGui.waitKey();
|
||||
//! [imshow 1]
|
||||
}
|
||||
{
|
||||
//! [imshow 2]
|
||||
Mat img = Imgcodecs.imread("image.jpg");
|
||||
Mat grey = new Mat();
|
||||
Imgproc.cvtColor(img, grey, Imgproc.COLOR_BGR2GRAY);
|
||||
Mat sobelx = new Mat();
|
||||
Imgproc.Sobel(grey, sobelx, CvType.CV_32F, 1, 0);
|
||||
MinMaxLocResult res = Core.minMaxLoc(sobelx); // find minimum and maximum intensities
|
||||
Mat draw = new Mat();
|
||||
double maxVal = res.maxVal, minVal = res.minVal;
|
||||
sobelx.convertTo(draw, CvType.CV_8U, 255.0 / (maxVal - minVal), -minVal * 255.0 / (maxVal - minVal));
|
||||
HighGui.namedWindow("image", HighGui.WINDOW_AUTOSIZE);
|
||||
HighGui.imshow("image", draw);
|
||||
HighGui.waitKey();
|
||||
//! [imshow 2]
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user