0

我在将多个图像存储在图像数组中并在此后显示时遇到了一个问题。我遇到的问题有点烦人。在显示图像时,即使在提到索引之后,最后一个图像也会显示。我检查了大小,它是好的。这是我尝试过的示例代码

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

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

4

1 回答 1

1

这是你的问题:

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

该方法crop是将裁剪后的图像保存到memory变量中。然后你将它分配给数组的(i-1)th元素。ImgSeg在下一次迭代中,您正在更改 的内容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();

于 2017-03-21T18:50:16.807 回答