使图像颜色均匀代码python
时间: 2023-10-14 15:27:34 浏览: 241
实现图像颜色均匀化的代码如下:
```python
import cv2
# 读取图像
img = cv2.imread('image.jpg')
# 将图像转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 计算直方图
hist = cv2.calcHist([gray], [0], None, [256], [0, 256])
# 计算累计分布函数
cdf = hist.cumsum()
# 归一化
cdf_normalized = cdf * hist.max() / cdf.max()
# 计算均衡化后的像素值
equalized = cv2.normalize(gray, None, 0, 255, cv2.NORM_MINMAX)
# 显示原始图像和均衡化后的图像
cv2.imshow('Original', gray)
cv2.imshow('Equalized', equalized)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
这段代码首先读取图像,然后将其转换为灰度图像。接着,使用 `cv2.calcHist()` 函数计算图像的直方图,然后计算累计分布函数。接下来,将累计分布函数归一化,以便将其作为像素值的映射表。最后,使用 `cv2.normalize()` 函数将图像进行均衡化,并显示原始图像和均衡化后的图像。
相关问题
python中图像特征提取代码
### Python 图像特征提取 示例代码
#### 使用灰度共生矩阵 (GLCM)
灰度共生矩阵是一种常用的纹理分析方法,能够捕捉图像中的空间关系。
```python
from skimage.feature import greycomatrix, greycoprops
import numpy as np
import cv2
def glcm_features(image_path):
image = cv2.imread(image_path, 0) # 读取灰度图
glcm = greycomatrix(image, distances=[1], angles=[0], levels=256, symmetric=True, normed=True)
contrast = greycoprops(glcm, 'contrast')[0][0]
dissimilarity = greycoprops(glcm, 'dissimilarity')[0][0]
homogeneity = greycoprops(glcm, 'homogeneity')[0][0]
energy = greycoprops(glcm, 'energy')[0][0]
correlation = greycoprops(glcm, 'correlation')[0][0]
return {
"Contrast": contrast,
"Dissimilarity": dissimilarity,
"Homogeneity": homogeneity,
"Energy": energy,
"Correlation": correlation
}
```
此函数实现了基于灰度共生矩阵的多种纹理特性计算[^1]。
#### 颜色直方图
颜色直方图是另一种常见的图像特征表示方式,适用于色彩分布较为均匀的对象识别场景。
```python
import matplotlib.pyplot as plt
import cv2
def color_histogram(image_path):
image = cv2.imread(image_path)
colors = ('b', 'g', 'r')
hist_data = []
for i, col in enumerate(colors):
hist = cv2.calcHist([image], [i], None, [256], [0, 256])
hist_data.append(hist.flatten())
plt.plot(hist, color=col)
plt.xlim([0, 256])
plt.show()
return np.array(hist_data).flatten()
color_histogram('example.jpg') # 替换为实际图片路径
```
上述脚本绘制并返回了RGB三个通道的颜色频率曲线数据.
#### VGG网络提取高级语义特征
对于更复杂的视觉模式理解任务,则可借助预训练好的深层卷积神经网络模型如VGG来进行高层次抽象表征的学习.
```python
from keras.applications.vgg16 import VGG16, preprocess_input
from keras.preprocessing.image import img_to_array, load_img
from keras.models import Model
model = VGG16(weights='imagenet', include_top=False)
layer_name = 'block4_pool' # 可选不同层作为输出
intermediate_layer_model = Model(inputs=model.input, outputs=model.get_layer(layer_name).output)
def vgg_feature_extraction(image_path):
img = load_img(image_path, target_size=(224, 224))
x = img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
features = intermediate_layer_model.predict(x)
flattened_features = features.flatten()
return flattened_features
vgg_feature_extraction('example.jpg') # 替换为实际图片路径
```
这段程序展示了利用Keras加载预训练VGG16模型,并从中获取指定中间层激活响应的过程[^2].
灰度图像实现反差基本一致、相邻细部反差增大,颜色均匀,反差适中的算法python实现
可以使用以下代码实现:
```python
import cv2
import numpy as np
# 读取灰度图像
img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
# 计算最大灰度值和最小灰度值
min_val, max_val, _, _ = cv2.minMaxLoc(img)
# 计算灰度级别
levels = 256
# 计算增益和偏移量
gain = (levels-1) / (max_val - min_val)
bias = -min_val * gain
# 计算反差增强后的图像
img_contrast = cv2.convertScaleAbs(img, alpha=gain, beta=bias)
# 显示原始图像和反差增强后的图像
cv2.imshow('Original Image', img)
cv2.imshow('Contrast Enhanced Image', img_contrast)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在上述代码中,我们首先读取了灰度图像,并计算了最大灰度值和最小灰度值。接下来,我们计算了灰度级别,并根据最大灰度值、最小灰度值、灰度级别计算了增益和偏移量。最后,我们使用cv2.convertScaleAbs函数对图像进行反差增强,并将原始图像和反差增强后的图像显示出来。
值得注意的是,上述代码中计算的增益和偏移量是基于整个图像的最大灰度值和最小灰度值计算的。如果需要对图像的局部区域进行反差增强,可以先对图像进行分割,然后分别计算每个局部区域的最大灰度值和最小灰度值,并基于这些值计算增益和偏移量。
阅读全文
相关推荐
















