How to blur the image according to segmentation mapHow to blur/ feather the edges of an object in an image using Opencv?How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory in Python?How can I make a time delay in Python?How do I sort a dictionary by value?How to make a chain of function decorators?How to make a flat list out of list of lists?How do I list all files of a directory?Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition

Some numbers are more equivalent than others

Freedom of speech and where it applies

Journal losing indexing services

Do Legal Documents Require Signing In Standard Pen Colors?

Have I saved too much for retirement so far?

Proving a function is onto where f(x)=|x|.

Query about absorption line spectra

How must one send away the mother bird?

What linear sensor for a keyboard?

Is there a conventional notation or name for the slip angle?

Open a doc from terminal, but not by its name

Greco-Roman egalitarianism

List of people who lose a child in תנ"ך

Why do IPv6 unique local addresses have to have a /48 prefix?

Can a significant change in incentives void an employment contract?

How do ground effect vehicles perform turns?

What (else) happened July 1st 1858 in London?

Is it improper etiquette to ask your opponent what his/her rating is before the game?

Can someone explain how this makes sense electrically?

Can we have a perfect cadence in a minor key?

Difference between -| and |- in TikZ

Why did the EU agree to delay the Brexit deadline?

Proof of Lemma: Every nonzero integer can be written as a product of primes

Why did the HMS Bounty go back to a time when whales are already rare?



How to blur the image according to segmentation map


How to blur/ feather the edges of an object in an image using Opencv?How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory in Python?How can I make a time delay in Python?How do I sort a dictionary by value?How to make a chain of function decorators?How to make a flat list out of list of lists?How do I list all files of a directory?Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition













1















Forgive me if I am unable to explain well because I am not native speaker.



I am working on blurring the part of image according to the white part of segmentation map. For example here is my segmentation image ( bmp image ).
segmented image.



Now what I want is to blur the part of original image where the pixels are white in the segmentation map. I just wrote the following code to so.



mask = mask >= 0.5
mask = np.reshape(mask, (512, 512))

mh, mw = 512, 512
mask_n = np.ones((mh, mw, 3))

mask_n[:,:,0] *= mask
mask_n[:,:,1] *= mask
mask_n[:,:,2] *= mask

# discard padded area
ih, iw, _ = image_n.shape

delta_h = mh - ih
delta_w = mw - iw

top = delta_h // 2
bottom = mh - (delta_h - top)
left = delta_w // 2
right = mw - (delta_w - left)

mask_n = mask_n[top:bottom, left:right, :]


# addWeighted
image_n = image_n *1 + cv2.blur(mask_n * 0.8, (800, 800))


Please help me, Thanks.










share|improve this question






















  • what is the question, exactly?

    – Shai
    Mar 7 at 12:26











  • How to blur the region in image which is white in Segmented image.

    – Muhammad Rafique
    Mar 7 at 13:42















1















Forgive me if I am unable to explain well because I am not native speaker.



I am working on blurring the part of image according to the white part of segmentation map. For example here is my segmentation image ( bmp image ).
segmented image.



Now what I want is to blur the part of original image where the pixels are white in the segmentation map. I just wrote the following code to so.



mask = mask >= 0.5
mask = np.reshape(mask, (512, 512))

mh, mw = 512, 512
mask_n = np.ones((mh, mw, 3))

mask_n[:,:,0] *= mask
mask_n[:,:,1] *= mask
mask_n[:,:,2] *= mask

# discard padded area
ih, iw, _ = image_n.shape

delta_h = mh - ih
delta_w = mw - iw

top = delta_h // 2
bottom = mh - (delta_h - top)
left = delta_w // 2
right = mw - (delta_w - left)

mask_n = mask_n[top:bottom, left:right, :]


# addWeighted
image_n = image_n *1 + cv2.blur(mask_n * 0.8, (800, 800))


Please help me, Thanks.










share|improve this question






















  • what is the question, exactly?

    – Shai
    Mar 7 at 12:26











  • How to blur the region in image which is white in Segmented image.

    – Muhammad Rafique
    Mar 7 at 13:42













1












1








1








Forgive me if I am unable to explain well because I am not native speaker.



I am working on blurring the part of image according to the white part of segmentation map. For example here is my segmentation image ( bmp image ).
segmented image.



Now what I want is to blur the part of original image where the pixels are white in the segmentation map. I just wrote the following code to so.



