opencvpython图像代码_PythonOpenCV各种图像库的图像读写 增强 方式的简单介绍(附代码)...

该篇博客详细介绍了图像处理的各种技术,包括缩放、对比度亮度调整、噪声添加、随机裁剪、平移变换、亮度和色度增强、对比度调整以及模糊处理。通过Python的OpenCV和PIL库实现了一系列图像操作,展示了如何增强和改变图像的视觉效果。
摘要由CSDN通过智能技术生成

resize给定高和宽的像数值

img = cv2.imread('../96.jpg',0) # 0表示读入灰色图片,1表示读入彩色图片

resized = cv2.resize(img, (100,100), interpolation = cv2.INTER_AREA)

print(np.array(resized).shape)

cv2.imwrite("11.jpg",img)

图像各种增强转化代码

#!/usr/bin/python

# -*- coding: UTF-8 -*-

# 导入库

import cv2

import numpy as np

import random #random模块用于生成随机数

import tensorflow as tf

import matplotlib.pyplot as plt

import imutils #3

from PIL import Image

from PIL import ImageEnhance

# 执行图片缩放

def zoom(read,out,name):

image = cv2.imread(read+name)

r = 100.0 / image.shape[1]

dim = (95, int(image.shape[0] * r))

# 执行图片缩放,并显示

resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)

# cv2.imshow("resized", resized) #17

# cv2.waitKey(0) #18

write(out, "zoom_" + name, resized)

#对比度亮度

def turn(read,out,name):

pic = cv2.imread(read+name) #读入图片

contrast = 1 #对比度

brightness = 80 #亮度

pic_turn = cv2.addWeighted(pic,contrast,pic,0,brightness)

#cv2.addWeighted(对象,对比度,对象,对比度)

'''cv2.addWeighted()实现的是图像透明度的改变与图像的叠加'''

# cv2.imshow('turn', pic_turn) #显示图片

# cv2.waitKey(0) # 18

write(out, "pic_turn_" + name, pic_turn)

#噪声

def pic_noise(read,out,name):

pic = cv2.imread(read+name)

for i in range(150):

pic[random.randint(0, pic.shape[0]-1)][random.randint(0,pic.shape[1]-1)][:]=255

# cv2.imshow('pic_noise', pic)

# cv2.waitKey(0) # 18

write(out, "pic_" + name, pic)

#随机裁剪

def crop_img():

img = cv2.imread("5.jpg")

#将图片进行随机裁剪为280×280

crop_img = tf.random_crop(img,[280,280,3])

sess = tf.InteractiveSession()

#显示图片

# cv2.imwrite("img/crop.jpg",crop_img.eval())

plt.figure(1)

plt.subplot(121)

#将图片由BGR转成RGB

img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)

plt.imshow(img)

plt.title("原始图片")

plt.subplot(122)

crop_img = cv2.cvtColor(crop_img.eval(),cv2.COLOR_BGR2RGB)

plt.title("裁剪后的图片")

plt.imshow(crop_img)

plt.show()

sess.close()

#移动

def move(read,out,name):

image = cv2.imread(read+name) #8

# cv2.imshow("原始图片", image) #9

M = np.float32([[1, 0, 35], [0, 1, 35]]) #10

shifted = cv2.warpAffine(image, M, (image.shape[1], image.shape[0])) #11

# cv2.imshow("Shifted Down and Right", shifted) #12

write(out,"sdar_"+name,shifted)

M = np.float32([[1, 0, -30], [0, 1, -30]]) #13

shifted = cv2.warpAffine(image, M, (image.shape[1], image.shape[0])) #14

# cv2.imshow("Shifted Up and Left", shifted) #15

write(out, "sual_" + name, shifted)

shifted = imutils.translate(image, 0, 20) #16

# cv2.imshow("Shifted down", shifted) #17

# write(out, "sd_" + name, shifted)

# cv2.waitKey(0) #18

def write(out,name,image):

cv2.imwrite(out + name, image)

#亮度增强

def brightness(read,out,name):

image = Image.open(read+name)

# 亮度增强,增强因子为0.0将产生黑色图像;为1.0将保持原始图像。

enh_bri = ImageEnhance.Brightness(image)

image_brightened = enh_bri.enhance(1.5)

image_brightened.save(out+"brightness1_" + name)

image_brightened = enh_bri.enhance(0.8)#变暗

image_brightened.save(out + "brightness2_" + name)

#色度增强

def color(read,out,name):

image = Image.open(read + name)

# 色度,增强因子为1.0是原始图像

enh_col = ImageEnhance.Color(image)

# 色度增强

image_colored = enh_col.enhance(1.5)

image_colored.save(out+"image_colored_1_" + name)

# 色度减弱

image_colored1 = enh_col.enhance(0.8)

image_colored1.save(out + "image_colored_2_" + name)

def enhance(read,out,name):

image = Image.open(read + name)

enh_con = ImageEnhance.Contrast(image)

# 对比度,增强因子为1.0是原始图片

# 对比度增强

image_contrasted = enh_con.enhance(1.5)

image_contrasted.save(out+"enh_con1_" + name)

image_contrasted = enh_con.enhance(0.8)

image_contrasted.save(out+"enh_con2_" + name)

def blur_demo(read,out,name):#平均模糊

image = cv2.imread(read + name) # 8

dst = cv2.blur(image,(5,5))

# cv2.imshow('blur1', dst)

write(out, "blur1_" + name, dst)

dst = cv2.blur(image, (10, 10))

write(out, "blur2_" + name, dst)

# cv2.imshow('blur2',dst)

# write(out, "sd_" + name, shifted)

# cv2.waitKey(0) #18

def run(readPath, out, picture):

move(readPath, out, picture)

zoom(readPath, out, picture)

pic_noise(readPath, out, picture)

turn(readPath, out, picture)

brightness(readPath, out, picture)

color(readPath, out, picture)

enhance(readPath, out, picture)

blur_demo(readPath, out, picture)

'''

out = "D:\\tmp\\a1\\"

picture = "396.jpg"

readPath = "D:\\tmp\\other\\"

run(readPath, out, picture)

'''

'''

#-*- coding: UTF-8 -*-

from PIL import Image

from PIL import ImageEnhance

#原始图像

image = Image.open('lena.jpg')

image.show()

#亮度增强

enh_bri = ImageEnhance.Brightness(image)

brightness = 1.5

image_brightened = enh_bri.enhance(brightness)

image_brightened.show()

#色度增强

enh_col = ImageEnhance.Color(image)

color = 1.5

image_colored = enh_col.enhance(color)

image_colored.show()

#对比度增强

enh_con = ImageEnhance.Contrast(image)

contrast = 1.5

image_contrasted = enh_con.enhance(contrast)

image_contrasted.show()

#锐度增强

enh_sha = ImageEnhance.Sharpness(image)

sharpness = 3.0

image_sharped = enh_sha.enhance(sharpness)

image_sharped.show()

'''

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值