python image basic image process

from:https://www.cnblogs.com/kongzhagen/p/6295925.html

https://www.cnblogs.com/denny402/p/5124152.html

https://blog.csdn.net/qq_23589775/article/details/81143584

Python-Image 基本的图像处理操作,有需要的朋友可以参考下。

Python 里面最常用的图像操作库是

 

Image library(PIL),功能上,虽然还不能跟Matlab比较,但是还是比较强大的,废话补多少,写点记录笔记。

1. 首先需要导入需要的图像库:

import Image

2. 读取一张图片:

im=Image.open('/home/Picture/test.jpg')

3. 显示一张图片:

im.show()

4. 保存图片:

im.save("save.gif","GIF") #保存图像为gif格式

5. 创建新图片:

Image.new(mode,size)

Image.new(mode,size,color)

栗子:newImg = Image.new("RGBA",(640,480),(0,255,0))
newImg.save("newImg.png","PNG")

6.两张图片相加:

Image.blend(img1,img2,alpha) # 这里alpha表示img1和img2的比例参数

7. 点操作:

im.point(function) #,这个function接受一个参数,且对图片中的每一个点执行这个函数
比如:out=im.point(lambdai:i*1.5)#对每个点进行50%的加强

8. 查看图像信息:
im.format, im.size, im.mode

9. 图片裁剪:
box=(100,100,500,500)

#设置要裁剪的区域

region=im.crop(box) #此时,region是一个新的图像对象。

10. 图像黏贴(合并)

im.paste(region,box)#粘贴box大小的region到原先的图片对象中。

11. 通道分离:
r,g,b=im.split()#分割成三个通道,此时r,g,b分别为三个图像对象。

12. 通道合并:
im=Image.merge("RGB",(b,g,r))#将b,r两个通道进行翻转。

13. 改变图像的大小:
out=img.resize((128,128))#resize成128*128像素大小

14. 旋转图像:
out=img.rotate(45) #逆时针旋转45度

有更方便的:
region = region.transpose(Image.ROTATE_180)

15. 图像转换:
out = im.transpose(Image.FLIP_LEFT_RIGHT)

#左右对换。

out = im.transpose(Image.FLIP_TOP_BOTTOM)

#上下对换

16. 图像类型转换:
im=im.convert("RGBA")

17. 获取某个像素位置的值:
im.getpixel((4,4))

18. 写某个像素位置的值:
img.putpixel((4,4),(255,0,0))

 

python数字图像处理(7):图像的形变与缩放

图像的形变与缩放,使用的是skimage的transform模块,函数比较多,功能齐全。

1、改变图片尺寸resize

函数格式为:

skimage.transform.resize(imageoutput_shape)

image: 需要改变尺寸的图片

output_shape: 新的图片尺寸

复制代码

from skimage import transform,data
import matplotlib.pyplot as plt
img = data.camera()
dst=transform.resize(img, (80, 60))
plt.figure('resize')

plt.subplot(121)
plt.title('before resize')
plt.imshow(img,plt.cm.gray)

plt.subplot(122)
plt.title('before resize')
plt.imshow(dst,plt.cm.gray)

plt.show()

复制代码

将camera图片由原来的512*512大小,变成了80*60大小。从下图中的坐标尺,我们能够看出来:

 

2、按比例缩放rescale

函数格式为:

skimage.transform.rescale(image, scale[, ...])

scale参数可以是单个float数,表示缩放的倍数,也可以是一个float型的tuple,如[0.2,0.5],表示将行列数分开进行缩放

from skimage import transform,data
img = data.camera()
print(img.shape)  #图片原始大小 
print(transform.rescale(img, 0.1).shape)  #缩小为原来图片大小的0.1倍
print(transform.rescale(img, [0.5,0.25]).shape)  #缩小为原来图片行数一半,列数四分之一
print(transform.rescale(img, 2).shape)   #放大为原来图片大小的2倍

结果为:

(512, 512)
(51, 51)
(256, 128)
(1024, 1024)

3、旋转 rotate

skimage.transform.rotate(image, angle[, ...],resize=False)

angle参数是个float类型数,表示旋转的度数

resize用于控制在旋转时,是否改变大小 ,默认为False

复制代码

from skimage import transform,data
import matplotlib.pyplot as plt
img = data.camera()
print(img.shape)  #图片原始大小
img1=transform.rotate(img, 60) #旋转90度,不改变大小 
print(img1.shape)
img2=transform.rotate(img, 30,resize=True)  #旋转30度,同时改变大小
print(img2.shape)   

