Store image to Array - java

Before adding image to JLabel, I used below code to resize them.
BufferedImage myPicture1 = ImageIO.read(new
File("C:\\Users\\yumi\\Desktop\\Salad.png"));
Image scaled1 = myPicture1.getScaledInstance(80,95,Image.SCALE_SMOOTH);
JLabel picLabel1 = new JLabel("Japanese Noodles",new
ImageIcon(scaled1),JLabel.CENTER);
panel.add(picLabel1);
Now I have array, want to store image to array
static private JLabel[] foodLabel;
static private JTextField[] qtyField;
static private ImageIcon[] imageIcon;
static private Image[] imageScaled;
static private BufferedImage[] image;
static private File[] file;
private static final int ELEMENTS = 9;
Trying to read file and scale it
file[0] = new File("C:\\Users\\yumi\\Desktop\\Salad.png");
.....
for (int i = 0; i < ELEMENTS; i++) {
image[i] = ImageIO.read(file[i]);
imageScaled[i] = image[i].getScaledInstance(80,95,Image.SCALE_SMOOTH);
foodLabel[i] = new JLabel(imageIcon([imageScaled[i]])); // error
}
Error
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
Syntax error on token "(", Expression expected after this token

The following should work. You have to create an ImageIcon for your scaled image first.
for (int i = 0; i < ELEMENTS; i++) {
image[i] = ImageIO.read(file[i]);
imageScaled[i] = image[i].getScaledInstance(80,95,Image.SCALE_SMOOTH);
imageIcon[i] = new ImageIcon(imageScaled[i]);
foodLabel[i] = new JLabel(imageIcon[i]);
}
Note that there seems to be no reason to keep all those values in an array. Unless you have more code that references those arrays the following is a bit cleaner:
for (int i = 0; i < ELEMENTS; i++) {
Image image = ImageIO.read(file[i]);
Image imageScaled = image.getScaledInstance(80,95,Image.SCALE_SMOOTH);
ImageIcon imageIcon = new ImageIcon(imageScaled);
foodLabel[i] = new JLabel(imageIcon);
}

Related

how to use Yolo v3 with Java to detect objects (cows to be specific) in pictures:

