cv2.normalize(gray_img, None, 0, 255, cv2.NORM_MINMAX)
时间: 2024-06-01 16:12:22 浏览: 13
This line of code normalizes the pixel values in a grayscale image (gray_img) to a range of 0 to 255 using the minimum-maximum normalization method (cv2.NORM_MINMAX). The normalized image is not stored back to the same variable, as None is passed as the second argument.
相关问题
import cv2 from skimage.feature import hog # 加载LFW数据集 from sklearn.datasets import fetch_lfw_people lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4) # 将数据集划分为训练集和测试集 from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(lfw_people.images, lfw_people.target, test_size=0.2, random_state=42) # 图像预处理和特征提取 from skimage import exposure import numpy as np train_features = [] for i in range(X_train.shape[0]): # 将人脸图像转换为灰度图 gray_img = cv2.cvtColor(X_train[i], cv2.COLOR_BGR2GRAY) # 归一化像素值 gray_img = cv2.normalize(gray_img, None, 0, 1, cv2.NORM_MINMAX, cv2.CV_32F) # 计算HOG特征 hog_features, hog_image = hog(gray_img, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), block_norm='L2', visualize=True, transform_sqrt=False) # 将HOG特征作为样本特征 train_features.append(hog_features) train_features = np.array(train_features) train_labels = y_train test_features = [] for i in range(X_test.shape[0]): # 将人脸图像转换为灰度图 gray_img = cv2.cvtColor(X_test[i], cv2.COLOR_BGR2GRAY) # 归一化像素值 gray_img = cv2.normalize(gray_img, None, 0, 1, cv2.NORM_MINMAX, cv2.CV_32F) # 计算HOG特征 hog_features, hog_image = hog(gray_img, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), block_norm='L2', visualize=True, transform_sqrt=False) # 将HOG特征作为样本特征 test_features.append(hog_features) test_features = np.array(test_features) test_labels = y_test # 训练模型 from sklearn.naive_bayes import GaussianNB gnb = GaussianNB() gnb.fit(train_features, train_labels) # 对测试集中的人脸图像进行预测 predict_labels = gnb.predict(test_features) # 计算预测准确率 from sklearn.metrics import accuracy_score accuracy = accuracy_score(test_labels, predict_labels) print('Accuracy:', accuracy)
这段代码是在导入Python中用于图像处理和计算机视觉的两个库:cv2和skimage.feature。从skimage.feature导入了hog函数,是用于计算图像的HOG(方向梯度直方图)特征的函数。
cv2.waitKey() cv2.destroyAllWindows() fig = plt.gcf() fig.set_size_inches(8, 6) plt.savefig('scatter_plot_with_annotations.png', dpi=100) plt.clf() pixmap= QPixmap("scatter_plot_with_annotations.png") # 加载图像 img = cv2.imread('D:\shujuji\guangliang\80Lux\\000031.jpg') # 将图像转换为灰度图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 计算灰度图像的平均值 mean = np.mean(gray) # 计算灰度图像的标准差 std = np.std(gray) # 设置增强系数 k = 50 # 计算最小值和最大值 min_val = mean - k * std max_val = mean + k * std # 亮度增强 enhanced = cv2.normalize(gray, None, min_val, max_val, cv2.NORM_MINMAX) # 显示原始图像和增强后的图像 cv2.imshow('Input', img) cv2.imshow('Enhanced', enhanced) # 等待按下任意按键 cv2.waitKey(0) # 关闭所有窗口 cv2.destroyAllWindows()处理结果图怎么变得可以调整大小
这段代码使用了Matplotlib库中的函数来生成散点图,并将其保存为png格式的图片。然后使用Qt库中的QPixmap类来加载该图片,并使用OpenCV库中的cv2.imread()函数来加载原始图像。接着使用cv2.cvtColor()函数将原始图像转换为灰度图像,使用np.mean()和np.std()函数计算灰度图像的平均值和标准差,设置增强系数k,并使用cv2.normalize()函数进行亮度增强。最后使用cv2.imshow()函数显示原始图像和增强后的图像,并使用cv2.waitKey()函数等待按下任意按键,使用cv2.destroyAllWindows()函数关闭所有窗口。
至于你提到的“处理结果图怎么变得可以调整大小”,我不太明白你的意思是什么。如果你是想让生成的散点图可以调整大小,可以在代码中使用fig.set_size_inches()函数来设置图像的大小,然后使用plt.savefig()函数将图像保存为指定大小的png格式文件。如果你是想让处理后的灰度图像可以调整大小,可以使用cv2.resize()函数来调整图像的大小。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)