plt.figure('resize')

plt.subplot(121)
plt.title('rotate 60')
plt.imshow(img1,plt.cm.gray)

plt.subplot(122)
plt.title('rotate  30')
plt.imshow(img2,plt.cm.gray)

plt.show()

复制代码

显示结果:

4、图像金字塔

以多分辨率来解释图像的一种有效但概念简单的结构就是图像金字塔。图像金字塔最初用于机器视觉和图像压缩,一幅图像的金字塔是一系列以金字塔形状排列的分辨率逐步降低的图像集合。金字塔的底部是待处理图像的高分辨率表示,而顶部是低分辨率的近似。当向金字塔的上层移动时,尺寸和分辨率就降低。

在此,我们举一个高斯金字塔的应用实例,函数原型为:

skimage.transform.pyramid_gaussian(image, downscale=2)
downscale控制着金字塔的缩放比例

复制代码

import numpy as np
import matplotlib.pyplot as plt
from skimage import data,transform

image = data.astronaut()  #载入宇航员图片
rows, cols, dim = image.shape  #获取图片的行数,列数和通道数
pyramid = tuple(transform.pyramid_gaussian(image, downscale=2))  #产生高斯金字塔图像
#共生成了log(512)=9幅金字塔图像,加上原始图像共10幅,pyramid[0]-pyramid[1]

composite_image = np.ones((rows, cols + cols / 2, 3), dtype=np.double)  #生成背景

composite_image[:rows, :cols, :] = pyramid[0]  #融合原始图像

i_row = 0
for p in pyramid[1:]:
    n_rows, n_cols = p.shape[:2]
    composite_image[i_row:i_row + n_rows, cols:cols + n_cols] = p  #循环融合9幅金字塔图像
    i_row += n_rows

plt.imshow(composite_image)
plt.show()

复制代码

上右图,就是10张金字塔图像,下标为0的表示原始图像,后面每层的图像行和列变为上一层的一半,直至变为1

除了高斯金字塔外,还有其它的金字塔,如:

skimage.transform.pyramid_laplacian(image, downscale=2):

Chapter 1, A Taste of Machine Learning, will gently introduce you to the different subfields of machine learning, and explain how to install OpenCV and other essential tools in the Python Anaconda environment. Chapter 2, Working with Data in OpenCV and Python, will show you what a typical machine learning workflow looks like, and where data comes in to play. I will explain the difference between training and test data, and show you how to load, store, manipulate, and visualize data with OpenCV and Python. Chapter 3, First Steps in Supervised Learning, will introduce you to the topic of supervised learning by reviewing some core concepts, such as classification and regression. You will learn how to implement a simple machine learning algorithm in OpenCV, how to make predictions about the data, and how to evaluate your model. Chapter 4, Representing Data and Engineering Features, will teach you how to get a feel for some common and well-known machine learning datasets and how to extract the interesting stuff from your raw data. Chapter 5, Using Decision Trees to Make a Medical Diagnosis, will show you how to build decision trees in OpenCV, and use them in a variety of classification and regression problems. Chapter 6, Detecting Pedestrians with Support Vector Machines, will explain how to build support vector machines in OpenCV, and how to apply them to detect pedestrians in images. Chapter 7, Implementing a Spam Filter with Bayesian Learning, will introduce you to probability theory, and show you how you can use Bayesian inference to classify emails as spam or not. Chapter 8, Discovering Hidden Structures with Unsupervised Learning, will talk about unsupervised learning algorithms such as k-means clustering and Expectation-Maximization, and show you how they can be used to extract hidden structures in simple, unlabeled datasets. Chapter 9, Using Deep Learning to Classify Handwritten Digits, will introduce you to the exciting field of deep learning. Starting with the perceptron and multi-layer perceptrons, you will learn how to build deep neural networks in order to classify handwritten digits from the extensive MNIST database. Chapter 10, Combining Different Algorithms into an Ensemble, will show you how to effectively combine multiple algorithms into an ensemble in order to overcome the weaknesses of individual learners, resulting in more accurate and reliable predictions. Chapter 11, Selecting the Right Model with Hyper-Parameter Tuning, will introduce you to the concept of model selection, which allows you to compare different machine learning algorithms in order to select the right tool for the task at hand. Chapter 12, Wrapping Up, will conclude the book by giving you some useful tips on how to approach future machine learning problems on your own, and where to find information on more advanced topics.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值