mask = mask >= 0.5
mask = np.reshape(mask, (512, 512))

mh, mw = 512, 512
mask_n = np.ones((mh, mw, 3))

mask_n[:,:,0] *= mask
mask_n[:,:,1] *= mask
mask_n[:,:,2] *= mask

# discard padded area
ih, iw, _ = image_n.shape

delta_h = mh - ih
delta_w = mw - iw

top = delta_h // 2
bottom = mh - (delta_h - top)
left = delta_w // 2
right = mw - (delta_w - left)

mask_n = mask_n[top:bottom, left:right, :]


# addWeighted
image_n = image_n *1 + cv2.blur(mask_n * 0.8, (800, 800))


Please help me, Thanks.










share|improve this question














Forgive me if I am unable to explain well because I am not native speaker.



I am working on blurring the part of image according to the white part of segmentation map. For example here is my segmentation image ( bmp image ).
segmented image.



Now what I want is to blur the part of original image where the pixels are white in the segmentation map. I just wrote the following code to so.



mask = mask >= 0.5
mask = np.reshape(mask, (512, 512))

mh, mw = 512, 512
mask_n = np.ones((mh, mw, 3))

mask_n[:,:,0] *= mask
mask_n[:,:,1] *= mask
mask_n[:,:,2] *= mask

# discard padded area
ih, iw, _ = image_n.shape

delta_h = mh - ih
delta_w = mw - iw

top = delta_h // 2
bottom = mh - (delta_h - top)
left = delta_w // 2
right = mw - (delta_w - left)

mask_n = mask_n[top:bottom, left:right, :]


# addWeighted
image_n = image_n *1 + cv2.blur(mask_n * 0.8, (800, 800))


Please help me, Thanks.







python opencv blur image-segmentation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 7 at 8:58









Muhammad RafiqueMuhammad Rafique

177




177












  • what is the question, exactly?

    – Shai
    Mar 7 at 12:26











  • How to blur the region in image which is white in Segmented image.

    – Muhammad Rafique
    Mar 7 at 13:42

















  • what is the question, exactly?

    – Shai
    Mar 7 at 12:26











  • How to blur the region in image which is white in Segmented image.

    – Muhammad Rafique
    Mar 7 at 13:42
















what is the question, exactly?

– Shai
Mar 7 at 12:26





what is the question, exactly?

– Shai
Mar 7 at 12:26













How to blur the region in image which is white in Segmented image.

– Muhammad Rafique
Mar 7 at 13:42





How to blur the region in image which is white in Segmented image.

– Muhammad Rafique
Mar 7 at 13:42












2 Answers
2






active

oldest

votes


















1














You can do it in the following steps:



  1. Load original image and mask image.

  2. Blur the whole original image and save it in a different variable.

  3. Use np.where() method to select the pixels from the mask where you want blurred values and then replace it.

See the sample code below:



import cv2
import numpy as np

img = cv2.imread("./image.png")
blurred_img = cv2.GaussianBlur(img, (21, 21), 0)
mask = cv2.imread("./mask.png")

output = np.where(mask==np.array([255, 255, 255]), blurred_img, img)
cv2.imwrite("./output.png", output)





share|improve this answer




















  • 1





    Shouldn't it be output = np.where(mask==np.array([255, 255, 255]), blurred_img, img)?

    – Tonechas
    Mar 7 at 16:53











  • @Tonechas Edited it. Thanks for pointing it out.

    – Chris Henry
    Mar 7 at 16:59











  • Thank you very much @ChrisHenry you saved me a lot of time. Thanks

    – Muhammad Rafique
    Mar 7 at 17:00



















0














Here's an alternative to the solution proposed by @Chris Henri. It relies on scipy.ndimage.filters.gaussian_filter and NumPy's boolean indexing:



from skimage import io
import numpy as np
from scipy.ndimage.filters import gaussian_filter
import matplotlib.pyplot as plt

mask = io.imread('https://i.stack.imgur.com/qJiKf.png')
img = np.random.random(size=mask.shape[:2])
idx = mask.min(axis=-1) == 255
blurred = gaussian_filter(img, sigma=3)
blurred[~idx] = 0

fig, axs = plt.subplots(1, 3, figsize=(12, 4))
for ax, im in zip(axs, [img, mask, blurred]):
ax.imshow(im, cmap='gray')
ax.set_axis_off()
plt.show(fig)


