How to do Histogram Equalization on specific area

2024/11/15 20:03:54

I have a image and I want to do HE or CLAHE on specific area of the image. I already have a mask for the image. Is there any possible way to do so?

Answer

Here is the code to achieve that :

import cv2 as cv
import numpy as np# Load your color  image
#src = cv.imread("___YourImagePath__.jpg", 
#cv.IMREAD_COLOR)#Create random color image
src = np.random.randint(255, size=(800,800,3),dtype=np.uint8)
cv.imshow('Random Color Image',src)
cv.waitKey(0)# conver to gray
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)# process gray image
equalized = cv.equalizeHist(gray)# create a mask (binary image with same size as source image )
height,width,depth = src.shape
mask = np.zeros((height,width))
cv.circle(mask,( int(width/2),int(height/2)),int(width/3),1,thickness=-1)# display mask
cv.imshow('Mask',mask)
cv.waitKey(0)# Copy processed region using the mask
ProcessedRegion = np.where(mask!=0,equalized,gray)#display result
cv.imshow('Processed region result', ProcessedRegion)
cv.waitKey(0)

Output :

enter image description here

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

Related Q&A

Timing out a multiprocessing function

I need to set a time limit on a python function which use some multiprocessing stuff (I dont know if it matters). Something like this:function(a_list):p1 = Process(a_list[0:len(a_list/2)])p2 = Process(…

How can I get the actual axis limits when using ax.axis(equal)?

I am using ax.axes(equal) to make the axis spacing equal on X and Y, and also setting xlim and ylim. This over-constrains the problem and the actual limits are not what I set in ax.set_xlim() or ax.set…

Python rarfile package: fail to open files

So I was trying to archive a .rar file using rarfile library in Python, but it keeps saying "failed to open". Am using Mac OS X El Capitan, python 2.7. Any help would be appreciated, thanks.O…

Sublime Text 3 Python Interactive Console? [duplicate]

This question already has answers here:Cant send input to running program in Sublime Text(5 answers)Closed 7 years ago.I have been using a lot of sublime text 3 to write python. However, whenever a pro…

Is there any value to a Switch / Case implementation in Python?

Recently, I saw some discussions online about how there is no good "switch / case" equivalent in Python. I realize that there are several ways to do something similar - some with lambda, som…

Replacing the existing MainWindow with a new window with Python, PyQt, Qt Designer

Im new to Python GUI programming Im have trouble making a GUI app. I have a main window with only a button widget on it. What i want to know is how to replace the existing window with a new window when…

Using LaTeX Beamer to display code

Im using the following LaTeX code in a Beamer presentation:\begin{frame}\begin{figure}\centering\tiny\lstset{language=python}\lstinputlisting{code/get_extent.py}\end{figure} \end{frame}Is it possible t…

Python metaclass and the object base class

After reading the excellent SO post, I tried crafting a module level metaclass:def metaclass(future_class_name, future_class_parents, future_class_attrs):print "module.__metaclass__"future_cl…

Is this the equivalent of a copy constructor in Python?

Im reviewing some old python code and came accross this pattern frequently:class Foo(object):def __init__(self, other = None):if other:self.__dict__ = dict(other.__dict__)Is this how a copy constructor…

Fabric error No handlers could be found for logger paramiko.transport

Im not sure why Im getting this error thats terminating my connection. I updated paramiko-1.7.6 from 1.7.5 via easy_install.Im trying to setup Fabric to upload my Django app to my server. The error see…