2

I am using inRange() function of OpenCV4Android to detect a colored blob in an image. Then I am using drawContours() to draw the contours around the selected object.

What I want is that when the contour is drawn, the image outside the contour (i.e. the part which is not a part of the contour or detected colored blob/object) should get a little blur.

How can I do that? I am not asking necessarily for code. If you can tell me what function(s) to use, or how to go about it, I'll look into the API's and do it. Thank you.

enter image description here

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Solace
  • 8,612
  • 22
  • 95
  • 183
  • 1
    did you try the gaussian blur? – OBX Apr 11 '16 at 02:41
  • 1
    I guess gaussian blur on a sub region would work . see this : http://stackoverflow.com/questions/24195138/gaussian-blurring-with-opencv-only-blurring-a-subregion-of-an-image – OBX Apr 11 '16 at 02:44
  • @shaheen I have seen the gaussian blur method but I don't know how to apply it to parts other than the region of interest. – Solace Apr 11 '16 at 02:47
  • @shaheen Thank you for that link. They have given `image(region)` as the first argument to the `GaussianBlur` function, where `region` is the region-of-interest (like the area inside the contour). I don't know I would give that kind of argument in OpenCV4Android :s – Solace Apr 11 '16 at 02:49
  • 1
    I guess a much easier way would be to apply the blur by using the renderscript available in Android. PS: I haven't tried it . But seems a sturdy option. https://futurestud.io/blog/how-to-blur-images-efficiently-with-androids-renderscript , http://developer.android.com/guide/topics/renderscript/index.html – OBX Apr 11 '16 at 02:54
  • @shaheen Thank you so very much indeed, but this seems more complicated, since I need to blur out the image outside the contour (i.e. _outside_ the orange ball), and not the entire image. I don't think Android has any concept of ROIs (Region Of Interest). :( – Solace Apr 11 '16 at 03:10
  • 6
    I think the solution may be to blur the complete image, and only afterwards combine the blurred and the original image using a mask which is generated by the area defined by your contour (using a function like bitwise_and). I could only give you a opencv python solution if you dfon't know how to do this. – tfv Apr 11 '16 at 03:13
  • Thank you very much. I am going to try it in Java/Android, then I'll tell you =) – Solace Apr 11 '16 at 03:30
  • @tfv do you mind helping me with the solution to this in python? i'm having the same problem. i have what i want to keep in focus stored as rectangle coordinates and I to blur everything else, but i don't know how to get stuff like bitwise_and working right. can you take a look at my gist please? https://gist.github.com/stanchiang/b4e4890160a054a9c1d65f9152172600 – stanley Jul 17 '16 at 12:53

1 Answers1

0

You can try this one it is in python:

import cv2
import numpy as np

img = cv2.imread("/home/user/Downloads/lena.png")
blurred_img = cv2.GaussianBlur(img, (21, 21), 0)

mask = np.zeros((512, 512, 3), dtype=np.uint8)
mask = cv2.circle(mask, (258, 258), 100, np.array([255, 255, 255]), -1)

out = np.where(mask==np.array([255, 255, 255]), img, blurred_img)

cv2.imwrite("./out.png", out)
Tanay
  • 561
  • 1
  • 3
  • 16