在JPanel上显示检测到的人脸 [英] Displaying detected faces on a JPanel

查看:241
本文介绍了在JPanel上显示检测到的人脸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个用于从输入图像中检测人脸的项目.我在Java中使用opencv. 我面临的问题如下

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. 检测到的脸部将放置在JLabel s setIcon方法上.
  2. 第一次放置面孔,但对于下一张图像,先前的面孔不会被清除.
  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)添加面孔:

jFaceLabel是JLabel数组,初始化为大小100 jpDetectedImage是一个包含JLabel(面孔)的JPanel

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)去除面部:

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

问题是,每个面孔第一次都显示在JLabel上,但是连续检测到面孔会导致旧面孔重叠. 检测到的面部存储在物理路径上,并在加载用于检测的图像时删除.

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.

我需要从jpDetectedImage面板中删除jFaceLabel数组,并为每个连续的检测阶段重新分配内存.

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

如何动态地从JPanel删除JLabel,并使用新的ImageIcon再次添加它们?

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

推荐答案

执行此操作的更好方法是使用setIcon()在适当位置更新标签的图标,如下所示

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

一种不太灵活的选择是删除组件并验证容器,如此处所示.

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

这篇关于在JPanel上显示检测到的人脸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