plots






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55039717%2fhow-to-blur-the-image-according-to-segmentation-map%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can do it in the following steps:



    1. Load original image and mask image.

    2. Blur the whole original image and save it in a different variable.

    3. Use np.where() method to select the pixels from the mask where you want blurred values and then replace it.

    See the sample code below:



    import cv2
    import numpy as np

    img = cv2.imread("./image.png")
    blurred_img = cv2.GaussianBlur(img, (21, 21), 0)
    mask = cv2.imread("./mask.png")

    output = np.where(mask==np.array([255, 255, 255]), blurred_img, img)
    cv2.imwrite("./output.png", output)





    share|improve this answer




















    • 1





      Shouldn't it be output = np.where(mask==np.array([255, 255, 255]), blurred_img, img)?

      – Tonechas
      Mar 7 at 16:53











    • @Tonechas Edited it. Thanks for pointing it out.

      – Chris Henry
      Mar 7 at 16:59











    • Thank you very much @ChrisHenry you saved me a lot of time. Thanks

      – Muhammad Rafique
      Mar 7 at 17:00
















    1














    You can do it in the following steps:



    1. Load original image and mask image.

    2. Blur the whole original image and save it in a different variable.

    3. Use np.where() method to select the pixels from the mask where you want blurred values and then replace it.

    See the sample code below:



    import cv2
    import numpy as np

    img = cv2.imread("./image.png")
    blurred_img = cv2.GaussianBlur(img, (21, 21), 0)
    mask = cv2.imread("./mask.png")

    output = np.where(mask==np.array([255, 255, 255]), blurred_img, img)
    cv2.imwrite("./output.png", output)





    share|improve this answer




















    • 1





      Shouldn't it be output = np.where(mask==np.array([255, 255, 255]), blurred_img, img)?

      – Tonechas
      Mar 7 at 16:53











    • @Tonechas Edited it. Thanks for pointing it out.

      – Chris Henry
      Mar 7 at 16:59











    • Thank you very much @ChrisHenry you saved me a lot of time. Thanks

      – Muhammad Rafique
      Mar 7 at 17:00














    1












    1








    1







    You can do it in the following steps:



    1. Load original image and mask image.

    2. Blur the whole original image and save it in a different variable.

    3. Use np.where() method to select the pixels from the mask where you want blurred values and then replace it.

    See the sample code below:



    import cv2
    import numpy as np

    img = cv2.imread("./image.png")
    blurred_img = cv2.GaussianBlur(img, (21, 21), 0)
    mask = cv2.imread("./mask.png")

    output = np.where(mask==np.array([255, 255, 255]), blurred_img, img)
    cv2.imwrite("./output.png", output)





    share|improve this answer















    You can do it in the following steps:



    1. Load original image and mask image.

    2. Blur the whole original image and save it in a different variable.

    3. Use np.where() method to select the pixels from the mask where you want blurred values and then replace it.

    See the sample code below:



    import cv2
    import numpy as np

    img = cv2.imread("./image.png")
    blurred_img = cv2.GaussianBlur(img, (21, 21), 0)
    mask = cv2.imread("./mask.png")

    output = np.where(mask==np.array([255, 255, 255]), blurred_img, img)
    cv2.imwrite("./output.png", output)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 8 at 4:54

























    answered Mar 7 at 15:43









    Chris HenryChris Henry

    1016




    1016







    • 1





      Shouldn't it be output = np.where(mask==np.array([255, 255, 255]), blurred_img, img)?

      – Tonechas
      Mar 7 at 16:53











    • @Tonechas Edited it. Thanks for pointing it out.

      – Chris Henry
      Mar 7 at 16:59











    • Thank you very much @ChrisHenry you saved me a lot of time. Thanks

      – Muhammad Rafique
      Mar 7 at 17:00













    • 1





      Shouldn't it be output = np.where(mask==np.array([255, 255, 255]), blurred_img, img)?

      – Tonechas
      Mar 7 at 16:53











    • @Tonechas Edited it. Thanks for pointing it out.

      – Chris Henry
      Mar 7 at 16:59











    • Thank you very much @ChrisHenry you saved me a lot of time. Thanks

      – Muhammad Rafique
      Mar 7 at 17:00








    1




    1





    Shouldn't it be output = np.where(mask==np.array([255, 255, 255]), blurred_img, img)?

    – Tonechas
    Mar 7 at 16:53





    Shouldn't it be output = np.where(mask==np.array([255, 255, 255]), blurred_img, img)?

    – Tonechas
    Mar 7 at 16:53













    @Tonechas Edited it. Thanks for pointing it out.

    – Chris Henry
    Mar 7 at 16:59





    @Tonechas Edited it. Thanks for pointing it out.

    – Chris Henry
    Mar 7 at 16:59













    Thank you very much @ChrisHenry you saved me a lot of time. Thanks

    – Muhammad Rafique
    Mar 7 at 17:00






    Thank you very much @ChrisHenry you saved me a lot of time. Thanks

    – Muhammad Rafique
    Mar 7 at 17:00














    0














    Here's an alternative to the solution proposed by @Chris Henri. It relies on scipy.ndimage.filters.gaussian_filter and NumPy's boolean indexing:



    from skimage import io
    import numpy as np
    from scipy.ndimage.filters import gaussian_filter
    import matplotlib.pyplot as plt

    mask = io.imread('https://i.stack.imgur.com/qJiKf.png')
    img = np.random.random(size=mask.shape[:2])
    idx = mask.min(axis=-1) == 255
    blurred = gaussian_filter(img, sigma=3)
    blurred[~idx] = 0

    fig, axs = plt.subplots(1, 3, figsize=(12, 4))
    for ax, im in zip(axs, [img, mask, blurred]):
    ax.imshow(im, cmap='gray')
    ax.set_axis_off()
    plt.show(fig)


    plots






    share|improve this answer



























      0














      Here's an alternative to the solution proposed by @Chris Henri. It relies on scipy.ndimage.filters.gaussian_filter and NumPy's boolean indexing:



      from skimage import io
      import numpy as np
      from scipy.ndimage.filters import gaussian_filter
      import matplotlib.pyplot as plt

      mask = io.imread('https://i.stack.imgur.com/qJiKf.png')
      img = np.random.random(size=mask.shape[:2])
      idx = mask.min(axis=-1) == 255
      blurred = gaussian_filter(img, sigma=3)
      blurred[~idx] = 0

      fig, axs = plt.subplots(1, 3, figsize=(12, 4))
      for ax, im in zip(axs, [img, mask, blurred]):
      ax.imshow(im, cmap='gray')
      ax.set_axis_off()
      plt.show(fig)


      plots






      share|improve this answer

























        0












        0








        0







        Here's an alternative to the solution proposed by @Chris Henri. It relies on scipy.ndimage.filters.gaussian_filter and NumPy's boolean indexing:



        from skimage import io
        import numpy as np
        from scipy.ndimage.filters import gaussian_filter
        import matplotlib.pyplot as plt

        mask = io.imread('https://i.stack.imgur.com/qJiKf.png')
        img = np.random.random(size=mask.shape[:2])
        idx = mask.min(axis=-1) == 255
        blurred = gaussian_filter(img, sigma=3)
        blurred[~idx] = 0

        fig, axs = plt.subplots(1, 3, figsize=(12, 4))
        for ax, im in zip(axs, [img, mask, blurred]):
        ax.imshow(im, cmap='gray')
        ax.set_axis_off()
        plt.show(fig)


        plots






        share|improve this answer













        Here's an alternative to the solution proposed by @Chris Henri. It relies on scipy.ndimage.filters.gaussian_filter and NumPy's boolean indexing:



        from skimage import io
        import numpy as np
        from scipy.ndimage.filters import gaussian_filter
        import matplotlib.pyplot as plt

        mask = io.imread('https://i.stack.imgur.com/qJiKf.png')
        img = np.random.random(size=mask.shape[:2])
        idx = mask.min(axis=-1) == 255
        blurred = gaussian_filter(img, sigma=3)
        blurred[~idx] = 0

        fig, axs = plt.subplots(1, 3, figsize=(12, 4))
        for ax, im in zip(axs, [img, mask, blurred]):
        ax.imshow(im, cmap='gray')
        ax.set_axis_off()
        plt.show(fig)


        plots







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 7 at 17:40









        TonechasTonechas

        7,14392253




        7,14392253



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55039717%2fhow-to-blur-the-image-according-to-segmentation-map%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

            Алба-Юлія

            Захаров Федір Захарович