kaggle竞赛 | 计算机视觉 | 数字图像基础操作

cv2基本操作

import numpy as np
import matplotlib.pyplot as plt
import cv2

img = cv2.imread('cat.jpeg')
plt.imshow(img)

在这里插入图片描述

img = cv2.imread('cat.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)

BGR2RGB,将BGR转化为RGB
在这里插入图片描述
转化为灰度图

img = cv2.imread('cat.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.imshow(img,cmap='gray')

在这里插入图片描述

转化为HSV图像

H代表色色调,用角度度量,取值范围为0°-360°。从红色开始安逆时针方向计算,红色为0度,绿色为120度,蓝色为240度。
S代表饱和度,饱和度S表示颜色接近光谱色的程度。一种颜色,可以看成是某种光谱色与白色混合的结果。其中光谱色所占的比例越大,颜色越接近光谱色,颜色的饱和度也就越高。饱和度高,颜色就越深越艳。
V代表明度。明度表示颜色明亮的程度,对于光源色,明度值与发光体的亮度有关;对于物体色,明度值和物体的透射比和反色比有关。通常取值范围为0%(黑)到100%(白)。

RGB

RGB 是我们接触最多的颜色空间,由三个通道表示一幅图像,分别为红色®,绿色(G)和蓝色(B)。这三种颜色的不同组合可以形成几乎所有的其他颜色。
RGB 颜色空间是图像处理中最基本、最常用、面向硬件的颜色空间,比较容易理解。
RGB 颜色空间利用三个颜色分量的线性组合来表示颜色,任何颜色都与这三个分量有关,而且这三个分量是高度相关的,所以连续变换颜色时并不直观,想对图像的颜色进行调整需要更改这三个分量才行。

img = cv2.imread('cat.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
plt.imshow(img,cmap='gray')

在这里插入图片描述
添加滤波器,类似于卷积
不同的kernel,得到的图片效果不同
此时的kernel_sharpening会使图片更具有细节

img = cv2.imread('cat.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

plt.figure(figsize=(20, 20))
plt.subplot(1, 2, 1)
plt.imshow(img)

kernel_sharpening = np.array([[-1,-1,-1], 
                              [-1,9,-1], 
                              [-1,-1,-1]])
sharpened = cv2.filter2D(img, -1, kernel_sharpening)

plt.subplot(1, 2, 2)
plt.title("Image Sharpening")
plt.imshow(sharpened)

在这里插入图片描述

erode操作,忽视部分细节

img = cv2.imread('cat.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

plt.figure(figsize=(20, 20))
plt.subplot(1, 2, 1)
plt.title("Original")
plt.imshow(img)


# Let's define our kernel size
kernel = np.ones((5,5), np.uint8)

# Now we erode
erosion = cv2.erode(img, kernel, iterations = 1)

plt.subplot(1, 2, 2)
plt.title("Erosion")
plt.imshow(erosion)

在这里插入图片描述
提取图片线条,边缘检测

img = cv2.imread('cat.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
height, width,_ = img.shape

# X角度线条
sobel_x = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)
# Y角度线条
sobel_y = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)

plt.figure(figsize=(20, 20))

# 原始线条
plt.subplot(3, 2, 1)
plt.title("Original")
plt.imshow(img)

plt.subplot(3, 2, 2)
plt.title("Sobel X")
plt.imshow(sobel_x)

plt.subplot(3, 2, 3)
plt.title("Sobel Y")
plt.imshow(sobel_y)


sobel_OR = cv2.bitwise_or(sobel_x, sobel_y)

plt.subplot(3, 2, 4)
plt.title("sobel_OR")
plt.imshow(sobel_OR)

# 拉普拉斯算子  边缘检测
laplacian = cv2.Laplacian(img, cv2.CV_64F)

plt.subplot(3, 2, 5)
plt.title("Laplacian")
plt.imshow(laplacian)

 # Canny()边缘检测
canny = cv2.Canny(img, 50, 120)

plt.subplot(3, 2, 6)
plt.title("Canny")

调整图片尺寸和大小

img = cv2.imread('cat.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

plt.figure(figsize=(20, 20))

plt.subplot(2, 2, 1)
plt.title("Original")
plt.imshow(img)

# 3/4图片大小
image_scaled = cv2.resize(img, None, fx=0.75, fy=0.75)

plt.subplot(2, 2, 2)
plt.title("Scaling - Linear Interpolation")
plt.imshow(image_scaled)

# 2倍图片大小
img_scaled = cv2.resize(img, None, fx=2, fy=2, interpolation = cv2.INTER_CUBIC)

plt.subplot(2, 2, 3)
plt.title("Scaling - Cubic Interpolation")
plt.imshow(img_scaled)

# 调整图片宽高
img_scaled = cv2.resize(img, (900, 400), interpolation = cv2.INTER_AREA)

plt.subplot(2, 2, 4)
plt.title("Scaling - Skewed Size")
plt.imshow(img_scaled)

选取图片的部分区域

img = cv2.imread('cat.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

plt.figure(figsize=(20, 20))

plt.subplot(2, 2, 1)
plt.title("Original")
plt.imshow(img)

height, width = img.shape[:2]

# 让我们获取起始像素坐标(裁剪矩形的左上角)
start_row, start_col = int(height * .25), int(width * .25)

# 让我们获取起始像素坐标(裁剪矩形的右下角)
end_row, end_col = int(height * .75), int(width * .75)

# 只需使用索引即可裁剪出所需的矩形
cropped = img[start_row:end_row , start_col:end_col]


plt.subplot(2, 2, 2)
plt.title("Cropped")
plt.imshow(cropped)

在这里插入图片描述
滤波器的kernel全1,是一种模糊操作

img = cv2.imread('cat.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

plt.figure(figsize=(20, 20))

plt.subplot(2, 2, 1)
plt.title("Original")
plt.imshow(img)

# Creating our 3 x 3 kernel
kernel_3x3 = np.ones((3, 3), np.float32) / 9

# We use the cv2.fitler2D to conovlve the kernal with an image 
blurred = cv2.filter2D(img, -1, kernel_3x3)

plt.subplot(2, 2, 2)
plt.title("3x3 Kernel Blurring")
plt.imshow(blurred)

# Creating our 7 x 7 kernel
kernel_7x7 = np.ones((7, 7), np.float32) / 49

blurred2 = cv2.filter2D(img, -1, kernel_7x7)

plt.subplot(2, 2, 3)
plt.title("7x7 Kernel Blurring")
plt.imshow(blurred2)

在这里插入图片描述
卷积核越大,越模糊

图片哈希

import imagehash
from PIL import Image

ahash = imagehash.average_hash(Image.open("cat.jpeg"))
ahash

在这里插入图片描述

str(ahash)

在这里插入图片描述

图像数据扩展

无监督数据扩展方法

albumentations库 简单实用

from albumentations import (
    HorizontalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
    Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
    IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine,
    IAASharpen, IAAEmboss, RandomContrast, RandomBrightness, Flip, OneOf, Compose, RandomGamma, 
    ElasticTransform, ChannelShuffle,RGBShift, Rotate
)

# 图像水平翻转
img = cv2.imread('cat.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# 水平翻转的概率
aug = HorizontalFlip(p=1)
img_aug = aug(image = img)['image']

plt.figure(figsize=(20, 20))
plt.subplot(1, 2, 1)
plt.imshow(img)

plt.subplot(1, 2, 2)
plt.imshow(img_aug)

在这里插入图片描述
部分复杂形态变化

img = cv2.imread('cat.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

aug = GridDistortion(p=1)
img_aug = aug(image = img)['image']

plt.figure(figsize=(20, 20))
plt.subplot(1, 2, 1)
plt.imshow(img)

plt.subplot(1, 2, 2)
plt.imshow(img_aug)

在这里插入图片描述
角度变化

img = cv2.imread('cat.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

aug = ShiftScaleRotate(p=1)
img_aug = aug(image = img)['image']

plt.figure(figsize=(20, 20))
plt.subplot(1, 2, 1)
plt.imshow(img)

plt.subplot(1, 2, 2)
plt.imshow(img_aug)

在这里插入图片描述

多个数据扩增方法一起运用 Compose方法

img = cv2.imread('cat.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

aug = Compose([
    ShiftScaleRotate(p=1),
    HorizontalFlip(p=1),
    RandomBrightness(p=1)
])
img_aug = aug(image = img)['image']

plt.figure(figsize=(20, 20))
plt.subplot(1, 2, 1)
plt.imshow(img)

plt.subplot(1, 2, 2)
plt.imshow(img_aug)

在这里插入图片描述

监督式数据扩展方法

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码魔法师!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值