前言
我前年看过了《Opencv3编程入门》,当时我下的是Opencv4.0.1 , 所以有些代码用不了就改成了适合早期4.0的代码了,不知道后面的版本的使用方法变了没。
我只记得概念特别多,我也看不懂,尽管笔记都做出来了。后面也没有项目经验,就都忘了。也是因为学的不好,不知道该怎么实践。你说磨皮,非线性滤波和高斯模糊这种东西,这就调用个函数的事,不屑于去做,但是物体识别也不懂,特征不知道怎么找
图像加载显示
import numpy as np
from sklearn import preprocessing
from sklearn.preprocessing import Normalizer
from sklearn.preprocessing import FunctionTransformer
from sklearn.preprocessing import Binarizer
from sklearn.preprocessing import PolynomialFeatures
from sklearn.preprocessing import StandardScaler
import pandas as pd
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
from fancyimpute import KNN
from sklearn.covariance import EllipticEnvelope
from sklearn.preprocessing import LabelBinarizer, MultiLabelBinarizer
from sklearn.feature_extraction import DictVectorizer
from sklearn.neighbors import KNeighborsClassifier
import cv2
from matplotlib import pyplot as plt
print("cv version : ",cv2.__version__)
"""
cv2.IMREAD_COLOR 彩色BGR
cv2.IMREAD_GRAYSCALE 灰度
cv2.IMREAD_UNCHANGED 原图BGRA
"""
src_image=cv2.imread("ntr.jpg",cv2.IMREAD_COLOR)
h,w=src_image.shape[:2]
dst_image = cv2.resize(src_image,(int(w/2),int(h/2)))
print(type(src_image))
print(src_image.shape)
cv2.imshow("src",src_image)
cv2.imshow("dst",dst_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv version : 4.5.4-dev
<class 'numpy.ndarray'>
(797, 564, 3)
单个点的像素值
src_image=cv2.imread("ntr.jpg",cv2.IMREAD_COLOR)
print(src_image.shape)
print(src_image[0,0])
print('--------------------------------------------------------')
src_image2=cv2.imread("sister.png",cv2.IMREAD_UNCHANGED)
print(src_image2.shape)
print(src_image2[0,0])
print('--------------------------------------------------------')
h,w=src_image2.shape[:2]
print(src_image2[int(w/2),int(h/2)])
cv2.imshow("ntr.jpg",src_image)
cv2.imshow("sister.png",src_image2)
cv2.waitKey(0)
cv2.destroyAllWindows()
(797, 564, 3)
[158 148 148]
--------------------------------------------------------
(518, 541, 4)
[255 255 255 0]
--------------------------------------------------------
[229 248 253 255]
- sister.png是用PS扣过背景的透明图
- 不扣背景,只是用格式工厂转成png的话,还是没用Alpha通道,只是BGR三个通道
cvtColor色彩空间转换
src_image=cv2.imread("yes.png",cv2.IMREAD_COLOR)
"""
cv2.COLOR_BGR2GRAY
cv2.COLOR_RGB2GRAY
cv2.COLOR_GRAY2BGR
cv2.COLOR_GRAY2RGB
cv2.COLOR_BGR2RGB
cv2.COLOR_BGR2HSV
cv2.COLOR_RGB2HSV
cv2.COLOR_HSV2BGR
cv2.COLOR_HSV2RGB
cv2.COLOR_BGR2YUV
cv2.COLOR_RGB2YUV
cv2.COLOR_YUV2BGR
cv2.COLOR_YUV2RGB
"""
dst_image=cv2.cvtColor(src_image,cv2.COLOR_BGR2RGB)
plt.subplot(1,2,1)
plt.imshow(src_image)
plt.subplot(1,2,2)
plt.imshow(dst_image)
plt.show()
裁剪
img1=cv2.imread("cute.jpg",cv2.IMREAD_COLOR)
h,w=img1.shape[:2]
img2=img1[:,: int(w/2)] #裁掉右边一半
img3=img1[: int(h/2),:] #裁掉下面一半
cv2.imshow("1",img1)
cv2.imshow("2",img2)
cv2.imshow("3",img3)
cv2.waitKey(0)
cv2.destroyAllWindows()
线性滤波
boxFilter 方框滤波
img1=cv2.imread("fox.jpg",cv2.IMREAD_COLOR)
h,w=img1.shape[:2]
"""
boxFilter的卷积核
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
"""
img2=cv2.boxFilter(img1,
-1,
(5,5),
True
)
cv2.imshow("1",img1)
cv2.imshow("2",img2)
cv2.waitKey(0)
cv2.destroyAllWindows()
blur 均值滤波
img1=cv2.imread("stare.jpg",cv2.IMREAD_COLOR)
h,w=img1.shape[:2]
"""
blur的卷积核
0 0 1 0 0
0 1 1 1 0
1 1 1 1 1
0 1 1 1 0
0 0 1 0 0
"""
img2=cv2.blur(img1,
(5,5),
)
cv2.imshow("1",img1)
cv2.imshow("2",img2)
cv2.waitKey(0)
cv2.destroyAllWindows()
GaussianBlur 高斯滤波
img1=cv2.imread("happy.jpg",cv2.IMREAD_COLOR)
h,w=img1.shape[:2]
img2=cv2.GaussianBlur(img1,
(5,5),
0.8 ,
0.8
)
cv2.imshow("1",img1)
cv2.imshow("2",img2)
cv2.waitKey(0)
cv2.destroyAllWindows()
filter2D 自定义核
img1=cv2.imread("my.jpg",cv2.IMREAD_COLOR)
h,w=img1.shape[:2]
kernel=np.ones((5,5))/5
print(kernel)
img2=cv2.filter2D(img1,-1,kernel)
cv2.imshow("1",img1)
cv2.imshow("2",img2)
cv2.waitKey(0)
cv2.destroyAllWindows()
核为 np.ones((5,5))/100
核为 np.ones((5,5))/25
核为 np.ones((5,5))/5
|