practical python and opencv_3rdEdition

cv2.destroyAllWindows()

 


image = cv2.imread('c:/Users/oucbu/Pictures/1.jpg')
 

image’s type is <class 'numpy.ndarray'>

所以之后对image的处理就跳到了numpy的应用了。

此时的image是一个三维数组。数组大小为:

img.shape=(height,width,channel)

当然可以反过来访问:

height=img.shape[0]

(h,w)=img.shape[:2]   意思是[0] [1]合并输出

如果想访问数组元素:

img[0,0,0]=(0,0)像素点R值

img[0,0]=(0,0)的RGB值    r,g,b=img[0,0 r,g,b=img[0,0]

img[0]=第0行

 

 

数组部分截取:

img1=img[0:30,0:40]

 

数组部分赋值:

img[0:30,0:40]=(0,255,0)

 

cv2.imshow(‘winsows name’,image)

第一个参数不能少,image类型是ndarray数组就行(2维或者3维)

 

颜色变换

imageOut=cv2.cvtcolot(image,cv2.COLOR_RGB2GRAY)

COLOR_BGR2GRAY

cv2.COLOR_BGR2HSV

cv2.COLOR_BGR2LAB

变灰度图实际上就是少一个维度,但是不是简单的从RGB中任取一个作为灰度图,是有计算公式的。

 

画线、框、圆

cv2.line(img,(0,0),(50,50),(0,255,0),30)  30是线宽,如果是-1,意思是画实心图
cv2.rectangle(img,(40,40),(90,90),(255,0,0),20)
cv2.circle(img
,(100,100),40,10)

带()的参数类型为tuple。

 

平移:

M = np.float32([[1, 0, -50], [0, 1, -90]])  #下、右 平移

M = np.float32([[1, 0, -50], [0, 1, -90]])  # 上、左平移
shifted = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))
cv2.imshow(
"Shifted Up and Left", shifted)

 

shifted = imutils.translate(image, -10, 100)#左、下平移

 

M = cv2.getRotationMatrix2D((image.shape[1]//2,image.shape[0]//2), -90, 1.0)#顺时针90-1为反转
rotated = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))


rotated = imutils.rotate(image, 180)#简单


flipped = cv2.flip(image, 1)#反转0:垂直,-1:水平

 

resized = cv2.resize(image, (30,30), interpolation = cv2.INTER_AREA)#缩小到(3030)方形图
dim = (150, int(150 * image.shape[0]  / image.shape[1] ))
resized = cv2.resize(image
, dim, interpolation = cv2.INTER_AREA)#同比例缩小


resized = imutils.resize(image, width = 100)

 

全像素批量处理:

cv2.add(np.uint8([200]), np.uint8([100])) = 255  # 因为是unsigned int8,最大256
cv2.subtract(np.uint8([50]), np.uint8([100])) = 0
np.uint8([200]) + np.uint8([100]) = 44
np.uint8([50]) - np.uint8([100]) = 206

 

M = np.ones(image.shape, dtype="uint8") * 100  # 此时M可以说是一张图,也可以说是array
added = cv2.add(image, M)
cv2.imshow(
"Added", added)

M = np.ones(image.shape
, dtype="uint8") * 50
subtracted = cv2.subtract(image, M)
cv2.imshow(
"Subtracted", subtracted)
cv2.waitKey(
0)

bitwiseAnd = cv2.bitwise_and(rectangle
, circle)
cv2.imshow(
"AND", bitwiseAnd)
cv2.waitKey(
0)

bitwiseOr = cv2.bitwise_or(rectangle
, circle)
cv2.imshow(
"OR", bitwiseOr)
cv2.waitKey(
0)

bitwiseXor = cv2.bitwise_xor(rectangle
, circle)
cv2.imshow(
"XOR", bitwiseXor)
cv2.waitKey(
0)

bitwiseNot = cv2.bitwise_not(circle)
cv2.imshow(
"NOT", bitwiseNot)
cv2.waitKey(
0)

 

 

(B, G, R) = cv2.split(image)  # 通道拆分
merged = cv2.merge([B, G, R])

变成3个2维数组

 

看单通道彩图:

 

zeros = np.zeros(image.shape[:2], dtype = "uint8")

cv2.imshow("Red", cv2.merge([zeros, zeros, R]))

cv2.imshow("Green", cv2.merge([zeros, G, zeros]))

cv2.imshow("Blue", cv2.merge([B, zeros, zeros]))

 

直方图:

image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
hist = cv2.calcHist([image]
, [0], None, [256], [0, 256])#[ndarray]=list

当然也可以看RGB各通道的直方图。

 

zip使用:

import cv2
from matplotlib import pyplot as plt
image = cv2.imread(
'c:/Users/oucbu/Pictures/1.jpg')
chans = cv2.split(image)
colors = (
"b", "g", "r")
plt.figure()
plt.title(
"’Flattened’ Color Histogram")
plt.xlabel(
"Bins")
plt.ylabel(
"# of Pixels")
for (chan, color) in zip(chans, colors):
    hist = cv2.calcHist([chan]
, [0], None, [256], [0, 256])
    plt.plot(hist
, color=color)
    plt.xlim([
0, 256])

 

 

直方图;


import cv2
import numpy as np
from matplotlib import pyplot as plt
image = cv2.imread(
'c:/Users/oucbu/Pictures/1.jpg')
chans = cv2.split(image)
colors = (
"b", "g", "r")

# 2D直方图
fig = plt.figure()
ax = fig.add_subplot(
131)
hist = cv2.calcHist([chans[
1], chans[0]], [0, 1], None, [32, 32], [0, 256, 0, 256])
p = ax.imshow(hist
, interpolation="nearest")
ax.set_title(
"2D Color Histogram for G and B")
plt.colorbar(p)

ax = fig.add_subplot(
132)
hist = cv2.calcHist([chans[
1], chans[2]], [0, 1], None, [32, 32], [0, 256, 0, 256])
p = ax.imshow(hist
, interpolation="nearest")
ax.set_title(
"2D Color Histogram for G and R")
plt.colorbar(p)

ax = fig.add_subplot(
133)
hist = cv2.calcHist([chans[
0], chans[2]], [0, 1], None, [32, 32], [0, 256, 0, 256])
p = ax.imshow(hist
, interpolation="nearest")
ax.set_title(
"2D Color Histogram for B and R")
plt.colorbar(p)


print("2D histogram shape: {}, with {} values".format(hist.shape, hist.flatten().shape[0]))

# 3D直方图
hist = cv2.calcHist([image], [0, 1, 2],
                    None,
[8, 8, 8], [0, 256, 0, 256, 0, 256])
print("3D histogram shape: {}, with {} values".format(
    hist.shape
, hist.flatten().shape[0]))
plt.show()


# 直方图均衡化
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
eq = cv2.equalizeHist(image)
cv2.imshow(
"Histogram Equalization", np.hstack([image, eq]))  # np.hstack:两个图合并一个图,并排显示

7.5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值