Add Java and Python code for the following imgproc tutorials: Finding contours in your image, Convex Hull, Creating Bounding boxes and circles for contours, Creating Bounding rotated boxes and ellipses for contours, Image Moments, Point Polygon Test.
parent
93f2fd396b
commit
a11ef2650e
25 changed files with 1884 additions and 367 deletions
@ -0,0 +1,179 @@ |
||||
import java.awt.BorderLayout; |
||||
import java.awt.Container; |
||||
import java.awt.Image; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Random; |
||||
|
||||
import javax.swing.BoxLayout; |
||||
import javax.swing.ImageIcon; |
||||
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.core.MatOfPoint; |
||||
import org.opencv.core.MatOfPoint2f; |
||||
import org.opencv.core.Point; |
||||
import org.opencv.core.Rect; |
||||
import org.opencv.core.Scalar; |
||||
import org.opencv.core.Size; |
||||
import org.opencv.highgui.HighGui; |
||||
import org.opencv.imgcodecs.Imgcodecs; |
||||
import org.opencv.imgproc.Imgproc; |
||||
|
||||
class GeneralContours1 { |
||||
private Mat srcGray = new Mat(); |
||||
private JFrame frame; |
||||
private JLabel imgSrcLabel; |
||||
private JLabel imgContoursLabel; |
||||
private static final int MAX_THRESHOLD = 255; |
||||
private int threshold = 100; |
||||
private Random rng = new Random(12345); |
||||
|
||||
public GeneralContours1(String[] args) { |
||||
//! [setup]
|
||||
/// Load source image
|
||||
String filename = args.length > 0 ? args[0] : "../data/stuff.jpg"; |
||||
Mat src = Imgcodecs.imread(filename); |
||||
if (src.empty()) { |
||||
System.err.println("Cannot read image: " + filename); |
||||
System.exit(0); |
||||
} |
||||
|
||||
/// Convert image to gray and blur it
|
||||
Imgproc.cvtColor(src, srcGray, Imgproc.COLOR_BGR2GRAY); |
||||
Imgproc.blur(srcGray, srcGray, new Size(3, 3)); |
||||
//! [setup]
|
||||
|
||||
//! [createWindow]
|
||||
// Create and set up the window.
|
||||
frame = new JFrame("Creating Bounding boxes and circles for contours demo"); |
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
||||
// Set up the content pane.
|
||||
Image img = HighGui.toBufferedImage(src); |
||||
addComponentsToPane(frame.getContentPane(), img); |
||||
//! [createWindow]
|
||||
// Use the content pane's default BorderLayout. No need for
|
||||
// setLayout(new BorderLayout());
|
||||
// Display the window.
|
||||
frame.pack(); |
||||
frame.setVisible(true); |
||||
update(); |
||||
} |
||||
|
||||
private void addComponentsToPane(Container pane, Image img) { |
||||
if (!(pane.getLayout() instanceof BorderLayout)) { |
||||
pane.add(new JLabel("Container doesn't use BorderLayout!")); |
||||
return; |
||||
} |
||||
|
||||
JPanel sliderPanel = new JPanel(); |
||||
sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.PAGE_AXIS)); |
||||
|
||||
//! [trackbar]
|
||||
sliderPanel.add(new JLabel("Canny threshold: ")); |
||||
JSlider slider = new JSlider(0, MAX_THRESHOLD, threshold); |
||||
slider.setMajorTickSpacing(20); |
||||
slider.setMinorTickSpacing(10); |
||||
slider.setPaintTicks(true); |
||||
slider.setPaintLabels(true); |
||||
slider.addChangeListener(new ChangeListener() { |
||||
@Override |
||||
public void stateChanged(ChangeEvent e) { |
||||
JSlider source = (JSlider) e.getSource(); |
||||
threshold = source.getValue(); |
||||
update(); |
||||
} |
||||
}); |
||||
//! [trackbar]
|
||||
sliderPanel.add(slider); |
||||
pane.add(sliderPanel, BorderLayout.PAGE_START); |
||||
|
||||
JPanel imgPanel = new JPanel(); |
||||
imgSrcLabel = new JLabel(new ImageIcon(img)); |
||||
imgPanel.add(imgSrcLabel); |
||||
|
||||
Mat blackImg = Mat.zeros(srcGray.size(), CvType.CV_8U); |
||||
imgContoursLabel = new JLabel(new ImageIcon(HighGui.toBufferedImage(blackImg))); |
||||
imgPanel.add(imgContoursLabel); |
||||
|
||||
pane.add(imgPanel, BorderLayout.CENTER); |
||||
} |
||||
|
||||
private void update() { |
||||
//! [Canny]
|
||||
/// Detect edges using Canny
|
||||
Mat cannyOutput = new Mat(); |
||||
Imgproc.Canny(srcGray, cannyOutput, threshold, threshold * 2); |
||||
//! [Canny]
|
||||
|
||||
//! [findContours]
|
||||
/// Find contours
|
||||
List<MatOfPoint> contours = new ArrayList<>(); |
||||
Mat hierarchy = new Mat(); |
||||
Imgproc.findContours(cannyOutput, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); |
||||
//! [findContours]
|
||||
|
||||
//! [allthework]
|
||||
/// Approximate contours to polygons + get bounding rects and circles
|
||||
MatOfPoint2f[] contoursPoly = new MatOfPoint2f[contours.size()]; |
||||
Rect[] boundRect = new Rect[contours.size()]; |
||||
Point[] centers = new Point[contours.size()]; |
||||
float[][] radius = new float[contours.size()][1]; |
||||
|
||||
for (int i = 0; i < contours.size(); i++) { |
||||
contoursPoly[i] = new MatOfPoint2f(); |
||||
Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(i).toArray()), contoursPoly[i], 3, true); |
||||
boundRect[i] = Imgproc.boundingRect(new MatOfPoint(contoursPoly[i].toArray())); |
||||
centers[i] = new Point(); |
||||
Imgproc.minEnclosingCircle(contoursPoly[i], centers[i], radius[i]); |
||||
} |
||||
//! [allthework]
|
||||
|
||||
//! [zeroMat]
|
||||
Mat drawing = Mat.zeros(cannyOutput.size(), CvType.CV_8UC3); |
||||
//! [zeroMat]
|
||||
//! [forContour]
|
||||
/// Draw polygonal contour + bonding rects + circles
|
||||
List<MatOfPoint> contoursPolyList = new ArrayList<>(contoursPoly.length); |
||||
for (MatOfPoint2f poly : contoursPoly) { |
||||
contoursPolyList.add(new MatOfPoint(poly.toArray())); |
||||
} |
||||
|
||||
for (int i = 0; i < contours.size(); i++) { |
||||
Scalar color = new Scalar(rng.nextInt(256), rng.nextInt(256), rng.nextInt(256)); |
||||
Imgproc.drawContours(drawing, contoursPolyList, i, color); |
||||
Imgproc.rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2); |
||||
Imgproc.circle(drawing, centers[i], (int) radius[i][0], color, 2); |
||||
} |
||||
//! [forContour]
|
||||
|
||||
//! [showDrawings]
|
||||
/// Show in a window
|
||||
imgContoursLabel.setIcon(new ImageIcon(HighGui.toBufferedImage(drawing))); |
||||
frame.repaint(); |
||||
//! [showDrawings]
|
||||
} |
||||
} |
||||
|
||||
public class GeneralContoursDemo1 { |
||||
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 GeneralContours1(args); |
||||
} |
||||
}); |
||||
} |
||||
} |
@ -0,0 +1,176 @@ |
||||
import java.awt.BorderLayout; |
||||
import java.awt.Container; |
||||
import java.awt.Image; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Random; |
||||
|
||||
import javax.swing.BoxLayout; |
||||
import javax.swing.ImageIcon; |
||||
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.core.MatOfPoint; |
||||
import org.opencv.core.MatOfPoint2f; |
||||
import org.opencv.core.Point; |
||||
import org.opencv.core.RotatedRect; |
||||
import org.opencv.core.Scalar; |
||||
import org.opencv.core.Size; |
||||
import org.opencv.highgui.HighGui; |
||||
import org.opencv.imgcodecs.Imgcodecs; |
||||
import org.opencv.imgproc.Imgproc; |
||||
|
||||
class GeneralContours2 { |
||||
private Mat srcGray = new Mat(); |
||||
private JFrame frame; |
||||
private JLabel imgSrcLabel; |
||||
private JLabel imgContoursLabel; |
||||
private static final int MAX_THRESHOLD = 255; |
||||
private int threshold = 100; |
||||
private Random rng = new Random(12345); |
||||
|
||||
public GeneralContours2(String[] args) { |
||||
//! [setup]
|
||||
/// Load source image
|
||||
String filename = args.length > 0 ? args[0] : "../data/stuff.jpg"; |
||||
Mat src = Imgcodecs.imread(filename); |
||||
if (src.empty()) { |
||||
System.err.println("Cannot read image: " + filename); |
||||
System.exit(0); |
||||
} |
||||
|
||||
/// Convert image to gray and blur it
|
||||
Imgproc.cvtColor(src, srcGray, Imgproc.COLOR_BGR2GRAY); |
||||
Imgproc.blur(srcGray, srcGray, new Size(3, 3)); |
||||
//! [setup]
|
||||
|
||||
//! [createWindow]
|
||||
// Create and set up the window.
|
||||
frame = new JFrame("Creating Bounding rotated boxes and ellipses for contours demo"); |
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
||||
// Set up the content pane.
|
||||
Image img = HighGui.toBufferedImage(src); |
||||
addComponentsToPane(frame.getContentPane(), img); |
||||
//! [createWindow]
|
||||
// Use the content pane's default BorderLayout. No need for
|
||||
// setLayout(new BorderLayout());
|
||||
// Display the window.
|
||||
frame.pack(); |
||||
frame.setVisible(true); |
||||
update(); |
||||
} |
||||
|
||||
private void addComponentsToPane(Container pane, Image img) { |
||||
if (!(pane.getLayout() instanceof BorderLayout)) { |
||||
pane.add(new JLabel("Container doesn't use BorderLayout!")); |
||||
return; |
||||
} |
||||
|
||||
JPanel sliderPanel = new JPanel(); |
||||
sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.PAGE_AXIS)); |
||||
|
||||
//! [trackbar]
|
||||
sliderPanel.add(new JLabel("Canny threshold: ")); |
||||
JSlider slider = new JSlider(0, MAX_THRESHOLD, threshold); |
||||
slider.setMajorTickSpacing(20); |
||||
slider.setMinorTickSpacing(10); |
||||
slider.setPaintTicks(true); |
||||
slider.setPaintLabels(true); |
||||
slider.addChangeListener(new ChangeListener() { |
||||
@Override |
||||
public void stateChanged(ChangeEvent e) { |
||||
JSlider source = (JSlider) e.getSource(); |
||||
threshold = source.getValue(); |
||||
update(); |
||||
} |
||||
}); |
||||
//! [trackbar]
|
||||
sliderPanel.add(slider); |
||||
pane.add(sliderPanel, BorderLayout.PAGE_START); |
||||
|
||||
JPanel imgPanel = new JPanel(); |
||||
imgSrcLabel = new JLabel(new ImageIcon(img)); |
||||
imgPanel.add(imgSrcLabel); |
||||
|
||||
Mat blackImg = Mat.zeros(srcGray.size(), CvType.CV_8U); |
||||
imgContoursLabel = new JLabel(new ImageIcon(HighGui.toBufferedImage(blackImg))); |
||||
imgPanel.add(imgContoursLabel); |
||||
|
||||
pane.add(imgPanel, BorderLayout.CENTER); |
||||
} |
||||
|
||||
private void update() { |
||||
//! [Canny]
|
||||
/// Detect edges using Canny
|
||||
Mat cannyOutput = new Mat(); |
||||
Imgproc.Canny(srcGray, cannyOutput, threshold, threshold * 2); |
||||
//! [Canny]
|
||||
|
||||
//! [findContours]
|
||||
/// Find contours
|
||||
List<MatOfPoint> contours = new ArrayList<>(); |
||||
Mat hierarchy = new Mat(); |
||||
Imgproc.findContours(cannyOutput, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); |
||||
//! [findContours]
|
||||
|
||||
/// Find the rotated rectangles and ellipses for each contour
|
||||
RotatedRect[] minRect = new RotatedRect[contours.size()]; |
||||
RotatedRect[] minEllipse = new RotatedRect[contours.size()]; |
||||
for (int i = 0; i < contours.size(); i++) { |
||||
minRect[i] = Imgproc.minAreaRect(new MatOfPoint2f(contours.get(i).toArray())); |
||||
minEllipse[i] = new RotatedRect(); |
||||
if (contours.get(i).rows() > 5) { |
||||
minEllipse[i] = Imgproc.fitEllipse(new MatOfPoint2f(contours.get(i).toArray())); |
||||
} |
||||
} |
||||
|
||||
//! [zeroMat]
|
||||
/// Draw contours + rotated rects + ellipses
|
||||
Mat drawing = Mat.zeros(cannyOutput.size(), CvType.CV_8UC3); |
||||
//! [zeroMat]
|
||||
//! [forContour]
|
||||
for (int i = 0; i < contours.size(); i++) { |
||||
Scalar color = new Scalar(rng.nextInt(256), rng.nextInt(256), rng.nextInt(256)); |
||||
// contour
|
||||
Imgproc.drawContours(drawing, contours, i, color); |
||||
// ellipse
|
||||
Imgproc.ellipse(drawing, minEllipse[i], color, 2); |
||||
// rotated rectangle
|
||||
Point[] rectPoints = new Point[4]; |
||||
minRect[i].points(rectPoints); |
||||
for (int j = 0; j < 4; j++) { |
||||
Imgproc.line(drawing, rectPoints[j], rectPoints[(j+1) % 4], color); |
||||
} |
||||
} |
||||
//! [forContour]
|
||||
|
||||
//! [showDrawings]
|
||||
/// Show in a window
|
||||
imgContoursLabel.setIcon(new ImageIcon(HighGui.toBufferedImage(drawing))); |
||||
frame.repaint(); |
||||
//! [showDrawings]
|
||||
} |
||||
} |
||||
|
||||
public class GeneralContoursDemo2 { |
||||
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 GeneralContours2(args); |
||||
} |
||||
}); |
||||
} |
||||
} |
@ -0,0 +1,137 @@ |
||||
import java.awt.BorderLayout; |
||||
import java.awt.Container; |
||||
import java.awt.Image; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Random; |
||||
|
||||
import javax.swing.BoxLayout; |
||||
import javax.swing.ImageIcon; |
||||
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.core.MatOfPoint; |
||||
import org.opencv.core.Point; |
||||
import org.opencv.core.Scalar; |
||||
import org.opencv.core.Size; |
||||
import org.opencv.highgui.HighGui; |
||||
import org.opencv.imgcodecs.Imgcodecs; |
||||
import org.opencv.imgproc.Imgproc; |
||||
|
||||
class FindContours { |
||||
private Mat srcGray = new Mat(); |
||||
private JFrame frame; |
||||
private JLabel imgSrcLabel; |
||||
private JLabel imgContoursLabel; |
||||
private static final int MAX_THRESHOLD = 255; |
||||
private int threshold = 100; |
||||
private Random rng = new Random(12345); |
||||
|
||||
public FindContours(String[] args) { |
||||
/// Load source image
|
||||
String filename = args.length > 0 ? args[0] : "../data/HappyFish.jpg"; |
||||
Mat src = Imgcodecs.imread(filename); |
||||
if (src.empty()) { |
||||
System.err.println("Cannot read image: " + filename); |
||||
System.exit(0); |
||||
} |
||||
|
||||
/// Convert image to gray and blur it
|
||||
Imgproc.cvtColor(src, srcGray, Imgproc.COLOR_BGR2GRAY); |
||||
Imgproc.blur(srcGray, srcGray, new Size(3, 3)); |
||||
|
||||
// Create and set up the window.
|
||||
frame = new JFrame("Finding contours in your image demo"); |
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
||||
// Set up the content pane.
|
||||
Image img = HighGui.toBufferedImage(src); |
||||
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); |
||||
update(); |
||||
} |
||||
|
||||
private void addComponentsToPane(Container pane, Image img) { |
||||
if (!(pane.getLayout() instanceof BorderLayout)) { |
||||
pane.add(new JLabel("Container doesn't use BorderLayout!")); |
||||
return; |
||||
} |
||||
|
||||
JPanel sliderPanel = new JPanel(); |
||||
sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.PAGE_AXIS)); |
||||
|
||||
sliderPanel.add(new JLabel("Canny threshold: ")); |
||||
JSlider slider = new JSlider(0, MAX_THRESHOLD, threshold); |
||||
slider.setMajorTickSpacing(20); |
||||
slider.setMinorTickSpacing(10); |
||||
slider.setPaintTicks(true); |
||||
slider.setPaintLabels(true); |
||||
slider.addChangeListener(new ChangeListener() { |
||||
@Override |
||||
public void stateChanged(ChangeEvent e) { |
||||
JSlider source = (JSlider) e.getSource(); |
||||
threshold = source.getValue(); |
||||
update(); |
||||
} |
||||
}); |
||||
sliderPanel.add(slider); |
||||
pane.add(sliderPanel, BorderLayout.PAGE_START); |
||||
|
||||
JPanel imgPanel = new JPanel(); |
||||
imgSrcLabel = new JLabel(new ImageIcon(img)); |
||||
imgPanel.add(imgSrcLabel); |
||||
|
||||
Mat blackImg = Mat.zeros(srcGray.size(), CvType.CV_8U); |
||||
imgContoursLabel = new JLabel(new ImageIcon(HighGui.toBufferedImage(blackImg))); |
||||
imgPanel.add(imgContoursLabel); |
||||
|
||||
pane.add(imgPanel, BorderLayout.CENTER); |
||||
} |
||||
|
||||
private void update() { |
||||
/// Detect edges using Canny
|
||||
Mat cannyOutput = new Mat(); |
||||
Imgproc.Canny(srcGray, cannyOutput, threshold, threshold * 2); |
||||
|
||||
/// Find contours
|
||||
List<MatOfPoint> contours = new ArrayList<>(); |
||||
Mat hierarchy = new Mat(); |
||||
Imgproc.findContours(cannyOutput, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); |
||||
|
||||
/// Draw contours
|
||||
Mat drawing = Mat.zeros(cannyOutput.size(), CvType.CV_8UC3); |
||||
for (int i = 0; i < contours.size(); i++) { |
||||
Scalar color = new Scalar(rng.nextInt(256), rng.nextInt(256), rng.nextInt(256)); |
||||
Imgproc.drawContours(drawing, contours, i, color, 2, Core.LINE_8, hierarchy, 0, new Point()); |
||||
} |
||||
|
||||
imgContoursLabel.setIcon(new ImageIcon(HighGui.toBufferedImage(drawing))); |
||||
frame.repaint(); |
||||
} |
||||
} |
||||
|
||||
public class FindContoursDemo { |
||||
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 FindContours(args); |
||||
} |
||||
}); |
||||
} |
||||
} |
@ -0,0 +1,154 @@ |
||||
import java.awt.BorderLayout; |
||||
import java.awt.Container; |
||||
import java.awt.Image; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Random; |
||||
|
||||
import javax.swing.BoxLayout; |
||||
import javax.swing.ImageIcon; |
||||
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.core.MatOfInt; |
||||
import org.opencv.core.MatOfPoint; |
||||
import org.opencv.core.Point; |
||||
import org.opencv.core.Scalar; |
||||
import org.opencv.core.Size; |
||||
import org.opencv.highgui.HighGui; |
||||
import org.opencv.imgcodecs.Imgcodecs; |
||||
import org.opencv.imgproc.Imgproc; |
||||
|
||||
class Hull { |
||||
private Mat srcGray = new Mat(); |
||||
private JFrame frame; |
||||
private JLabel imgSrcLabel; |
||||
private JLabel imgContoursLabel; |
||||
private static final int MAX_THRESHOLD = 255; |
||||
private int threshold = 100; |
||||
private Random rng = new Random(12345); |
||||
|
||||
public Hull(String[] args) { |
||||
/// Load source image
|
||||
String filename = args.length > 0 ? args[0] : "../data/stuff.jpg"; |
||||
Mat src = Imgcodecs.imread(filename); |
||||
if (src.empty()) { |
||||
System.err.println("Cannot read image: " + filename); |
||||
System.exit(0); |
||||
} |
||||
|
||||
/// Convert image to gray and blur it
|
||||
Imgproc.cvtColor(src, srcGray, Imgproc.COLOR_BGR2GRAY); |
||||
Imgproc.blur(srcGray, srcGray, new Size(3, 3)); |
||||
|
||||
// Create and set up the window.
|
||||
frame = new JFrame("Convex Hull demo"); |
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
||||
// Set up the content pane.
|
||||
Image img = HighGui.toBufferedImage(src); |
||||
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); |
||||
update(); |
||||
} |
||||
|
||||
private void addComponentsToPane(Container pane, Image img) { |
||||
if (!(pane.getLayout() instanceof BorderLayout)) { |
||||
pane.add(new JLabel("Container doesn't use BorderLayout!")); |
||||
return; |
||||
} |
||||
|
||||
JPanel sliderPanel = new JPanel(); |
||||
sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.PAGE_AXIS)); |
||||
|
||||
sliderPanel.add(new JLabel("Canny threshold: ")); |
||||
JSlider slider = new JSlider(0, MAX_THRESHOLD, threshold); |
||||
slider.setMajorTickSpacing(20); |
||||
slider.setMinorTickSpacing(10); |
||||
slider.setPaintTicks(true); |
||||
slider.setPaintLabels(true); |
||||
slider.addChangeListener(new ChangeListener() { |
||||
@Override |
||||
public void stateChanged(ChangeEvent e) { |
||||
JSlider source = (JSlider) e.getSource(); |
||||
threshold = source.getValue(); |
||||
update(); |
||||
} |
||||
}); |
||||
sliderPanel.add(slider); |
||||
pane.add(sliderPanel, BorderLayout.PAGE_START); |
||||
|
||||
JPanel imgPanel = new JPanel(); |
||||
imgSrcLabel = new JLabel(new ImageIcon(img)); |
||||
imgPanel.add(imgSrcLabel); |
||||
|
||||
Mat blackImg = Mat.zeros(srcGray.size(), CvType.CV_8U); |
||||
imgContoursLabel = new JLabel(new ImageIcon(HighGui.toBufferedImage(blackImg))); |
||||
imgPanel.add(imgContoursLabel); |
||||
|
||||
pane.add(imgPanel, BorderLayout.CENTER); |
||||
} |
||||
|
||||
private void update() { |
||||
/// Detect edges using Canny
|
||||
Mat cannyOutput = new Mat(); |
||||
Imgproc.Canny(srcGray, cannyOutput, threshold, threshold * 2); |
||||
|
||||
/// Find contours
|
||||
List<MatOfPoint> contours = new ArrayList<>(); |
||||
Mat hierarchy = new Mat(); |
||||
Imgproc.findContours(cannyOutput, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); |
||||
|
||||
/// Find the convex hull object for each contour
|
||||
List<MatOfPoint> hullList = new ArrayList<>(); |
||||
for (MatOfPoint contour : contours) { |
||||
MatOfInt hull = new MatOfInt(); |
||||
Imgproc.convexHull(contour, hull); |
||||
|
||||
Point[] contourArray = contour.toArray(); |
||||
Point[] hullPoints = new Point[hull.rows()]; |
||||
List<Integer> hullContourIdxList = hull.toList(); |
||||
for (int i = 0; i < hullContourIdxList.size(); i++) { |
||||
hullPoints[i] = contourArray[hullContourIdxList.get(i)]; |
||||
} |
||||
hullList.add(new MatOfPoint(hullPoints)); |
||||
} |
||||
|
||||
/// Draw contours + hull results
|
||||
Mat drawing = Mat.zeros(cannyOutput.size(), CvType.CV_8UC3); |
||||
for (int i = 0; i < contours.size(); i++) { |
||||
Scalar color = new Scalar(rng.nextInt(256), rng.nextInt(256), rng.nextInt(256)); |
||||
Imgproc.drawContours(drawing, contours, i, color); |
||||
Imgproc.drawContours(drawing, hullList, i, color ); |
||||
} |
||||
|
||||
imgContoursLabel.setIcon(new ImageIcon(HighGui.toBufferedImage(drawing))); |
||||
frame.repaint(); |
||||
} |
||||
} |
||||
|
||||
public class HullDemo { |
||||
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 Hull(args); |
||||
} |
||||
}); |
||||
} |
||||
} |
@ -0,0 +1,178 @@ |
||||
import java.awt.BorderLayout; |
||||
import java.awt.Container; |
||||
import java.awt.Image; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Random; |
||||
|
||||
import javax.swing.BoxLayout; |
||||
import javax.swing.ImageIcon; |
||||
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.core.MatOfPoint; |
||||
import org.opencv.core.MatOfPoint2f; |
||||
import org.opencv.core.Point; |
||||
import org.opencv.core.Scalar; |
||||
import org.opencv.core.Size; |
||||
import org.opencv.highgui.HighGui; |
||||
import org.opencv.imgcodecs.Imgcodecs; |
||||
import org.opencv.imgproc.Imgproc; |
||||
import org.opencv.imgproc.Moments; |
||||
|
||||
class MomentsClass { |
||||
private Mat srcGray = new Mat(); |
||||
private JFrame frame; |
||||
private JLabel imgSrcLabel; |
||||
private JLabel imgContoursLabel; |
||||
private static final int MAX_THRESHOLD = 255; |
||||
private int threshold = 100; |
||||
private Random rng = new Random(12345); |
||||
|
||||
public MomentsClass(String[] args) { |
||||
//! [setup]
|
||||
/// Load source image
|
||||
String filename = args.length > 0 ? args[0] : "../data/stuff.jpg"; |
||||
Mat src = Imgcodecs.imread(filename); |
||||
if (src.empty()) { |
||||
System.err.println("Cannot read image: " + filename); |
||||
System.exit(0); |
||||
} |
||||
|
||||
/// Convert image to gray and blur it
|
||||
Imgproc.cvtColor(src, srcGray, Imgproc.COLOR_BGR2GRAY); |
||||
Imgproc.blur(srcGray, srcGray, new Size(3, 3)); |
||||
//! [setup]
|
||||
|
||||
//! [createWindow]
|
||||
// Create and set up the window.
|
||||
frame = new JFrame("Image Moments demo"); |
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
||||
// Set up the content pane.
|
||||
Image img = HighGui.toBufferedImage(src); |
||||
addComponentsToPane(frame.getContentPane(), img); |
||||
//! [createWindow]
|
||||
// Use the content pane's default BorderLayout. No need for
|
||||
// setLayout(new BorderLayout());
|
||||
// Display the window.
|
||||
frame.pack(); |
||||
frame.setVisible(true); |
||||
update(); |
||||
} |
||||
|
||||
private void addComponentsToPane(Container pane, Image img) { |
||||
if (!(pane.getLayout() instanceof BorderLayout)) { |
||||
pane.add(new JLabel("Container doesn't use BorderLayout!")); |
||||
return; |
||||
} |
||||
|
||||
JPanel sliderPanel = new JPanel(); |
||||
sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.PAGE_AXIS)); |
||||
|
||||
//! [trackbar]
|
||||
sliderPanel.add(new JLabel("Canny threshold: ")); |
||||
JSlider slider = new JSlider(0, MAX_THRESHOLD, threshold); |
||||
slider.setMajorTickSpacing(20); |
||||
slider.setMinorTickSpacing(10); |
||||
slider.setPaintTicks(true); |
||||
slider.setPaintLabels(true); |
||||
slider.addChangeListener(new ChangeListener() { |
||||
@Override |
||||
public void stateChanged(ChangeEvent e) { |
||||
JSlider source = (JSlider) e.getSource(); |
||||
threshold = source.getValue(); |
||||
update(); |
||||
} |
||||
}); |
||||
//! [trackbar]
|
||||
sliderPanel.add(slider); |
||||
pane.add(sliderPanel, BorderLayout.PAGE_START); |
||||
|
||||
JPanel imgPanel = new JPanel(); |
||||
imgSrcLabel = new JLabel(new ImageIcon(img)); |
||||
imgPanel.add(imgSrcLabel); |
||||
|
||||
Mat blackImg = Mat.zeros(srcGray.size(), CvType.CV_8U); |
||||
imgContoursLabel = new JLabel(new ImageIcon(HighGui.toBufferedImage(blackImg))); |
||||
imgPanel.add(imgContoursLabel); |
||||
|
||||
pane.add(imgPanel, BorderLayout.CENTER); |
||||
} |
||||
|
||||
private void update() { |
||||
//! [Canny]
|
||||
/// Detect edges using Canny
|
||||
Mat cannyOutput = new Mat(); |
||||
Imgproc.Canny(srcGray, cannyOutput, threshold, threshold * 2); |
||||
//! [Canny]
|
||||
|
||||
//! [findContours]
|
||||
/// Find contours
|
||||
List<MatOfPoint> contours = new ArrayList<>(); |
||||
Mat hierarchy = new Mat(); |
||||
Imgproc.findContours(cannyOutput, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); |
||||
//! [findContours]
|
||||
|
||||
/// Get the moments
|
||||
List<Moments> mu = new ArrayList<>(contours.size()); |
||||
for (int i = 0; i < contours.size(); i++) { |
||||
mu.add(Imgproc.moments(contours.get(i))); |
||||
} |
||||
|
||||
/// Get the mass centers
|
||||
List<Point> mc = new ArrayList<>(contours.size()); |
||||
for (int i = 0; i < contours.size(); i++) { |
||||
//add 1e-5 to avoid division by zero
|
||||
mc.add(new Point(mu.get(i).m10 / (mu.get(i).m00 + 1e-5), mu.get(i).m01 / (mu.get(i).m00 + 1e-5))); |
||||
} |
||||
|
||||
//! [zeroMat]
|
||||
/// Draw contours
|
||||
Mat drawing = Mat.zeros(cannyOutput.size(), CvType.CV_8UC3); |
||||
//! [zeroMat]
|
||||
//! [forContour]
|
||||
for (int i = 0; i < contours.size(); i++) { |
||||
Scalar color = new Scalar(rng.nextInt(256), rng.nextInt(256), rng.nextInt(256)); |
||||
Imgproc.drawContours(drawing, contours, i, color, 2); |
||||
Imgproc.circle(drawing, mc.get(i), 4, color, -1); |
||||
} |
||||
//! [forContour]
|
||||
|
||||
//! [showDrawings]
|
||||
/// Show in a window
|
||||
imgContoursLabel.setIcon(new ImageIcon(HighGui.toBufferedImage(drawing))); |
||||
frame.repaint(); |
||||
//! [showDrawings]
|
||||
|
||||
/// Calculate the area with the moments 00 and compare with the result of the OpenCV function
|
||||
System.out.println("\t Info: Area and Contour Length \n"); |
||||
for (int i = 0; i < contours.size(); i++) { |
||||
System.out.format(" * Contour[%d] - Area (M_00) = %.2f - Area OpenCV: %.2f - Length: %.2f\n", i, |
||||
mu.get(i).m00, Imgproc.contourArea(contours.get(i)), |
||||
Imgproc.arcLength(new MatOfPoint2f(contours.get(i).toArray()), true)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
public class MomentsDemo { |
||||
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 MomentsClass(args); |
||||
} |
||||
}); |
||||
} |
||||
} |
@ -0,0 +1,93 @@ |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
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.MatOfPoint; |
||||
import org.opencv.core.MatOfPoint2f; |
||||
import org.opencv.core.Point; |
||||
import org.opencv.core.Scalar; |
||||
import org.opencv.core.Size; |
||||
import org.opencv.highgui.HighGui; |
||||
import org.opencv.imgproc.Imgproc; |
||||
|
||||
class PointPolygonTest { |
||||
public void run() { |
||||
/// Create an image
|
||||
int r = 100; |
||||
Mat src = Mat.zeros(new Size(4 * r, 4 * r), CvType.CV_8U); |
||||
|
||||
/// Create a sequence of points to make a contour
|
||||
List<Point> vert = new ArrayList<>(6); |
||||
vert.add(new Point(3 * r / 2, 1.34 * r)); |
||||
vert.add(new Point(1 * r, 2 * r)); |
||||
vert.add(new Point(3 * r / 2, 2.866 * r)); |
||||
vert.add(new Point(5 * r / 2, 2.866 * r)); |
||||
vert.add(new Point(3 * r, 2 * r)); |
||||
vert.add(new Point(5 * r / 2, 1.34 * r)); |
||||
|
||||
/// Draw it in src
|
||||
for (int i = 0; i < 6; i++) { |
||||
Imgproc.line(src, vert.get(i), vert.get((i + 1) % 6), new Scalar(255), 3); |
||||
} |
||||
|
||||
/// Get the contours
|
||||
List<MatOfPoint> contours = new ArrayList<>(); |
||||
Mat hierarchy = new Mat(); |
||||
Imgproc.findContours(src, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); |
||||
|
||||
/// Calculate the distances to the contour
|
||||
Mat rawDist = new Mat(src.size(), CvType.CV_32F); |
||||
float[] rawDistData = new float[(int) (rawDist.total() * rawDist.channels())]; |
||||
for (int i = 0; i < src.rows(); i++) { |
||||
for (int j = 0; j < src.cols(); j++) { |
||||
rawDistData[i * src.cols() + j] = (float) Imgproc |
||||
.pointPolygonTest(new MatOfPoint2f(contours.get(0).toArray()), new Point(j, i), true); |
||||
} |
||||
} |
||||
rawDist.put(0, 0, rawDistData); |
||||
|
||||
MinMaxLocResult res = Core.minMaxLoc(rawDist); |
||||
double minVal = Math.abs(res.minVal); |
||||
double maxVal = Math.abs(res.maxVal); |
||||
|
||||
/// Depicting the distances graphically
|
||||
Mat drawing = Mat.zeros(src.size(), CvType.CV_8UC3); |
||||
byte[] drawingData = new byte[(int) (drawing.total() * drawing.channels())]; |
||||
for (int i = 0; i < src.rows(); i++) { |
||||
for (int j = 0; j < src.cols(); j++) { |
||||
if (rawDistData[i * src.cols() + j] < 0) { |
||||
drawingData[(i * src.cols() + j) * 3] = |
||||
(byte) (255 - Math.abs(rawDistData[i * src.cols() + j]) * 255 / minVal); |
||||
} else if (rawDistData[i * src.cols() + j] > 0) { |
||||
drawingData[(i * src.cols() + j) * 3 + 2] = |
||||
(byte) (255 - rawDistData[i * src.cols() + j] * 255 / maxVal); |
||||
} else { |
||||
drawingData[(i * src.cols() + j) * 3] = (byte) 255; |
||||
drawingData[(i * src.cols() + j) * 3 + 1] = (byte) 255; |
||||
drawingData[(i * src.cols() + j) * 3 + 2] = (byte) 255; |
||||
} |
||||
} |
||||
} |
||||
drawing.put(0, 0, drawingData); |
||||
|
||||
/// Show your results
|
||||
HighGui.imshow("Source", src); |
||||
HighGui.imshow("Distance", drawing); |
||||
|
||||
HighGui.waitKey(); |
||||
System.exit(0); |
||||
} |
||||
} |
||||
|
||||
public class PointPolygonTestDemo { |
||||
public static void main(String[] args) { |
||||
// Load the native OpenCV library
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); |
||||
|
||||
new PointPolygonTest().run(); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,82 @@ |
||||
from __future__ import print_function |
||||
import cv2 as cv |
||||
import numpy as np |
||||
import argparse |
||||
import random as rng |
||||
|
||||
rng.seed(12345) |
||||
|
||||
def thresh_callback(val): |
||||
threshold = val |
||||
|
||||
## [Canny] |
||||
# Detect edges using Canny |
||||
canny_output = cv.Canny(src_gray, threshold, threshold * 2) |
||||
## [Canny] |
||||
|
||||
## [findContours] |
||||
# Find contours |
||||
_, contours, _ = cv.findContours(canny_output, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) |
||||
## [findContours] |
||||
|
||||
## [allthework] |
||||
# Approximate contours to polygons + get bounding rects and circles |
||||
contours_poly = [None]*len(contours) |
||||
boundRect = [None]*len(contours) |
||||
centers = [None]*len(contours) |
||||
radius = [None]*len(contours) |
||||
for i in range(len(contours)): |
||||
contours_poly[i] = cv.approxPolyDP(contours[i], 3, True) |
||||
boundRect[i] = cv.boundingRect(contours_poly[i]) |
||||
centers[i], radius[i] = cv.minEnclosingCircle(contours_poly[i]) |
||||
## [allthework] |
||||
|
||||
## [zeroMat] |
||||
drawing = np.zeros((canny_output.shape[0], canny_output.shape[1], 3), dtype=np.uint8) |
||||
## [zeroMat] |
||||
|
||||
## [forContour] |
||||
# Draw polygonal contour + bonding rects + circles |
||||
for i in range(len(contours)): |
||||
color = (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256)) |
||||
cv.drawContours(drawing, contours_poly, i, color) |
||||
cv.rectangle(drawing, (int(boundRect[i][0]), int(boundRect[i][1])), \ |
||||
(int(boundRect[i][0]+boundRect[i][2]), int(boundRect[i][1]+boundRect[i][3])), color, 2) |
||||
cv.circle(drawing, (int(centers[i][0]), int(centers[i][1])), int(radius[i]), color, 2) |
||||
## [forContour] |
||||
|
||||
## [showDrawings] |
||||
# Show in a window |
||||
cv.imshow('Contours', drawing) |
||||
## [showDrawings] |
||||
|
||||
## [setup] |
||||
# Load source image |
||||
parser = argparse.ArgumentParser(description='Code for Creating Bounding boxes and circles for contours tutorial.') |
||||
parser.add_argument('--input', help='Path to input image.', default='../data/stuff.jpg') |
||||
args = parser.parse_args() |
||||
|
||||
src = cv.imread(args.input) |
||||
if src is None: |
||||
print('Could not open or find the image:', args.input) |
||||
exit(0) |
||||
|
||||
# Convert image to gray and blur it |
||||
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) |
||||
src_gray = cv.blur(src_gray, (3,3)) |
||||
## [setup] |
||||
|
||||
## [createWindow] |
||||
# Create Window |
||||
source_window = 'Source' |
||||
cv.namedWindow(source_window) |
||||
cv.imshow(source_window, src) |
||||
## [createWindow] |
||||
## [trackbar] |
||||
max_thresh = 255 |
||||
thresh = 100 # initial threshold |
||||
cv.createTrackbar('Canny thresh:', source_window, thresh, max_thresh, thresh_callback) |
||||
thresh_callback(thresh) |
||||
## [trackbar] |
||||
|
||||
cv.waitKey() |
@ -0,0 +1,82 @@ |
||||
from __future__ import print_function |
||||
import cv2 as cv |
||||
import numpy as np |
||||
import argparse |
||||
import random as rng |
||||
|
||||
rng.seed(12345) |
||||
|
||||
def thresh_callback(val): |
||||
threshold = val |
||||
|
||||
## [Canny] |
||||
# Detect edges using Canny |
||||
canny_output = cv.Canny(src_gray, threshold, threshold * 2) |
||||
## [Canny] |
||||
|
||||
## [findContours] |
||||
# Find contours |
||||
_, contours, _ = cv.findContours(canny_output, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) |
||||
## [findContours] |
||||
|
||||
# Find the rotated rectangles and ellipses for each contour |
||||
minRect = [None]*len(contours) |
||||
minEllipse = [None]*len(contours) |
||||
for i in range(len(contours)): |
||||
minRect[i] = cv.minAreaRect(contours[i]) |
||||
if contours[i].shape[0] > 5: |
||||
minEllipse[i] = cv.fitEllipse(contours[i]) |
||||
|
||||
# Draw contours + rotated rects + ellipses |
||||
## [zeroMat] |
||||
drawing = np.zeros((canny_output.shape[0], canny_output.shape[1], 3), dtype=np.uint8) |
||||
## [zeroMat] |
||||
## [forContour] |
||||
for i in range(len(contours)): |
||||
color = (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256)) |
||||
# contour |
||||
cv.drawContours(drawing, contours, i, color) |
||||
# ellipse |
||||
if contours[i].shape[0] > 5: |
||||
cv.ellipse(drawing, minEllipse[i], color, 2) |
||||
# rotated rectangle |
||||
box = cv.boxPoints(minRect[i]) |
||||
box = np.intp(box) #np.intp: Integer used for indexing (same as C ssize_t; normally either int32 or int64) |
||||
cv.drawContours(drawing, [box], 0, color) |
||||
## [forContour] |
||||
|
||||
## [showDrawings] |
||||
# Show in a window |
||||
cv.imshow('Contours', drawing) |
||||
## [showDrawings] |
||||
|
||||
## [setup] |
||||
# Load source image |
||||
parser = argparse.ArgumentParser(description='Code for Creating Bounding rotated boxes and ellipses for contours tutorial.') |
||||
parser.add_argument('--input', help='Path to input image.', default='../data/stuff.jpg') |
||||
args = parser.parse_args() |
||||
|
||||
src = cv.imread(args.input) |
||||
if src is None: |
||||
print('Could not open or find the image:', args.input) |
||||
exit(0) |
||||
|
||||
# Convert image to gray and blur it |
||||
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) |
||||
src_gray = cv.blur(src_gray, (3,3)) |
||||
## [setup] |
||||
|
||||
## [createWindow] |
||||
# Create Window |
||||
source_window = 'Source' |
||||
cv.namedWindow(source_window) |
||||
cv.imshow(source_window, src) |
||||
## [createWindow] |
||||
## [trackbar] |
||||
max_thresh = 255 |
||||
thresh = 100 # initial threshold |
||||
cv.createTrackbar('Canny Thresh:', source_window, thresh, max_thresh, thresh_callback) |
||||
thresh_callback(thresh) |
||||
## [trackbar] |
||||
|
||||
cv.waitKey() |
@ -0,0 +1,50 @@ |
||||
from __future__ import print_function |
||||
import cv2 as cv |
||||
import numpy as np |
||||
import argparse |
||||
import random as rng |
||||
|
||||
rng.seed(12345) |
||||
|
||||
def thresh_callback(val): |
||||
threshold = val |
||||
|
||||
# Detect edges using Canny |
||||
canny_output = cv.Canny(src_gray, threshold, threshold * 2) |
||||
|
||||
# Find contours |
||||
_, contours, hierarchy = cv.findContours(canny_output, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) |
||||
|
||||
# Draw contours |
||||
drawing = np.zeros((canny_output.shape[0], canny_output.shape[1], 3), dtype=np.uint8) |
||||
for i in range(len(contours)): |
||||
color = (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256)) |
||||
cv.drawContours(drawing, contours, i, color, 2, cv.LINE_8, hierarchy, 0) |
||||
|
||||
# Show in a window |
||||
cv.imshow('Contours', drawing) |
||||
|
||||
# Load source image |
||||
parser = argparse.ArgumentParser(description='Code for Finding contours in your image tutorial.') |
||||
parser.add_argument('--input', help='Path to input image.', default='../data/HappyFish.jpg') |
||||
args = parser.parse_args() |
||||
|
||||
src = cv.imread(args.input) |
||||
if src is None: |
||||
print('Could not open or find the image:', args.input) |
||||
exit(0) |
||||
|
||||
# Convert image to gray and blur it |
||||
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) |
||||
src_gray = cv.blur(src_gray, (3,3)) |
||||
|
||||
# Create Window |
||||
source_window = 'Source' |
||||
cv.namedWindow(source_window) |
||||
cv.imshow(source_window, src) |
||||
max_thresh = 255 |
||||
thresh = 100 # initial threshold |
||||
cv.createTrackbar('Canny Thresh:', source_window, thresh, max_thresh, thresh_callback) |
||||
thresh_callback(thresh) |
||||
|
||||
cv.waitKey() |
@ -0,0 +1,57 @@ |
||||
from __future__ import print_function |
||||
import cv2 as cv |
||||
import numpy as np |
||||
import argparse |
||||
import random as rng |
||||
|
||||
rng.seed(12345) |
||||
|
||||
def thresh_callback(val): |
||||
threshold = val |
||||
|
||||
# Detect edges using Canny |
||||
canny_output = cv.Canny(src_gray, threshold, threshold * 2) |
||||
|
||||
# Find contours |
||||
_, contours, _ = cv.findContours(canny_output, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) |
||||
|
||||
# Find the convex hull object for each contour |
||||
hull_list = [] |
||||
for i in range(len(contours)): |
||||
hull = cv.convexHull(contours[i]) |
||||
hull_list.append(hull) |
||||
|
||||
# Draw contours + hull results |
||||
drawing = np.zeros((canny_output.shape[0], canny_output.shape[1], 3), dtype=np.uint8) |
||||
for i in range(len(contours)): |
||||
color = (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256)) |
||||
cv.drawContours(drawing, contours, i, color) |
||||
cv.drawContours(drawing, hull_list, i, color) |
||||
|
||||
# Show in a window |
||||
cv.imshow('Contours', drawing) |
||||
|
||||
# Load source image |
||||
parser = argparse.ArgumentParser(description='Code for Convex Hull tutorial.') |
||||
parser.add_argument('--input', help='Path to input image.', default='../data/stuff.jpg') |
||||
args = parser.parse_args() |
||||
|
||||
src = cv.imread(args.input) |
||||
if src is None: |
||||
print('Could not open or find the image:', args.input) |
||||
exit(0) |
||||
|
||||
# Convert image to gray and blur it |
||||
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) |
||||
src_gray = cv.blur(src_gray, (3,3)) |
||||
|
||||
# Create Window |
||||
source_window = 'Source' |
||||
cv.namedWindow(source_window) |
||||
cv.imshow(source_window, src) |
||||
max_thresh = 255 |
||||
thresh = 100 # initial threshold |
||||
cv.createTrackbar('Canny thresh:', source_window, thresh, max_thresh, thresh_callback) |
||||
thresh_callback(thresh) |
||||
|
||||
cv.waitKey() |
@ -0,0 +1,83 @@ |
||||
from __future__ import print_function |
||||
from __future__ import division |
||||
import cv2 as cv |
||||
import numpy as np |
||||
import argparse |
||||
import random as rng |
||||
|
||||
rng.seed(12345) |
||||
|
||||
def thresh_callback(val): |
||||
threshold = val |
||||
|
||||
## [Canny] |
||||
# Detect edges using Canny |
||||
canny_output = cv.Canny(src_gray, threshold, threshold * 2) |
||||
## [Canny] |
||||
|
||||
## [findContours] |
||||
# Find contours |
||||
_, contours, _ = cv.findContours(canny_output, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) |
||||
## [findContours] |
||||
|
||||
# Get the moments |
||||
mu = [None]*len(contours) |
||||
for i in range(len(contours)): |
||||
mu[i] = cv.moments(contours[i]) |
||||
|
||||
# Get the mass centers |
||||
mc = [None]*len(contours) |
||||
for i in range(len(contours)): |
||||
# add 1e-5 to avoid division by zero |
||||
mc[i] = (mu[i]['m10'] / (mu[i]['m00'] + 1e-5), mu[i]['m01'] / (mu[i]['m00'] + 1e-5)) |
||||
|
||||
# Draw contours |
||||
## [zeroMat] |
||||
drawing = np.zeros((canny_output.shape[0], canny_output.shape[1], 3), dtype=np.uint8) |
||||
## [zeroMat] |
||||
## [forContour] |
||||
for i in range(len(contours)): |
||||
color = (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256)) |
||||
cv.drawContours(drawing, contours, i, color, 2) |
||||
cv.circle(drawing, (int(mc[i][0]), int(mc[i][1])), 4, color, -1) |
||||
## [forContour] |
||||
|
||||
## [showDrawings] |
||||
# Show in a window |
||||
cv.imshow('Contours', drawing) |
||||
## [showDrawings] |
||||
|
||||
# Calculate the area with the moments 00 and compare with the result of the OpenCV function |
||||
for i in range(len(contours)): |
||||
print(' * Contour[%d] - Area (M_00) = %.2f - Area OpenCV: %.2f - Length: %.2f' % (i, mu[i]['m00'], cv.contourArea(contours[i]), cv.arcLength(contours[i], True))) |
||||
|
||||
## [setup] |
||||
# Load source image |
||||
parser = argparse.ArgumentParser(description='Code for Image Moments tutorial.') |
||||
parser.add_argument('--input', help='Path to input image.', default='../data/stuff.jpg') |
||||
args = parser.parse_args() |
||||
|
||||
src = cv.imread(args.input) |
||||
if src is None: |
||||
print('Could not open or find the image:', args.input) |
||||
exit(0) |
||||
|
||||
# Convert image to gray and blur it |
||||
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) |
||||
src_gray = cv.blur(src_gray, (3,3)) |
||||
## [setup] |
||||
|
||||
## [createWindow] |
||||
# Create Window |
||||
source_window = 'Source' |
||||
cv.namedWindow(source_window) |
||||
cv.imshow(source_window, src) |
||||
## [createWindow] |
||||
## [trackbar] |
||||
max_thresh = 255 |
||||
thresh = 100 # initial threshold |
||||
cv.createTrackbar('Canny Thresh:', source_window, thresh, max_thresh, thresh_callback) |
||||
thresh_callback(thresh) |
||||
## [trackbar] |
||||
|
||||
cv.waitKey() |
@ -0,0 +1,51 @@ |
||||
from __future__ import print_function |
||||
from __future__ import division |
||||
import cv2 as cv |
||||
import numpy as np |
||||
|
||||
# Create an image |
||||
r = 100 |
||||
src = np.zeros((4*r, 4*r), dtype=np.uint8) |
||||
|
||||
# Create a sequence of points to make a contour |
||||
vert = [None]*6 |
||||
vert[0] = (3*r//2, int(1.34*r)) |
||||
vert[1] = (1*r, 2*r) |
||||
vert[2] = (3*r//2, int(2.866*r)) |
||||
vert[3] = (5*r//2, int(2.866*r)) |
||||
vert[4] = (3*r, 2*r) |
||||
vert[5] = (5*r//2, int(1.34*r)) |
||||
|
||||
# Draw it in src |
||||
for i in range(6): |
||||
cv.line(src, vert[i], vert[(i+1)%6], ( 255 ), 3) |
||||
|
||||
# Get the contours |
||||
_, contours, _ = cv.findContours(src, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) |
||||
|
||||
# Calculate the distances to the contour |
||||
raw_dist = np.empty(src.shape, dtype=np.float32) |
||||
for i in range(src.shape[0]): |
||||
for j in range(src.shape[1]): |
||||
raw_dist[i,j] = cv.pointPolygonTest(contours[0], (j,i), True) |
||||
|
||||
minVal, maxVal, _, _ = cv.minMaxLoc(raw_dist) |
||||
minVal = abs(minVal) |
||||
maxVal = abs(maxVal) |
||||
|
||||
# Depicting the distances graphically |
||||
drawing = np.zeros((src.shape[0], src.shape[1], 3), dtype=np.uint8) |
||||
for i in range(src.shape[0]): |
||||
for j in range(src.shape[1]): |
||||
if raw_dist[i,j] < 0: |
||||
drawing[i,j,0] = 255 - abs(raw_dist[i,j]) * 255 / minVal |
||||
elif raw_dist[i,j] > 0: |
||||
drawing[i,j,2] = 255 - raw_dist[i,j] * 255 / maxVal |
||||
else: |
||||
drawing[i,j,0] = 255 |
||||
drawing[i,j,1] = 255 |
||||
drawing[i,j,2] = 255 |
||||
|
||||
cv.imshow('Source', src) |
||||
cv.imshow('Distance', drawing) |
||||
cv.waitKey() |
Loading…
Reference in new issue