How to create a zoned of gradation area on the edge of ROI in opencv python

2024/11/15 1:09:04

I have a binary image (white and black), the where Region of Interest (ROI) is black. The shape of ROI is irregular and the location of ROI can be anywhere in the frame.

I want to have a smooth gradation at the edge of the ROI (black -> dark grey -> grey -> light grey -> white).

I research the links below, however those cannot solve my problem:

  1. How to blur some portion of Image in Android?
  2. OpenCV - How to achieve a gradual image blur effect?
  3. Combining 2 images with transparent mask in opencv
  4. Gradient mask blending in opencv python

Example of ROI - original image, the REAL Shape is irregular and Location is anywhere:

example of ROI - original image, the REAL Shape is irregular and Location is anywhere

Expected result - in the edge of ROI the is gradation part changing from stright line to dash line: black -> dark grey -> grey -> light grey -> white:

expectation image - in the edge of ROI the is gradation part changing from stright line to dash line: black -> dark grey -> grey -> light grey -> white

Answer

Here is one way to do that for an outer gradient in Python/OpenCV.

 - Read the input- Convert to grayscale- Threshold to binary- Apply a distance transform- Stretch it to full dynamic range- Stretch to limit the gradient to a short distance around the rectangle- Save the output

Input:

enter image description here

import cv2
import numpy as np
import skimage.exposure# read image
img = cv2.imread('rect_black.jpg')# convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# threshold to binary
thresh = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY)[1]# apply distance transform
distimg = cv2.distanceTransform(thresh, cv2.DIST_L2, 3)# scale distance image to full dynamic range
distimg = skimage.exposure.rescale_intensity(distimg, in_range='image', out_range=(0,255)).astype(np.uint8)# scale distance image to form limited gradient border
result = skimage.exposure.rescale_intensity(distimg, in_range=(0,25), out_range=(0,255)).astype(np.uint8)# save result
cv2.imwrite('rect_black_distance.png',distimg)
cv2.imwrite('rect_black_result.png',result)# show the images
cv2.imshow("distance", distimg)
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Stretched Distance Image:

enter image description here

Result:

enter image description here

Note: In place of the distance transform, one could just use a Gaussian blur.

https://en.xdnf.cn/q/119459.html

Related Q&A

Prevent Terminal resize python curses

Im writing a program on python curses and I was wondering if there is a way to block terminal resizing in order to prevent curses crashing both on Linux and Windows. This is what happens.. Can I preven…

SymPy Not Doesnt Return LaTeX

Helloo! So, Im using SymPy to make a calculation for me. The trouble is, its output should be a LaTeX expression and in make case it prints something like SymPy Calculation Output Is there any way to s…

Python Flask: How to include JavaScript file for each template per blueprint

I have read Loading external script with jinja2 template directive and Import javascript files with jinja from static folder but unfortunately no closer I have a Python Flask site which is based on htt…

Difference between multiple elements in list with same string . Python 2.7 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…

EDX Course API: Getting EDX course list

I am making a project in python/flask. I want to get a list of all the courses of edx. But the API provides the list page by page. I cant figure out how to get the entire list. Any help is appreciated.…

How to extract particlar message from a vast displayed output using python regular expression?

Firstly in the code, i would like to know How can i add a for loop for CH (1-11) instead of writing for every number Also how to extract SUCCESS and FAILED message from the output (reference) For examp…

Enemy health bar aint draining pygame [duplicate]

This question already has answers here:How to put a health bar over the sprite in pygame(2 answers)Closed 3 years ago.Okay so I was trying to make a health bar for my enemy class and only a part of it …

Python - Create and instantiate class

I am building a class of playlists, which will hold many playlists of the same genre.class playlist(object):def __init__(self,name):self.name = nameI would like to instantiate them passing the user:def…

Missing samples of a dataframe in pandas

My df:In [163]: df.head() Out[163]: x-axis y-axis z-axis time 2017-07-27 06:23:08 -0.107666 -0.068848 0.963623 2017-07-27 06:23:08 -0.105225 -0.070068 0.963867 .....I set the index as dateti…

How to hide a button after clicked in Python

I was wondering how to hide my start button after being clicked so that If the user accidentally was clicker happy they wouldnt hit the button causing more bubbles to appear on screen. Below is a snipp…