0

I have come across a problem while storing multiple images in an image array and displaying it thereafter.The problem i encounter is somewhat irritating.while displaying the images the last image gets displayed even after mentioning the index.I have check the size and it was ok.Heres the sample code i have tried

import marvin.image.*;
import marvin.io.*;
import marvin.gui.*;

import java.awt.FlowLayout;
import java.awt.Image;

import javax.swing.JFrame;


public class apples {
public static int WORLD_WIDTH = 500;
public static int WORLD_HEIGHT = 300;
public static void main(String[] args){
    JFrame worldFrame = new JFrame("world");
    worldFrame.getContentPane();
    worldFrame.add(new world(WORLD_WIDTH, WORLD_HEIGHT));
    worldFrame.setVisible(true);
    worldFrame.setSize(WORLD_WIDTH , WORLD_HEIGHT+30);
    worldFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    segmentObjects segment = new segmentObjects();

}
}

heres the world class:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

public class world extends JPanel{

static int SPACEX;
static int SPACEY ;
public static Image world;
static int movex = 100;
static int movey = 240;

public world(int WORLD_WIDTH, int WORLD_HEIGHT) {
    SPACEX = WORLD_WIDTH;
    SPACEY = WORLD_HEIGHT;
}

public void paint(Graphics g) {

    BufferedImage worldB = new     BufferedImage(SPACEX,SPACEY,BufferedImage.TYPE_BYTE_BINARY);
    Graphics worldG = worldB.getGraphics();
    worldG.setColor(Color.WHITE);
    worldG.fillRect(0, 0,SPACEX, SPACEY); // draw the space

    worldG.setColor(Color.BLACK);

    //worldG.fillRect(movex, movey,50, 50); //draw the agent
    worldG.fillOval(300, 100, 50, 50);
    //worldG.fillOval(395, 100, 50, 50);
    for(int x =220;x<300;x++){
    worldG.drawLine(200, 200, x, 250);}


    world = worldB;
    g.drawImage(world, 0, 0, this);
}
}

heres the segment class:

import static marvin.MarvinPluginCollection.floodfillSegmentation;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.LinkedList;

import javax.swing.JFrame;
import static marvin.MarvinPluginCollection.*;
import marvin.gui.MarvinImagePanel;
import marvin.image.MarvinBlob;
import marvin.image.MarvinBlobSegment;
import marvin.image.MarvinContour;
import marvin.image.MarvinImage;
import marvin.image.MarvinSegment;
import marvin.io.MarvinImageIO;
import marvin.math.Point;

public class segmentObjects extends JFrame{
static MarvinImage original = new MarvinImage((BufferedImage) world.world);
static public int x1;
static public int y1;
static public int width;
static public int height;
static MarvinImage memory = new MarvinImage();
static ArrayList<Point> countorPoints = new ArrayList<Point>();
static int counter;
static MarvinImage[] ImgSeg=new MarvinImage[10];


public segmentObjects(){
super("segments");
MarvinImage image = original.clone();

MarvinSegment[] segments = floodfillSegmentation(image);

for(int i=1; i<segments.length; i++){

    MarvinSegment seg = segments[i];
    x1 =seg.x1; 
    y1 = seg.y1;
    width = seg.width; 
    height =seg.height;
    crop(original,memory,x1,y1,width,height);

    ImgSeg[i-1]=memory;
    //System.out.println(width+"  "+height);


    //System.out.println(ImgSeg.size()); 
    counter =segments.length-1;
    //contour(i);

}

System.out.println(ImgSeg[1].getWidth());
MarvinImagePanel imagePanel = new MarvinImagePanel();
imagePanel.setImage(ImgSeg[0]);
add(imagePanel);
setSize(400,630);
setVisible(true);
}  
}

Sorry if there is any minor errors like missing curl braces since i had to cut paste from my main project

  • Can you post a minimal working example? – ahoxha Mar 21 '17 at 16:33
  • I dont understand what example u r referring.its just not working the way it should be.thats the problem – Nizam Ahmed Mar 21 '17 at 16:37
  • I just used an image to segment different objects in a image and segment those objects i mean crop those objects using the coordinates and save each objects int that image array and later be displayed by used will.but when i tried to display the images using the index.it just displays the last image.I dont know wherses the problem.but if there is any kind of suggested alternative it would be really appreciable. – Nizam Ahmed Mar 21 '17 at 16:40
  • By example, I mean a class with a `main` method so that I can copy-paste into my environment and try it. – ahoxha Mar 21 '17 at 16:45
  • If it's not possible, then try debugging it and see if you are replacing elements of the array with the last image. From what you are telling me, it sounds something like that. – ahoxha Mar 21 '17 at 16:48
  • I have attached the classes the best i could.I hope you find the problem – Nizam Ahmed Mar 21 '17 at 18:10

1 Answers1

1

Here's your problem:

crop(original,memory,x1,y1,width,height);
ImgSeg[i-1]=memory;

The method crop is saving the cropped image into the memory variable. Then you are assigning it to the (i-1)th element of the ImgSeg array. In the next iteration, you are changing the contents of memory, but still the previous element of the ImgSeg array is pointing to this object, which means now it will have the new contents you just saved into memory. Thus, in the end (the last iteration), memory will have the contents of the last image, and all elements of ImgSeg are pointing to the same memory object.

To solve it, create a new MarvingImage inside the loop, save the cropped image in this object then assign it to the array's (i-1)th element.

for(int i=1; i<segments.length; i++){
    MarvinImage memory = new MarviImage();
    MarvinSegment seg = segments[i];
    x1 =seg.x1; 
    y1 = seg.y1;
    width = seg.width; 
    height =seg.height;
    crop(original,memory,x1,y1,width,height);

    ImgSeg[i-1]=memory;
    //System.out.println(width+"  "+height);

    //System.out.println(ImgSeg.size()); 
    counter =segments.length-1;
    //contour(i);
}

Remove this line static MarvinImage memory = new MarvinImage();

ahoxha
  • 1,919
  • 11
  • 21
  • @NizamAhmed, as Andrew Thompson pointed out, please [accept the answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235), so that people know it has been answered. This way, you and I, both earn some reputation. Thanks! – ahoxha Mar 21 '17 at 19:08