Create upper and lower bounds variables and setting up the class to display an image
[3501/OpenCVShowImage] / IsolateImage.java
1 import java.awt.Graphics;
2 import java.awt.Panel;
3 import java.awt.image.BufferedImage;
4 import java.io.IOException;
5 import java.net.URL;
6
7 import javax.imageio.ImageIO;
8 import javax.swing.JFrame;
9
10 import org.opencv.core.Core;
11 import org.opencv.core.Scalar;
12
13 public class IsolateImage extends Panel {
14 BufferedImage image;
15
16 public static final Scalar LOWER_BOUNDS = new Scalar(103, 164, 169);
17 public static final Scalar UPPER_BOUNDS = new Scalar(125, 229, 255);
18
19 static {
20 System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
21 }
22
23 public IsolateImage() {
24 try {
25 // image file
26 // name is
27 // testpicture.png
28 URL input = this.getClass().getResource(("imgs/testpicture.png"));
29 // this will read the image to save
30 // into a BufferedImage variable
31 image = ImageIO.read(input);
32 } catch (IOException ie) {
33 // this is to spill
34 // out a message
35 // saying
36 // something
37 // doesn't work
38 System.out.println("Error: " + ie.getMessage());
39 }
40 }
41
42 public void paint(Graphics g) {
43 // This is to actually draw the image out,
44 // necessary.
45
46 // Input of a bufferedimage, x, y, then
47 // an observer in this case null.
48 g.drawImage(image, 0, 0, null);
49 }
50
51 public static void main(String args[]) throws Exception {
52 // Creates a window to
53 // display the image
54 JFrame frame = new JFrame("Display image");
55
56 // Gets the image from ShowImage() method
57 Panel panel = new IsolateImage();
58
59 // Adds the image to the new window
60 // panel
61 frame.getContentPane().add(panel);
62
63 // sets the size of the panel
64 frame.setSize(500, 500);
65 // makes it visible
66 frame.setVisible(true);
67 }
68 }