Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

java - Displaying detected faces on a JPanel

I am working on a project for detecting faces from an input image. I am using opencv with Java. The problem which I'm facing is as below

  1. The faces that are detected are to be placed on a JLabels setIcon method.
  2. First time it places the faces, but for the next image, the previous faces are not cleared.

Following code that I tried to add and remove faces

1) Adding faces:

jFaceLabel is JLabel array initialized to size 100 jpDetectedImage is a JPanel which contains the JLabels (faces)

jFaceLabel = new JLabel[100];

for(int index=0;index<ImageHandler.noOfDetections;index++){
    jFaceLabel[index] = new JLabel();
    jFaceLabel[index].setIcon(new ImageIcon("C://Users//Public//Pictures//Sample Pictures//TestPics//temp//"+index+".jpg"));
    //jFaceLabel[index].setIcon(face);
    int x = this.jpDetectedImage.getX() + (index%2) * 64 + 10 * ((index%2)+1);
    int y = this.jpDetectedImage.getY() + (index/2) * 64 + 10 * ((index/2)+1);
    jFaceLabel[index].setBounds(x, y, 64, 64);
    this.jpDetectedImage.add(jFaceLabel[index]);

    if(index>8 && (index%2==0)){
        this.jpDetectedImage.setPreferredSize(new Dimension(
        this.jpDetectedImage.getPreferredSize().width,
        this.jpDetectedImage.getPreferredSize().height + 74
        ));
    }
    System.out.println("Placed : "+tempPath+"//"+index+".jpg");
}
jpDetectedImage.repaint();

2) Removing faces:

for(int j=0;j<ImageHandler.noOfDetections;j++){
    jFaceLabel[j].getParent().remove(jFaceLabel[j]);
}
this.jpDetectedImage.repaint();

The problem is, the first time every faces gets displayed on the JLabels but successive detection of faces, result in overlapping of the old faces. The detected faces are stored at a physical path and are deleted when a image for detection is loaded.

I require, is the removal of the jFaceLabel array from jpDetectedImage panel and a new memory allocation for every successive detection phase.

How to remove the JLabels from JPanel dynamically and add them again with a new ImageIcon?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The better way to do this is to update the label's icon in place using setIcon(), as shown here.

image1

A less flexible alternative is to remove the component and validate the container, as shown here.

image2


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...