python RGB图像处理

RGB图像扩增

  • 旋转
  • 平移
  • 翻转
  • 裁剪
  • 拼接
  • 加噪(高斯白噪声)
  • 边缘提取canny
  • 边缘提取(灰度、二值)
  • 平滑去噪(去高斯)

旋转

from PIL import Image
img = Image.open('img/imageRaw.png')
img = img.rotate(100)# 将图片旋转100度
img.save("img/rotateImg.png")

python RGB图像处理_第1张图片

平移

from PIL import Image,ImageChops
width, height = img.size
c = ImageChops.offset(img,50,10)#平移距离(img,xoff,yoff)
c.paste((0,0,0),(0,0,50,height))
c.paste((0,0,0),(0,0,width,10))
c.save("img/moveImg.png")

python RGB图像处理_第2张图片

翻转

import cv2
import matplotlib.pyplot as plt
image = cv2.imread("img/imageRaw.png")
scr = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
#0以X轴对称翻转,>0以Y轴对称翻转,<0以X轴Y轴同时翻转
image1 = cv2.flip(scr, 0)
image2 = cv2.flip(scr, 1)
image3 = cv2.flip(scr, -1)
titles = ["raw", "x", "y", "x_y"]
images = [scr, image1, image2, image3]
for i in range(4):
    plt.subplot(2,2,i+1), plt.imshow(images[i])
    plt.xticks([]), plt.yticks([])
    plt.title(titles[i])
plt.savefig("img/flip.png")

python RGB图像处理_第3张图片

裁剪

from PIL import Image
img = Image.open("img/imageRaw.png")
#x0,y0为裁剪区域左上坐标;x1,y1为裁剪区域右下坐标
crop_img = img.crop((50,50,300,300))#不能都是50,因为没有图片
crop_img.save("img/crop.png")

python RGB图像处理_第4张图片

拼接

from PIL import Image
img1 = Image.open("img/imageRaw.png")
img2 = Image.open("img/imageRaw2.png")
# 单幅图像尺寸
width, height = img1.size
# 创建空白长图
result = Image.new('RGBA', (width, height * 2))
# 拼接图片
result.paste(img1, box=(0, 0))
result.paste(img2, box=(0, height))
# 保存图片
result.save('img/res1.png')

python RGB图像处理_第5张图片

加噪(高斯白噪声)

# 添加噪声
import cv2
import numpy as np
#读取图片
img = cv2.imread("img/imageRaw.png")
width= img.size
#设置高斯分布的均值和方差
mean = 100
#设置高斯分布的标准差
sigma = 100
#根据均值和标准差生成符合高斯分布的噪声
gauss = np.random.normal(mean,sigma)
#给图片添加高斯噪声
noisy_img = img + gauss
#设置图片添加高斯噪声之后的像素值的范围
noisy_img = np.clip(noisy_img,a_min=0,a_max=255)
#保存图片
cv2.imwrite("img/noisy_img.png",noisy_img)

python RGB图像处理_第6张图片

边缘提取canny

import cv2
original_img = cv2.imread("img/img.png", 0)
# canny(): 边缘检测
img1 = cv2.GaussianBlur(original_img, (3, 3), 0)
canny = cv2.Canny(img1, 70, 100)
cv2.imwrite("img/canny1.png",canny)

python RGB图像处理_第7张图片

边缘提取(灰度、二值)

https://zhuanlan.zhihu.com/p/397565664

import cv2
import numpy as np
img = cv2.imread("img/imageRaw.png")
# 灰度
gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# 二值化
def get_binary_img(img):
    # gray img to bin image
    bin_img = np.zeros(shape=(img.shape), dtype=np.uint8)
    h = img.shape[0]
    w = img.shape[1]
    for i in range(h):
        for j in range(w):
            bin_img[i][j] = 255 if img[i][j] > 90 else 0
    return bin_img
# 调用
bin_img = get_binary_img(gray_img)
# 提取轮廓
def get_contour(bin_img):
    # get contour
    contour_img = np.zeros(shape=(bin_img.shape),dtype=np.uint8)
    contour_img += 255
    h = bin_img.shape[0]
    w = bin_img.shape[1]
    for i in range(1,h-1):
        for j in range(1,w-1):
            if(bin_img[i][j]==0):
                contour_img[i][j] = 0
                sum = 0
                sum += bin_img[i - 1][j + 1]
                sum += bin_img[i][j + 1]
                sum += bin_img[i + 1][j + 1]
                sum += bin_img[i - 1][j]
                sum += bin_img[i + 1][j]
                sum += bin_img[i - 1][j - 1]
                sum += bin_img[i][j - 1]
                sum += bin_img[i + 1][j - 1]
                if sum ==  0:
                    contour_img[i][j] = 255

    return contour_img
# 调用
contour_img = get_contour(bin_img)
def get_converse(img):
    h = img.shape[0]
    w = img.shape[1]
    for i in range(h):
        for j in range(w):
            bin_img[i][j] = 0 if img[i][j] == 255 else 255
    return bin_img
contour_img = get_converse(contour_img)
cv2.imwrite("img/canny2.png",contour_img)

python RGB图像处理_第8张图片

平滑去噪(去高斯)

import cv2
from matplotlib import pyplot as plt
noisy_img = cv2.imread("img/noisy_img.png")
result = cv2.GaussianBlur(noisy_img, (3, 3), 0)
cv2.imwrite("img/GaussianBlur.png",result)

python RGB图像处理_第9张图片

你可能感兴趣的:(图像处理,python,图像处理)