LoginSignup
1
0

More than 3 years have passed since last update.

Laplacian Filterの実験

Posted at

概要

エッジ検出(実際はエッジというよりただの色の違いなのだけど)しようと思って4方向と8方向のLaplacian Filterどっちが良いのか気になったから実験してみた。

入力画像

gray.png

4方向Laplacian Filter

img_lap.png

8方向Laplacian Filter

img_lap copy.png

結論

ほとんど同じやないかい!
結果が同じなので、とりあえず回転にも影響が少なさそうな8方向のFilterの方がを使って行きたいと思う

コード

8方向4方向お好きな方をどうぞ

   def LaplacianLayer(self, img):
        # 4 direction Laplacian
        laplacian_filter = torch.cuda.FloatTensor(
            [[0, 1, 0], [1, -4, 1], [0, 1, 0]]).view(1, 1, 3, 3)
        # 8 direction Laplacian
        # laplacian_filter = torch.cuda.FloatTensor(
        #     [[1, 1, 1], [1, -8, 1], [1, 1, 1]]).view(1, 1, 3, 3)

        gray = self.getGrayImage(img)

        img_lap = torch.nn.functional.conv2d(input=gray,
                                            weight=Variable(laplacian_filter),
                                            stride=1,
                                            padding=0)

        img_lap = torch.abs(img_lap)

        return img_lap

    def getGrayImage(self,rgbImg):
        gray = 0.114*rgbImg[:,0,:,:] + 0.587*rgbImg[:,1,:,:] + 0.299*rgbImg[:,2,:,:]
        gray = torch.unsqueeze(gray,1)
        return gray

参考文献

【画像処理】ラプラシアンフィルタの原理・特徴・計算式
https://algorithm.joho.info/image-processing/laplacian-filter/

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0