无法在图像数组中存储和显示多个图像



我在图像数组中存储多个图像并随后显示时遇到了问题。我遇到的问题有些烦人。在显示图像时,即使在提到索引后也会显示最后一个图像。我已经检查了尺寸,还可以。这是我尝试过的示例代码

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();
}
}

这是世界级的:

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);
}
}

下面是段类:

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);
}  
}

抱歉,如果有任何小错误,例如缺少卷曲大括号,因为我不得不从我的主项目中剪切粘贴

这是你的问题:

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

crop方法是将裁剪后的图像保存到memory变量中。然后,将其分配给ImgSeg数组的(i-1)th元素。在下一次迭代中,您正在更改 memory 的内容,但 ImgSeg 数组的上一个元素仍然指向此对象,这意味着现在它将拥有您刚刚保存到 memory 中的新内容。因此,在最后(最后一次迭代(中,memory将拥有最后一个图像的内容,并且ImgSeg的所有元素都指向同一个memory对象。

要解决这个问题,请在循环内创建一个新MarvingImage,将裁剪的图像保存在此对象中,然后将其分配给数组的(i-1)th元素。

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);
}

删除此行static MarvinImage memory = new MarvinImage();

相关内容

  • 没有找到相关文章

最新更新