0

I have a segmented mask of vessel and original image. Trying to use gaussian function to erode out that region into a lower intensity. I read the image and mask segment (black/white) then pass it over to erode_vasc; there I just average it out. Averaging is leaving a streak when I convert to gray. If I can get that into a lowered intensity so that it blends e.g. if I can convolve that region of lines to lower intensity is what I am after.

I took 2 images - original and segmented imaged and called this program. Issue is the line while in RGB is fine - it is hardly visible but once I push this to other image conversions, it starts to be visible.

import os
import os.path as osp
import cv2
import sys
import numpy as np
from PIL import Image
from skimage import io, img_as_float
from skimage.io import imshow
from scipy import signal
from skimage.color import rgb2yuv, rgb2hsv, rgb2gray, yuv2rgb, hsv2rgb
from skimage.transform import resize
import math
from tqdm import tqdm
import matplotlib.pyplot as plt
import time

def avg_vessel(boxarea_v, channel_v, mask_v):
    boxarea = boxarea_v.flatten()
    channel = channel_v.flatten()
    mask = mask_v.flatten()
    
    # selecting all pixels with mask value greater than 0.1
    AOI = (mask>0.1)
    
    retval = np.zeros(boxarea.shape)
    # assign local average to selected pixels
    retval[AOI] = boxarea[AOI]
    # keep remaining ones same as original
    retval[~AOI] = channel[~AOI]
    return retval.reshape(channel_v.shape)

# removes vessel from each channel
def erode_vasc(channel,mask):
    bordertype = 'fill'

    blursqr = (signal.convolve2d(channel,np.ones((8,8)),boundary=bordertype,mode='same'))/64
-->    blursqr = np.ones(blursqr,float32)/64 <-- using this for average
    avg_channel = np.zeros(channel.shape)
    avg_channel = avg_vessel(blursqr, channel, mask)
    channel = avg_channel

# given an image and mask, calls remove_vessel on each channel of image
def erode_image(orig_im, mask):

# Erode vasculature on red channel
    chr = erode_vasc(orig_im[:,:,0],mask)
# Erode vasculature on blue channel
    chg = erode_vasc(orig_im[:,:,1],mask)
# Erode vasculature on green channel
    chb = erode_vasc(orig_im[:,:,2],mask)
    eroded_image =  np.dstack((np.rint(abs(chr)), 
                   np.rint(abs(chg)), 
                   np.rint(abs(chb)))) /255

    plt.figure(num=None, figsize=(8,6), dpi = 80)
    plt.imshow(eroded_image)

How can one replace the average valuation with a gaussian kernel? How to calculate a Gaussian kernel matrix efficiently in numpy? is not complete.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Zulu112
  • 19
  • 3
  • What is your question about this? – mkrieger1 Aug 04 '23 at 21:33
  • I am able to get the average to work; instead I want to employ a guassian function instead of averaging out. – Zulu112 Aug 04 '23 at 21:37
  • And your question is how a gaussian function is defined? – mkrieger1 Aug 04 '23 at 21:38
  • yes how i can subsitute it : here specifically: blursqr = (signal.convolve2d(channel,np.ones((8,8)),boundary=bordertype,mode='same'))/64 # blur_area = np.ones(blur_area,float32)/64 <--- i was using this before - needing to now make it using a gaussian function avg_channel = np.zeros(channel.shape) avg_channel = avg_vessel(blur_area, channel, mask) channel = avg_channel – Zulu112 Aug 04 '23 at 21:47
  • blur_area = np.ones(blur_area,float32)/64 – Zulu112 Aug 04 '23 at 21:49
  • Yes, replace a box-shaped/uniform kernel by a gaussian kernel. – mkrieger1 Aug 04 '23 at 21:50
  • yes, correct with gaussian kernel – Zulu112 Aug 04 '23 at 21:52
  • how is this a duplicate question? the problem set is very different in terms of application. I take it back. I see the solution. import numpy as np import scipy.stats as st def gkern(kernlen=21, nsig=3): """Returns a 2D Gaussian kernel.""" x = np.linspace(-nsig, nsig, kernlen+1) kern1d = np.diff(st.norm.cdf(x)) kern2d = np.outer(kern1d, kern1d) return kern2d/kern2d.sum() – Zulu112 Aug 04 '23 at 22:36
  • Let me try it out. if it works, it is great news. a huge contribution – Zulu112 Aug 04 '23 at 22:39
  • challenge... not too familiar with np.. blursqr = np.ones(blursqr,float32)/64. If i have to replace it with above. where would i be changing? – Zulu112 Aug 04 '23 at 22:44
  • Replace `np.ones(...)` with `gkern(...)` (providing suitable arguments, I don't know which are suitable). – mkrieger1 Aug 04 '23 at 22:47
  • yes that is the challenge - getting the boxsqr from convolve is there. now it is 8x8patch. how to pass it is the question e.g. to auto cal the values gkern is asking for. kernlen will be 8 so we are good there only part is what is sig. – Zulu112 Aug 04 '23 at 22:53
  • it is not going to work - what i am looking for to use the convolved output to a guassian kernel. the link appears to be in right direction but it may not be the right solution. – Zulu112 Aug 04 '23 at 23:02
  • https://stackoverflow.com/questions/28351490/how-to-compute-the-mean-value-of-all-sub-imges-in-image-i/28351684#28351684 this appears to be closer to what i am seeking but it is in matlab – Zulu112 Aug 04 '23 at 23:06

0 Answers0