-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9b4678
commit 645b5bb
Showing
2 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
#!/usr/bin/env python3 | ||
# _*_coding:utf-8_*_ | ||
# @Author : Xnhyacinth | ||
# @Time : 2022/3/8 17:38 | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
import cv2 | ||
|
||
# Read in the image | ||
image = cv2.imread('img/1.jpg') | ||
|
||
# Print out the type of image data and its dimensions (height, width, and color) | ||
print('This image is:', type(image), | ||
' with dimensions:', image.shape) | ||
|
||
|
||
'''RGB''' | ||
# RGB | ||
# Make a copy of the image | ||
image_copy = np.copy(image) | ||
|
||
# OpenCV 会读取 BGR 格式(而不是 RGB 格式)的图像,因为一开始开发 OpenCV 的时候,BGR 颜色格式对相机制造商和图像软件提供商来说很受欢迎。 | ||
# 红色通道被认为是最不重要的颜色通道,因此列在最后面。但是,现在标准改变了,很多图像软件和相机都使用 RGB 格式 | ||
|
||
# Change color to RGB (from BGR) | ||
image_copy_rgb = cv2.cvtColor(image_copy, cv2.COLOR_BGR2RGB) | ||
|
||
# Display the image copy | ||
plt.imshow(image_copy_rgb) | ||
|
||
# 定义颜色阈值 | ||
lower_blue = np.array([0, 0, 250]) | ||
upper_blue = np.array([250, 250, 255]) | ||
|
||
# 创建遮罩 | ||
mask = cv2.inRange(image_copy_rgb, lower_blue, upper_blue) | ||
|
||
# Vizualize the mask | ||
plt.imshow(mask, cmap='gray') | ||
|
||
masked_image_rgb = np.copy(image_copy_rgb) | ||
|
||
masked_image_rgb[mask != 0] = [0, 0, 0] | ||
|
||
# Display it! | ||
plt.imshow(masked_image_rgb) | ||
# Load in a background image, and convert it to RGB | ||
background_image = cv2.imread('img/10.jpeg') | ||
# print(background_image.shape) | ||
background_image_rgb = cv2.cvtColor(background_image, cv2.COLOR_BGR2RGB) | ||
|
||
# Crop it to the right size (1482x988) | ||
crop_background = background_image_rgb[0:1482, 0:988] | ||
|
||
crop_background[mask == 0] = [0, 0, 0] | ||
|
||
# Display the background | ||
plt.imshow(crop_background) | ||
|
||
# Add the two images together to create a complete image! | ||
complete_image = masked_image_rgb + crop_background | ||
|
||
# Display the result | ||
plt.imshow(complete_image) | ||
|
||
|
||
|
||
'''HSV''' | ||
# Change color to HSV (from BGR) | ||
image_copy_hsv = cv2.cvtColor(image_copy, cv2.COLOR_BGR2HSV) | ||
|
||
# Display the image copy | ||
plt.imshow(image_copy_hsv) | ||
|
||
#提取图片的蓝色 | ||
lower_hsv=np.array([100,43,46]) | ||
upper_hsv=np.array([124,255,255]) | ||
mask=cv2.inRange(image_copy_hsv,lowerb=lower_hsv,upperb=upper_hsv) | ||
plt.imshow(mask, cmap='gray') | ||
# cv2.imshow("mask",mask) | ||
|
||
masked_image_hsv = np.copy(image_copy_hsv) | ||
|
||
masked_image_hsv[mask != 0] = [0, 0, 0] | ||
|
||
# Display it! | ||
plt.imshow(masked_image_hsv) | ||
|
||
# convert image it to HSV | ||
|
||
# print(background_image.shape) | ||
background_image_hsv = cv2.cvtColor(background_image, cv2.COLOR_BGR2HSV) | ||
|
||
# Crop it to the right size (1482x988) | ||
crop_background = background_image_hsv[0:1482, 0:988] | ||
|
||
crop_background[mask == 0] = [0, 0, 0] | ||
plt.imshow(crop_background) | ||
|
||
# Add the two images together to create a complete image! | ||
complete_image_hsv = masked_image_hsv + crop_background | ||
|
||
# Display the result | ||
plt.imshow(complete_image_hsv) | ||
|
||
# Change color to RGB (from HSV) | ||
complete_image_hsv = cv2.cvtColor(complete_image_hsv, cv2.COLOR_HSV2RGB) | ||
# Display the result | ||
plt.imshow(complete_image_hsv) | ||
|
||
|
||
|
||
'''HSL''' | ||
# Change color to HLS (from BGR) | ||
image_copy_hls = cv2.cvtColor(image_copy, cv2.COLOR_BGR2HLS) | ||
|
||
# Display the image copy | ||
plt.imshow(image_copy_hls) | ||
|
||
#提取图片的蓝色 | ||
lower_hls=np.array([100,43,46]) | ||
upper_hls=np.array([124,255,255]) | ||
mask=cv2.inRange(image_copy_hls,lowerb=lower_hls,upperb=upper_hls) | ||
plt.imshow(mask, cmap='gray') | ||
|
||
masked_image_hls = np.copy(image_copy_hls) | ||
|
||
masked_image_hls[mask != 0] = [0, 0, 0] | ||
|
||
# Display it! | ||
plt.imshow(masked_image_hls) | ||
|
||
# convert image it to HLS | ||
|
||
# print(background_image.shape) | ||
background_image_hls = cv2.cvtColor(background_image, cv2.COLOR_BGR2HLS) | ||
|
||
# Crop it to the right size (1482x988) | ||
crop_background = background_image_hls[0:1482, 0:988] | ||
|
||
crop_background[mask == 0] = [0, 0, 0] | ||
plt.imshow(crop_background) | ||
|
||
|
||
# Add the two images together to create a complete image! | ||
complete_image_hls = masked_image_hls + crop_background | ||
|
||
# Display the result | ||
plt.imshow(complete_image_hls) | ||
|
||
# Change color to RGB (from HSV) | ||
complete_image_hls = cv2.cvtColor(complete_image_hls, cv2.COLOR_HLS2RGB) | ||
# Display the result | ||
plt.imshow(complete_image_hls) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#!/usr/bin/env python3 | ||
# _*_coding:utf-8_*_ | ||
# @Author : Xnhyacinth | ||
# @Time : 2022/3/8 17:23 | ||
|
||
from matplotlib import pyplot as plt | ||
import cv2 | ||
import numpy as np | ||
|
||
|
||
# 定义高斯噪声函数 | ||
def noise_Gaussian(img, mean, var): | ||
""" | ||
img:原始图像 | ||
mean:均值 | ||
var:方差,值越大噪声越大 | ||
""" | ||
|
||
# 创建均值为mean方差为var呈高斯分布的图像矩阵 | ||
noise = np.random.normal(mean, var ** 0.5, img.shape) | ||
|
||
# 将原始图像的像素值进行归一化,除以255使像素值在0-1之间 | ||
img = np.array(img / 255, dtype=float) | ||
|
||
# 噪声和图片合并即加噪后的图像 | ||
out = img + noise | ||
|
||
# 解除归一化,乘以255将加噪后的图像的像素值恢复 | ||
out = np.uint8(out * 255) | ||
return out | ||
|
||
|
||
# 高斯滤波器 | ||
|
||
def gaussian_filter(img, K_size=3, sigma=1.0): | ||
img = np.asarray(np.uint8(img)) | ||
if len(img.shape) == 3: | ||
H, W, C = img.shape | ||
else: | ||
img = np.expand_dims(img, axis=-1) | ||
H, W, C = img.shape | ||
|
||
## Zero padding | ||
pad = K_size // 2 | ||
out = np.zeros((H + pad * 2, W + pad * 2, C), dtype=np.float64) | ||
out[pad: pad + H, pad: pad + W] = img.copy().astype(np.float64) | ||
|
||
## prepare Kernel | ||
K = np.zeros((K_size, K_size), dtype=np.float64) | ||
for x in range(-pad, -pad + K_size): | ||
for y in range(-pad, -pad + K_size): | ||
K[y + pad, x + pad] = np.exp(-(x ** 2 + y ** 2) / (2 * (sigma ** 2))) | ||
K /= (2 * np.pi * sigma * sigma) | ||
K /= K.sum() | ||
tmp = out.copy() | ||
|
||
# filtering | ||
for y in range(H): | ||
for x in range(W): | ||
for c in range(C): | ||
out[pad + y, pad + x, c] = np.sum(K * tmp[y: y + K_size, x: x + K_size, c]) | ||
out = np.clip(out, 0, 255) | ||
out = out[pad: pad + H, pad: pad + W].astype(np.uint8) | ||
return out | ||
|
||
|
||
if __name__ == "__main__": | ||
# 上传图片 | ||
# read the image | ||
img = cv2.imread("D:/photo/11.jpeg") | ||
|
||
# Print out the type of image data and its dimensions (height, width, and color) | ||
print('This image is:', type(img), | ||
' with dimensions:', img.shape) | ||
|
||
img_copy = np.copy(img) | ||
# change color to rgb(from bgr) | ||
img_copy = cv2.cvtColor(img_copy, cv2.COLOR_BGR2RGB) | ||
# display | ||
cv2.imshow("img", img_copy) | ||
|
||
## 添加高斯噪声 | ||
# 显示添加高斯噪声的结果图像 | ||
out = noise_Gaussian(img_copy, mean=0, var=0.003) | ||
cv2.imshow("out", out) | ||
|
||
# sigma=0.5 k_size=3 | ||
out_05 = gaussian_filter(img_copy, sigma=0.5) | ||
cv2.imshow("out_05", out_05) | ||
|
||
# sigma=0.5 k_size=5 | ||
out_05_5 = gaussian_filter(img_copy, K_size=5, sigma=0.5) | ||
cv2.imshow("out_05_5", out_05_5) | ||
|
||
cv2.waitKey(0) |