本文整理汇总了Python中cv2.createCLAHE方法的典型用法代码示例。如果您正苦于以下问题:Python cv2.createCLAHE方法的具体用法?Python cv2.createCLAHE怎么用?Python cv2.createCLAHE使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cv2
的用法示例。
在下文中一共展示了cv2.createCLAHE方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: prepare_cropped_sax_image
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import createCLAHE [as 别名]
def prepare_cropped_sax_image(sax_image, clahe=True, intermediate_crop=0, rotate=0):
if rotate != 0:
rot_mat = cv2.getRotationMatrix2D((sax_image.shape[0] / 2, sax_image.shape[0] / 2), rotate, 1)
sax_image = cv2.warpAffine(sax_image, rot_mat, (sax_image.shape[0], sax_image.shape[1]))
if intermediate_crop == 0:
res = sax_image[settings.CROP_INDENT_Y:settings.CROP_INDENT_Y + settings.TARGET_CROP, settings.CROP_INDENT_X:settings.CROP_INDENT_X + settings.TARGET_CROP]
else:
crop_indent_y = settings.CROP_INDENT_Y - ((intermediate_crop - settings.TARGET_CROP) / 2)
crop_indent_x = settings.CROP_INDENT_X - ((intermediate_crop - settings.TARGET_CROP) / 2)
res = sax_image[crop_indent_y:crop_indent_y + intermediate_crop, crop_indent_x:crop_indent_x + intermediate_crop]
res = cv2.resize(res, (settings.TARGET_CROP, settings.TARGET_CROP))
if clahe:
clahe = cv2.createCLAHE(tileGridSize=(1, 1))
res = clahe.apply(res)
return res
示例2: _random_clahe
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import createCLAHE [as 别名]
def _random_clahe(self, batch):
""" Randomly perform Contrast Limited Adaptive Histogram Equalization on
a batch of images """
base_contrast = self._constants["clahe_base_contrast"]
batch_random = np.random.rand(self._batchsize)
indices = np.where(batch_random > self._config.get("color_clahe_chance", 50) / 100)[0]
grid_bases = np.rint(np.random.uniform(0,
self._config.get("color_clahe_max_size", 4),
size=indices.shape[0])).astype("uint8")
contrast_adjustment = (grid_bases * (base_contrast // 2))
grid_sizes = contrast_adjustment + base_contrast
logger.trace("Adjusting Contrast. Grid Sizes: %s", grid_sizes)
clahes = [cv2.createCLAHE(clipLimit=2.0, # pylint: disable=no-member
tileGridSize=(grid_size, grid_size))
for grid_size in grid_sizes]
for idx, clahe in zip(indices, clahes):
batch[idx, :, :, 0] = clahe.apply(batch[idx, :, :, 0])
return batch
示例3: equalize_light
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import createCLAHE [as 别名]
def equalize_light(image, limit=3, grid=(7,7), gray=False):
if (len(image.shape) == 2):
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
gray = True
clahe = cv2.createCLAHE(clipLimit=limit, tileGridSize=grid)
lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
l, a, b = cv2.split(lab)
cl = clahe.apply(l)
limg = cv2.merge((cl,a,b))
image = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
if gray:
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
return np.uint8(image)
示例4: equalize_clahe_color
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import createCLAHE [as 别名]
def equalize_clahe_color(img):
"""Equalize the image splitting the image applying CLAHE to each channel
and merging the results
"""
cla = cv2.createCLAHE(clipLimit=4.0)
channels = cv2.split(img)
eq_channels = []
for ch in channels:
eq_channels.append(cla.apply(ch))
eq_image = cv2.merge(eq_channels)
return eq_image
# Create the dimensions of the figure and set title:
开发者ID:PacktPublishing,项目名称:Mastering-OpenCV-4-with-Python,代码行数:18,代码来源:clahe_histogram_equalization.py
示例5: resize_and_contrast
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import createCLAHE [as 别名]
def resize_and_contrast(in_dir, out_dir, target_size):
check_and_mkdir(out_dir)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
for subdir, dirs, files in os.walk(in_dir):
for f in files:
file_path = subdir + os.sep + f
if (is_image(f)):
img = cv2.imread(file_path, 0)
resized_img = cv2.resize(img, (target_size, target_size), interpolation = cv2.INTER_CUBIC)
class_dir = out_dir + os.sep + file_path.split("/")[-2]
check_and_mkdir(class_dir)
file_name = class_dir + os.sep + file_path.split("/")[-1]
print(file_name)
norm_image = cv2.normalize(resized_img, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F) * 256
# norm_image = clahe.apply(resized_img)
cv2.imwrite(file_name, norm_image)
# count the direct one-step sub directories (which will represent the class name)
示例6: main
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import createCLAHE [as 别名]
def main():
# read an image
img = cv2.imread('../figures/_DSC2126.jpg')
img = cv2.resize(img, (600,400))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# hist,bins = np.histogram(img[100:400, 100:400].flatten(),256,[0,256])
# cdf = hist.cumsum()
# cdf_normalized = cdf * hist.max()/ cdf.max()
# # plot hist normalized
# plot_hist_cdf(cdf_normalized, img[100:400, 100:400])
equ = cv2.equalizeHist(gray)
# create a CLAHE object (Arguments are optional).
clahe = cv2.createCLAHE()
cl1 = clahe.apply(gray)
plot_gray(gray, equ, cl1)
示例7: FindWand
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import createCLAHE [as 别名]
def FindWand():
global rval,old_frame,old_gray,p0,mask,color,ig,img,frame
try:
rval, old_frame = cam.read()
cv2.flip(old_frame,1,old_frame)
old_gray = cv2.cvtColor(old_frame,cv2.COLOR_BGR2GRAY)
equalizeHist(old_gray)
old_gray = GaussianBlur(old_gray,(9,9),1.5)
dilate_kernel = np.ones(dilation_params, np.uint8)
old_gray = cv2.dilate(old_gray, dilate_kernel, iterations=1)
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
old_gray = clahe.apply(old_gray)
#TODO: trained image recognition
p0 = cv2.HoughCircles(old_gray,cv2.HOUGH_GRADIENT,3,50,param1=240,param2=8,minRadius=4,maxRadius=15)
if p0 is not None:
p0.shape = (p0.shape[1], 1, p0.shape[2])
p0 = p0[:,:,0:2]
mask = np.zeros_like(old_frame)
ig = [[0] for x in range(20)]
print "finding..."
threading.Timer(3, FindWand).start()
except:
e = sys.exc_info()[1]
print "Error: %s" % e
exit
示例8: load_rgb
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import createCLAHE [as 别名]
def load_rgb(self, equalize=False):
# print("Loading:", self.image_file)
try:
img_rgb = cv2.imread(self.image_file, flags=cv2.IMREAD_ANYCOLOR|cv2.IMREAD_ANYDEPTH|cv2.IMREAD_IGNORE_ORIENTATION)
if equalize:
# equalize val (essentially gray scale level)
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
hsv = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2HSV)
hue, sat, val = cv2.split(hsv)
aeq = clahe.apply(val)
# recombine
hsv = cv2.merge((hue,sat,aeq))
# convert back to rgb
img_rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
h, w = img_rgb.shape[:2]
self.node.setInt('height', h)
self.node.setInt('width', w)
return img_rgb
except:
print(self.image_file + ":\n" + " rgb load error: " \
+ str(sys.exc_info()[1]))
return None
示例9: histogram_equalization
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import createCLAHE [as 别名]
def histogram_equalization(img):
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
# -----Splitting the LAB image to different channels-------------------------
l, a, b = cv2.split(lab)
# -----Applying CLAHE to L-channel-------------------------------------------
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
cl = clahe.apply(l)
# -----Merge the CLAHE enhanced L-channel with the a and b channel-----------
limg = cv2.merge((cl, a, b))
# -----Converting image from LAB Color model to RGB model--------------------
final = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
return final
示例10: clahe
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import createCLAHE [as 别名]
def clahe(filename):
os.chdir(os.path.dirname(filename))
print('Applying CLAHE, this might take awhile...')
currentVideo = os.path.basename(filename)
fileName, fileEnding = currentVideo.split('.',2)
saveName = str('CLAHE_') + str(fileName) + str('.avi')
cap = cv2.VideoCapture(currentVideo)
imageWidth = int(cap.get(3))
imageHeight = int(cap.get(4))
fps = cap.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(saveName, fourcc, fps, (imageWidth, imageHeight), 0)
try:
while True:
ret, image = cap.read()
if ret == True:
im = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
claheFilter = cv2.createCLAHE(clipLimit=2, tileGridSize=(16, 16))
claheCorrecttedFrame = claheFilter.apply(im)
out.write(claheCorrecttedFrame)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
else:
print(str('Completed video ') + str(saveName))
break
except:
print('clahe not applied')
cap.release()
out.release()
cv2.destroyAllWindows()
return saveName
示例11: clahe_auto
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import createCLAHE [as 别名]
def clahe_auto(directory):
filesFound= []
########### FIND FILES ###########
for i in os.listdir(directory):
if i.__contains__(".mp4"):
filesFound.append(i)
os.chdir(directory)
print('Applying CLAHE, this might take awhile...')
for i in filesFound:
currentVideo = i
saveName = str('CLAHE_') + str(currentVideo[:-4]) + str('.avi')
cap = cv2.VideoCapture(currentVideo)
imageWidth = int(cap.get(3))
imageHeight = int(cap.get(4))
fps = cap.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(saveName, fourcc, fps, (imageWidth, imageHeight), 0)
while True:
ret, image = cap.read()
if ret == True:
im = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
claheFilter = cv2.createCLAHE(clipLimit=2, tileGridSize=(16, 16))
claheCorrecttedFrame = claheFilter.apply(im)
out.write(claheCorrecttedFrame)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
else:
print(str('Completed video ') + str(saveName))
break
cap.release()
out.release()
cv2.destroyAllWindows()
return saveName
示例12: clahe_batch
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import createCLAHE [as 别名]
def clahe_batch(directory):
filesFound= []
########### FIND FILES ###########
for i in os.listdir(directory):
filesFound.append(i)
os.chdir(directory)
print('Applying CLAHE, this might take awhile...')
for i in filesFound:
currentVideo = i
saveName = str('CLAHE_') + str(currentVideo[:-4]) + str('.avi')
cap = cv2.VideoCapture(currentVideo)
imageWidth = int(cap.get(3))
imageHeight = int(cap.get(4))
fps = cap.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(saveName, fourcc, fps, (imageWidth, imageHeight), 0)
while True:
ret, image = cap.read()
if ret == True:
im = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
claheFilter = cv2.createCLAHE(clipLimit=2, tileGridSize=(16, 16))
claheCorrecttedFrame = claheFilter.apply(im)
out.write(claheCorrecttedFrame)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
else:
print(str('Completed video ') + str(saveName))
break
cap.release()
out.release()
cv2.destroyAllWindows()
return saveName
示例13: clahe_queue
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import createCLAHE [as 别名]
def clahe_queue(files):
filesFound= [files]
os.chdir(os.path.dirname(files))
print('Applying CLAHE, this might take awhile...')
for i in filesFound:
currentVideo = os.path.basename(i)
saveName = str('CLAHE_') + str(currentVideo[:-4]) + str('.avi')
cap = cv2.VideoCapture(currentVideo)
imageWidth = int(cap.get(3))
imageHeight = int(cap.get(4))
fps = cap.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(saveName, fourcc, fps, (imageWidth, imageHeight), 0)
while True:
ret, image = cap.read()
if ret == True:
im = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
claheFilter = cv2.createCLAHE(clipLimit=2, tileGridSize=(16, 16))
claheCorrecttedFrame = claheFilter.apply(im)
out.write(claheCorrecttedFrame)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
else:
print(str('Completed video ') + str(saveName))
break
cap.release()
out.release()
cv2.destroyAllWindows()
return saveName
示例14: __apply_multi_threshold
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import createCLAHE [as 别名]
def __apply_multi_threshold(self, src):
"""Apply multi thresholding using LAB, HLS and HSV.
Args:
src (int): Input image BGR.
numpy.ndarray, (720, 1280, 3), 0~255
Returns:
dst (int): Output image.
numpy.ndarray, (720, 1280), 0~1
"""
settings = []
settings.append({'cspace': 'LAB', 'channel': 2, 'clipLimit': 2.0, 'threshold': 190})
settings.append({'cspace': 'HLS', 'channel': 1, 'clipLimit': 1.0, 'threshold': 200})
settings.append({'cspace': 'HSV', 'channel': 2, 'clipLimit': 3.0, 'threshold': 230})
gray = cv2.cvtColor(src, cv2.COLOR_RGB2GRAY)
dst = np.zeros_like(gray)
for s in settings:
color_t = getattr(cv2, 'COLOR_RGB2{}'.format(s['cspace']))
gray = cv2.cvtColor(src, color_t)[:,:,s['channel']]
clahe = cv2.createCLAHE(s['clipLimit'], tileGridSize=(8,8))
norm_img = clahe.apply(gray)
binary = np.zeros_like(norm_img)
binary[(norm_img >= s['threshold']) & (norm_img <= 255)] = 1
dst[(dst == 1) | (binary == 1)] = 1
return dst
示例15: histogram_equalization
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import createCLAHE [as 别名]
def histogram_equalization(images, adaptive=True):
_images = np.array(images * 255, dtype = np.uint8)
pool = ThreadPool(4)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
def process_image(image):
#print image.shape, image.dtype
image = image.transpose(1,2,0)
if adaptive:
image[:,:,0] = clahe.apply(image[:,:,0])
image[:,:,1] = clahe.apply(image[:,:,1])
image[:,:,2] = clahe.apply(image[:,:,2])
else:
image[:,:,0] = cv2.equalizeHist(image[:,:,0])
image[:,:,1] = cv2.equalizeHist(image[:,:,1])
image[:,:,2] = cv2.equalizeHist(image[:,:,2])
image = image.transpose(2,0,1)
return image
equalized = pool.map(process_image, _images)
equalized = np.array(equalized, dtype=np.float32)/255.
#visualize_data(np.append(images[:8],equalized[:8],axis=0).transpose(0,2,3,1))
return equalized