I am working on some examples I found online to understand more about Yolo usage in java. I got this code that I edited a little and it can detect objects in videos but now I want to do it with Pictures and I am kinda struggling with it. I would appreciate if anyone can show me how to edit it or has an advise or a method to solve it .
The code:
`class yolo {
private static List<String> getOutputNames(Net net) {
List<String> names = new ArrayList<>();
List<Integer> outLayers = net.getUnconnectedOutLayers().toList();
List<String> layersNames = net.getLayerNames();
outLayers.forEach((item) -> names.add(layersNames.get(item - 1)));//unfold and create R-CNN layers from the loaded YOLO model//
return names;
}
public static void main(String[] args) throws InterruptedException {
System.load("C:\\Users\\LENOVO\\Desktop\\Java1\\Yolo\\opencv\\build\\java\\x64\\opencv_java400.dll");
System.out.println("Library Loaded");
System.load("C:\\Users\\LENOVO\\Desktop\\Java1\\Yolo\\opencv\\build\\java\\x64\\opencv_java400.dll");
String modelWeights = "C:\\Users\\LENOVO\\Desktop\\Java1\\Yolo\\yolov3.weights";
String modelConfiguration = "C:\\Users\\LENOVO\\Desktop\\Java1\\Yolo\\yolov3.cfg.txt";
String filePath = "C:\\Users\\LENOVO\\Desktop\\cows.mp4";
VideoCapture cap = new VideoCapture(filePath);
Mat frame = new Mat();
Mat dst = new Mat ();
//cap.read(frame);
JFrame jframe = new JFrame("Video");
JLabel vidpanel = new JLabel();
jframe.setContentPane(vidpanel);
jframe.setSize(600, 600);
jframe.setVisible(true);
Net net = Dnn.readNetFromDarknet(modelConfiguration, modelWeights);
//Thread.sleep(5000);
//Mat image = Imgcodecs.imread("D:\\yolo-object-detection\\yolo-object-detection\\images\\soccer.jpg");
Size sz = new Size(288,288);
List<Mat> result = new ArrayList<>();
List<String> outBlobNames = getOutputNames(net);
while (true) {
if (cap.read(frame)) {
Mat blob = Dnn.blobFromImage(frame, 0.00392, sz, new Scalar(0), true, false);
net.setInput(blob);
net.forward(result, outBlobNames);
// outBlobNames.forEach(System.out::println);
// result.forEach(System.out::println);
float confThreshold = 0.6f;
List<Integer> clsIds = new ArrayList<>();
List<Float> confs = new ArrayList<>();
List<Rect> rects = new ArrayList<>();
for (int i = 0; i < result.size(); ++i)
{
Mat level = result.get(i);
for (int j = 0; j < level.rows(); ++j)
{
Mat row = level.row(j);
Mat scores = row.colRange(5, level.cols());
Core.MinMaxLocResult mm = Core.minMaxLoc(scores);
float confidence = (float)mm.maxVal;
Point classIdPoint = mm.maxLoc;
if (confidence > confThreshold)
{
int centerX = (int)(row.get(0,0)[0] * frame.cols());
int centerY = (int)(row.get(0,1)[0] * frame.rows());
int width = (int)(row.get(0,2)[0] * frame.cols());
int height = (int)(row.get(0,3)[0] * frame.rows());
int left = centerX - width / 2;
int top = centerY - height / 2;
clsIds.add((int)classIdPoint.x);
confs.add((float)confidence);
rects.add(new Rect(left, top, width, height));
}
}
}
float nmsThresh = 0.5f;
MatOfFloat confidences = new MatOfFloat(Converters.vector_float_to_Mat(confs));
Rect[] boxesArray = rects.toArray(new Rect[0]);
MatOfRect boxes = new MatOfRect(boxesArray);
MatOfInt indices = new MatOfInt();
Dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThresh, indices);
int [] ind = indices.toArray();
int j=0;
for (int i = 0; i < ind.length; ++i)
{
int idx = ind[i];
Rect box = boxesArray[idx];
Imgproc.rectangle(frame, box.tl(), box.br(), new Scalar(0,0,255), 2);
//i=j;
System.out.println(idx);
}
// Imgcodecs.imwrite("D://out.png", image);
//System.out.println("Image Loaded");
ImageIcon image = new ImageIcon(Mat2bufferedImage(frame));
vidpanel.setIcon(image);
vidpanel.repaint();
// System.out.println(j);
//System.out.println("Done");
}
}
}
// }
private static BufferedImage Mat2bufferedImage(Mat image) {
MatOfByte bytemat = new MatOfByte();
Imgcodecs.imencode(".jpg", image, bytemat);
byte[] bytes = bytemat.toArray();
InputStream in = new ByteArrayInputStream(bytes);
BufferedImage img = null;
try {
img = ImageIO.read(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return img;
}`
`

OpenCV java One Line needed to Explain about Detection

sorry for my question i'm going to learn about openCv in java and one example i found that have line with something i can't understand
detections = detections.reshape(1, (int)detections.total() / 7);
From full code :
public class DeepNeuralNetworkProcessor {
private Net net;
private final String[] classNames = {"background",
"aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair",
"cow", "diningtable", "dog", "horse",
"motorbike", "person", "pottedplant",
"sheep", "sofa", "train", "tvmonitor"};
public DeepNeuralNetworkProcessor() {
this.net = Dnn.readNetFromCaffe(Files_Path.DDN_PROTO, Files_Path.DDN_MODEL);
}
public List<DnnObject> getObjectsInFrame(Mat frame, boolean isGrayFrame) {
int inWidth = 320;
int inHeight = 240;
double inScaleFactor = 0.007843;
double thresholdDnn = 0.2;//Minimum value to detect the object
double meanVal = 127.5;
Mat blob = null;
Mat detections = null;
List<DnnObject> objectList = new ArrayList<>();
int cols = frame.cols();
int rows = frame.rows();
try {
if (isGrayFrame)
Imgproc.cvtColor(frame, frame, Imgproc.COLOR_GRAY2RGB);
blob = Dnn.blobFromImage(frame, inScaleFactor,
new Size(inWidth, inHeight),
new Scalar(meanVal, meanVal, meanVal),
false, false);
net.setInput(blob);
detections = net.forward();
detections = detections.reshape(1, (int) detections.total() / 7);
//all detected objects
for (int i = 0; i < detections.rows(); ++i) {
double confidence = detections.get(i, 2)[0];
if (confidence < thresholdDnn)
continue;
int classId = (int) detections.get(i, 1)[0];
//...
}
} catch (Exception ex) {
ex.printStackTrace();
}
return objectList;
}
}
can anyone explain what this line does ?
or better clear explain about the detection mat.
and what net.forward do?
please send me some reference for java opencv or deeplearning4j

WARNING Possible use of "Transverse_Mercator" projection outside its valid area

I am trying to combign tiff image and shapefile and want show it. For this, I am using GeoTiff and I am stuck that my tiff file is now being displayed. Shapefile is showing properly but tiff image, which is having only 1 band and grey scale index, is not being shown because of some reason. I am getting one warning message as below.
2016-08-04T12:43:06.456+0530 WARNING Possible use of "Transverse_Mercator" projection outside its valid area.
Latitude 180°00.0'S is out of range (±90°).
How can I remove this message?
My code is as below
private void displayLayers() throws Exception {
AbstractGridFormat format = GridFormatFinder.findFormat(this.getBlueMarble());
this.setGridCoverageReader(format.getReader(this.getBlueMarble()));
Style rgbStyle = this.createRGBStyle();
// connect to the shapefile
FileDataStore dataStore = FileDataStoreFinder.getDataStore(this.getBorderShape());
SimpleFeatureSource shapefileSource = dataStore.getFeatureSource();
Style shpStyle = SLD.createPolygonStyle(Color.BLUE, null, 0.0f);
MapContent map = new MapContent();
map.getViewport().setCoordinateReferenceSystem(
DefaultGeographicCRS.WGS84);
map.setTitle("Illegal Mining");
Layer rasterLayer = new GridReaderLayer(this.getGridCoverageReader(), rgbStyle);
map.addLayer(rasterLayer);
Layer shpLayer = new FeatureLayer(shapefileSource, shpStyle);
map.addLayer(shpLayer);
System.out.println("Trying to show on map...");
JMapPane mapPane = new JMapPane();
mapPane.setMapContent(map);
mapPane.setDisplayArea(shapefileSource.getBounds());
//mapPane.setDisplayArea(this.getGridCoverageReader().getOriginalEnvelope());
this.add(mapPane, BorderLayout.CENTER);
}
private Style createRGBStyle() {
GridCoverage2DReader reader = this.getGridCoverageReader();
StyleFactory sf = this.getStyleFactory();
GridCoverage2D cov = null;
try {
cov = reader.read(null);
} catch (IOException giveUp) {
throw new RuntimeException(giveUp);
}
// We need at least three bands to create an RGB style
int numBands = cov.getNumSampleDimensions();
System.out.println("numBands:"+numBands);
if (numBands < 3) {
System.out.println("Bands are less than 3");
//return null;
}
// Get the names of the bands
String[] sampleDimensionNames = new String[numBands];
for (int i = 0; i < numBands; i++) {
GridSampleDimension dim = cov.getSampleDimension(i);
sampleDimensionNames[i] = dim.getDescription().toString();
}
final int RED = 0, GREEN = 1, BLUE = 2;
int[] channelNum = { -1, -1, -1 };
Boolean greyflag=false;
// We examine the band names looking for "red...", "green...",
// "blue...".
// Note that the channel numbers we record are indexed from 1, not 0.
for (int i = 0; i < numBands; i++) {
String name = sampleDimensionNames[i].toLowerCase();
System.out.println("name :"+name);
if (name != null) {
if (name.matches("red.*")) {
channelNum[RED] = i + 1;
} else if (name.matches("green.*")) {
channelNum[GREEN] = i + 1;
} else if (name.matches("blue.*")) {
channelNum[BLUE] = i + 1;
}else if(name.matches("gray.*")){
System.out.println("What to do here");
channelNum[RED] = 1;
channelNum[GREEN] = 2;
channelNum[BLUE] = 3;
greyflag=true;
}
}
}
// If we didn't find named bands "red...", "green...", "blue..."
// we fall back to using the first three bands in order
if(greyflag==false){
if (channelNum[RED] < 0 || channelNum[GREEN] < 0
|| channelNum[BLUE] < 0) {
channelNum[RED] = 1;
channelNum[GREEN] = 2;
channelNum[BLUE] = 3;
}
}
// Now we create a RasterSymbolizer using the selected channels
SelectedChannelType[] sct = new SelectedChannelType[cov
.getNumSampleDimensions()];
ContrastEnhancement ce = sf.contrastEnhancement(this.ff.literal(1.0),
ContrastMethod.NORMALIZE);
for (int i = 0; i < numBands; i++) {
sct[i] = sf.createSelectedChannelType(
String.valueOf(channelNum[i]), ce);
System.out.println(String.valueOf(channelNum[i]));
}
RasterSymbolizer sym = sf.getDefaultRasterSymbolizer();
ChannelSelection sel =sf.channelSelection(sct[RED]);
if(numBands>1){
sel = sf.channelSelection(sct[RED], sct[GREEN],
sct[BLUE]);
}
sym.setChannelSelection(sel);
return SLD.wrapSymbolizers(sym);
}
I just pass two files as below code
public MapImagePanel() {
this.setLayout(new BorderLayout(0, 0));
this.setBackground(Color.BLUE);
this.setPreferredSize(new Dimension(720, 360));
this.setBlueMarble(new File("E:/tifffilename.TIFF"));
this.setBorderShape(new File("E:/shapefilename.shp"));
try {
this.displayLayers();
} catch (Exception e) {
e.printStackTrace();
}
}
This is how i use this class in main class
//see output in main method
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MapImagePanel panel = new MapImagePanel();
panel.setPreferredSize(new Dimension(1024,768));
panel.setVisible(true);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
frame.show();
TLDR; add the following line to your program start up:
System.setProperty("org.geotools.referencing.forceXY", "true");
From GeoTools FAQ as computer programmers they knew that coordinates would be expressed as longitude,latitude pairs so they could use existing graphics code easily by treating them as a simple (x,y) pair. but for sequence like (x,y) or (y,x) they confused that is why this error is coming.

Creating multiple JToggleButton with indivdual ImageIcon

ImageIcon img_1 = new ImageIcon("src/menu/btn_1.jpg");
ImageIcon img_2 = new ImageIcon("src/menu/btn_2.jpg");
ImageIcon img_3 = new ImageIcon("src/menu/btn_3.jpg");
ImageIcon img_4 = new ImageIcon("src/menu/btn_4.jpg");
ImageIcon img_5 = new ImageIcon("src/menu/btn_5.jpg");
ImageIcon img_6 = new ImageIcon("src/menu/btn_6.jpg");
ImageIcon img_7 = new ImageIcon("src/menu/btn_7.jpg");
ImageIcon img_8 = new ImageIcon("src/menu/btn_8.jpg");
ImageIcon img_9 = new ImageIcon("src/menu/btn_9.jpg");
bgNumbers = new ButtonGroup();
btnNumbers = new JToggleButton[9];
for (int i = 0; i < 9; i++) {
btnNumbers[i] = new JToggleButton(???);
.....
.....
}
How do I insert the individuals ImageIcon img_1, img_2 ....... img_9 into the loop?
Add them to an array instead of individual variables. – Gábor Bakos Apr 4 at 7:35

How can I get an Image and then Use it to make an ImageIcon in Java GUI

I am trying to make a card game and Am running into an issue with my getImage() function.
I have an String Array of the Cards ex:
private static String[] hearts = {"ah.gif","2h.gif","3h.gif","4h.gif","5h.gif","6h.gif","7h.gif","8h.gif","9h.gif","th.gif","jh.gif","qh.gif","kh.gif"};
My getImage() looks like this:
public String getImage(Card card){
if (0 == suit){
img = hearts[rank];
}
else if (1 == suit){
img = spades[rank];
}
else if (2 == suit){
img = diamond[rank];
}
else if (3 == suit){
img = clubs[rank];
}
However, because it is stored as a string, I get an error when I try to use the img as an ImgIcon Ex:
ImageIcon image = (card.getImage(card));
JLabel label = new JLabel(image);
Any Ideas? Thanks
Create an array of ImageIcons and use the String array and a for loop to create the Icon array. Simple. :)
// in declaration section
private ImageIcon heartsIcons = new ImageIcon[hearts.length];
// in code section
try {
for (int i = 0; i < hearts.length; i++) {
// you may need different code to get the Image file vs. resource.
BufferedImage img = ImageIO.read(getClass().getResource(hearts[i]));
heartsIcons[i] = new ImageIcon(img);
}
} catch (IOException e) {
e.printStackTrace();
}

Categories

Resources