1

我正在尝试使用带有 Java 的 BoofCV 来实现 HOG 描述符。HOG 的步骤之一是应用一维内核,如下所示:

Gx(x,y)=[-1 0 1]* I(x,y)

Gy(x,y)=[-1 0 1]T *I(x,y)

我为此步骤编写的代码如下:

import java.awt.image.BufferedImage;
import java.io.File;
import boofcv.alg.filter.convolve.GConvolveImageOps;
import boofcv.core.image.ConvertBufferedImage;
import boofcv.core.image.border.ImageBorder_F32;
import boofcv.gui.image.ShowImages;
import boofcv.io.image.UtilImageIO;
import boofcv.struct.convolve.*;
import boofcv.struct.image.ImageFloat32;

public class Main {

    public static void main(String[] args) {
        String IMAGE_LOCATION = "D:/dog_orig.jpg";
        File image_file = new File(IMAGE_LOCATION);

        if(!image_file.exists()) {
            System.err.println("Image not Found");
            System.exit(1);
        }

        // Load image
        BufferedImage pic  = UtilImageIO.loadImage(IMAGE_LOCATION   );

        // Initialize destinations images
        ImageFloat32 dest1 = new ImageFloat32(pic.getWidth(), pic.getHeight());
        ImageFloat32 dest2 = new ImageFloat32(pic.getWidth(), pic.getHeight());
        ImageFloat32 distortedImg = ConvertBufferedImage.convertFrom(pic, dest1);

        // Initialize kernel
        Kernel1D_F32 kx = new Kernel1D_F32(3);
        Kernel1D_F32 ky = new Kernel1D_F32(3);
        kx.data[0] = -1;
        kx.data[1] = 0;
        kx.data[2] = 1;

        ky.data[0] = 1;
        ky.data[1] = 0;
        ky.data[2] = -1;

        ImageBorder_F32 border = new ImageBorder_F32() {
            @Override
            public void setOutside(int arg0, int arg1, float arg2) {
                // TODO Auto-generated method stub   
            }
            @Override
            public float getOutside(int arg0, int arg1) {
                // TODO Auto-generated method stub
                return 0;
            }
        };

        // Apply 1-D mask horizontal
        GConvolveImageOps.horizontal(kx, distortedImg, dest2, border);
        // Apply 1-D mask vertical
        GConvolveImageOps.vertical(ky, distortedImg, dest2, border);

        BufferedImage destenation = new BufferedImage(dest2.getWidth(),dest2.getHeight(),BufferedImage.TYPE_BYTE_GRAY);
        destenation=ConvertBufferedImage.convertTo(distortedImg, destenation);
        ShowImages.showWindow( destenation,"image after apply kernel ");

        System.out.print("Done");
    }
}

但是我应用蒙版后图像没有改变。

输入图像。 图 1

应用蒙版后的图像。 图 2

这是真的还是代码中有错误并且图像应该更改?

问候!提前致谢!

4

0 回答 0