LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to apply bilateral filter over a 2d matrix image

Solved!
Go to solution

I have got ans(just image) for this bilateral filter but confused with Call Library Function Node

 

How to attempt a bilateral filter over a 2d Matrix image

 

gptshubham595_0-1611054294550.png

 

0 Kudos
Message 1 of 26
(2,162 Views)

I want to apply bilateral filter to this image 

I have code for matlab how to convert it in labview

 

Img=360x360

patchVar = std2(img)^2;

DoS = 2*patchVar;

J = imbilatfilt(I,DoS);

imshow(J)

0 Kudos
Message 2 of 26
(2,154 Views)

No need for double posts, keep things in one place...

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 3 of 26
(2,125 Views)

I have a python code also for this without any  builtin function 

But unable to convert this for Labview

 

import numpy as np
import cv2
import sys
import math


def distance(x, y, i, j):
    return np.sqrt((x-i)**2 + (y-j)**2)


def gaussian(x, sigma):
    return (1.0 / (2 * math.pi * (sigma ** 2))) * math.exp(- (x ** 2) / (2 * sigma ** 2))


def apply_bilateral_filter(source, filtered_image, x, y, diameter, sigma_i, sigma_s):
    hl = diameter/2
    i_filtered = 0
    Wp = 0
    i = 0
    while i < diameter:
        j = 0
        while j < diameter:
            neighbour_x = x - (hl - i)
            neighbour_y = y - (hl - j)
            if neighbour_x >= len(source):
                neighbour_x -= len(source)
            if neighbour_y >= len(source[0]):
                neighbour_y -= len(source[0])
            gi = gaussian(source[neighbour_x][neighbour_y] - source[x][y], sigma_i)
            gs = gaussian(distance(neighbour_x, neighbour_y, x, y), sigma_s)
            w = gi * gs
            i_filtered += source[neighbour_x][neighbour_y] * w
            Wp += w
            j += 1
        i += 1
    i_filtered = i_filtered / Wp
    filtered_image[x][y] = int(round(i_filtered))


def bilateral_filter_own(source, filter_diameter, sigma_i, sigma_s):
    filtered_image = np.zeros(source.shape)

    i = 0
    while i < len(source):
        j = 0
        while j < len(source[0]):
            apply_bilateral_filter(source, filtered_image, i, j, filter_diameter, sigma_i, sigma_s)
            j += 1
        i += 1
    return filtered_image


if __name__ == "__main__":
    src=cv2.imread(str(sys.argv[1]), 0)
    filtered_image_own = bilateral_filter_own(src, 5, 12.0, 16.0)
    cv2.imwrite("filtered_image_own.png", filtered_image_own)

0 Kudos
Message 4 of 26
(2,113 Views)
Solution
Accepted by topic author gptshubham595

finally solved this

Thank you @Raven, @GerdW, and all community members 

 

many times I was unclear with my posts coz I was breaking parts of this in a simpler form, I failed in some attempts to explain clearly, after learning from various other docs and with community help! Successfully got this!

 

Thank You!

will improve me! 😇

 

 

 

gptshubham595_0-1611303928681.png

 

0 Kudos
Message 5 of 26
(2,082 Views)

Hi gpt,

 


@gptshubham595 wrote:

finally solved this

Successfully got this!


Well, are you sure you need those two innermost loops?

Why are there so many coercion dots, especially on your own subVIs?

Still need to cleanup the block diagram! (Like the hidden wire from outermost loop "i" terminal to tunnel of next loop…)

Use better labels for controls/indicators. Using "integer" for an U32 indicator seems senseless…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 6 of 26
(2,077 Views)

There are a total of 4 loops in python so yes

I am using double+int so I think it is showing red coercion dots

Sure, will try to clean up and make it look better in term of displaying some hidden wires

For labels it was a test for some reason so text not mattered much as of now renamed it btw!

 

Thank You!

0 Kudos
Message 7 of 26
(2,062 Views)

Can you attach the code for it?

 

Thanks

Rahul

0 Kudos
Message 8 of 26
(2,041 Views)

...

0 Kudos
Message 9 of 26
(1,986 Views)

Hey Rahul you can use this directly to do this using above python code and python integration toolbox via Enthought 

img.jpeg

0 Kudos
Message 10 of 26
(1,966 Views)