python图像操作

均值滤波

result = cv2.blur(source, (5,5))
https://blog.csdn.net/Eastmount/article/details/82216380
 

高斯滤波

#高斯滤波
result = cv2.GaussianBlur(source, (3,3), 0)
https://blog.csdn.net/Eastmount/article/details/82216380
 

中值滤波

import scipy.signal as signal

signal.medfilt(x,3) #中值滤波
https://cloud.tencent.com/developer/article/1098391


import cv2
img_medianBlur=cv2.medianBlur(img01,5)
https://blog.csdn.net/dgut_guangdian/article/details/78378896

np.median 计算中位数
https://www.cnblogs.com/wojianxin/p/12500348.html
 

联合双边滤波

纯python实现
https://niecongchong.github.io/2019/08/29/%E8%81%94%E5%90%88%E5%8F%8C%E8%BE%B9%E6%BB%A4%E6%B3%A2/

dst = cv.ximgproc.jointBilateralFilter(joint,src,33,2,0)
https://www.cnblogs.com/silence-cho/p/11027218.html

C++
https://blog.csdn.net/panda1234lee/article/details/52839205
https://blog.csdn.net/panda1234lee/article/details/52858124
 

双边滤波

lbimg=cv2.bilateralFilter(newimg,3,140,140)
https://blog.csdn.net/jacke121/article/details/79843428

功能说明
双边滤波器可以去除无关噪声,同时保持较好的边缘信息。
但是,其速度比绝大多数滤波器都慢。
关于双边滤波,可以参考:Bilateral Filter
关于2个sigma参数:

简单起见,可以令2个sigma的值相等;
如果他们很小(小于10),那么滤波器几乎没有什么效果;
如果他们很大(大于150),那么滤波器的效果会很强,使图像显得非常卡通化;
关于参数d:

过大的滤波器(d>5)执行效率低。
对于实时应用,建议取d=5;
对于需要过滤严重噪声的离线应用,可取d=9;
d>0时,由d指定邻域直径;
d<=0时,d会自动由sigmaSpace的值确定,且d与sigmaSpace成正比;
https://blog.csdn.net/duwangthefirst/article/details/79971369

空域权重w_d衡量的是 p,q 两点之间的距离,距离越远权重越低;
值域权重w_r衡量的是 p,q两点之间的像素值相似程度,越相似权重越大

在平坦区域,临近像素的像素值的差值较小,对应值域权重w_r接近于1,此时空域权重w_d起主要作用,相当于直接对此区域进行高斯模糊。因此,平坦区域相当于进行高斯模糊。
在边缘区域,临近像素的像素值的差值较大,对应值域权重w_r接近于0,导致此处核函数下降(因w=w_r*w_d),当前像素受到的影响就越小,从而保持了原始图像的边缘的细节信息。
https://blog.csdn.net/guyuealian/article/details/82660826
 

导向滤波

import cv2
import numpy as np

def guideFilter(I, p, winSize, eps, s):
    # 输入图像的高、宽
    h, w = I.shape[:2]

    # 缩小图像
    size = (int(round(w * s)), int(round(h * s)))
    small_I = cv2.resize(I, size, interpolation=cv2.INTER_CUBIC)
    small_p = cv2.resize(I, size, interpolation=cv2.INTER_CUBIC)

    # 缩小滑动窗口
    X = winSize[0]
    small_winSize = (int(round(X * s)), int(round(X * s)))

    # I的均值平滑 p的均值平滑
    mean_small_I = cv2.blur(small_I, small_winSize)
    mean_small_p = cv2.blur(small_p, small_winSize)

    # I*I和I*p的均值平滑
    mean_small_II = cv2.blur(small_I * small_I, small_winSize)
    mean_small_Ip = cv2.blur(small_I * small_p, small_winSize)

    # 方差、协方差
    var_small_I = mean_small_II - mean_small_I * mean_small_I
    cov_small_Ip = mean_small_Ip - mean_small_I * mean_small_p

    small_a = cov_small_Ip / (var_small_I + eps)
    small_b = mean_small_p - small_a * mean_small_I

    # 对a、b进行均值平滑
    mean_small_a = cv2.blur(small_a, small_winSize)
    mean_small_b = cv2.blur(small_b, small_winSize)

    # 放大
    size1 = (w, h)
    mean_a = cv2.resize(mean_small_a, size1, interpolation=cv2.INTER_LINEAR)
    mean_b = cv2.resize(mean_small_b, size1, interpolation=cv2.INTER_LINEAR)

    q = mean_a * I + mean_b

    return q
    
if __name__ == '__main__':
    eps = 0.01
    winSize = (16,16)       #类似卷积核(数字越大,磨皮效果越好)
    image = cv2.imread(r'./5921.png', cv2.IMREAD_ANYCOLOR)
    image = cv2.resize(image,None,fx=0.8,fy=0.8,interpolation=cv2.INTER_CUBIC)
    I = image/255.0       #将图像归一化
    p =I
    s = 3 #步长
    guideFilter_img = guideFilter(I, p, winSize, eps,s)

    # 保存导向滤波结果
    guideFilter_img = guideFilter_img  * 255         #(0,1)->(0,255)
    guideFilter_img[guideFilter_img  > 255] = 255    #防止像素溢出
    guideFilter_img = np.round(guideFilter_img )
    guideFilter_img = guideFilter_img.astype(np.uint8)
    cv2.imshow("image",image)
    cv2.imshow("winSize_16", guideFilter_img )
    cv2.waitKey(0)
    cv2.destroyAllWindows()

I 是导向图像、pp 是输入图像
https://blog.csdn.net/wsp_1138886114/article/details/84228939

原理
https://blog.csdn.net/edogawachia/article/details/78872932
导向滤波比起双边滤波来说在边界附近效果较好
https://blog.csdn.net/edogawachia/article/details/78872932
 

L0平滑

Image Smoothing via L0 Gradient Minimization (SIGGRAPH Asia 2011)

python代码
https://blog.csdn.net/qq_38347905/article/details/77675688

原理
 
https://blog.csdn.net/bluecol/article/details/48750561

2015-ICCV-Fast and Effective L0 Gradient Minimization by Region Fusion


cv2.erode

#OpenCV定义的结构元素
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
 
#腐蚀图像
eroded = cv2.erode(img,kernel)
 
#膨胀图像
dilated = cv2.dilate(img,kernel)


#定义结构元素
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5, 5))
 
#闭运算
closed = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
#显示腐蚀后的图像
cv2.imshow("Close",closed);
 
#开运算
opened = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
#显示腐蚀后的图像
cv2.imshow("Open", opened);

如对一幅二值图连续使用闭运算和开运算,将获得图像中的主要对象。同样,如果想消除图像中的噪声(即图像中的“小点”),也可以对图像先用开运算后用闭运算,不过这样也会消除一些破碎的对象。
https://blog.csdn.net/sunny2038/article/details/9137759

cv2.MORPH_CROSS

cv2.MORPH_ELLIPSE

cv2.MORPH_RECT


水平方向腐蚀
kernel = np.ones((1,3), np.uint8)
img = cv2.erode(img, kernel, iterations=2)
https://blog.csdn.net/Eastmount/article/details/83581277

erode(gray,gray,Mat(1,2,CV_8U,cvScalar(1)),Point(0,0),hDliateNum);
http://blog.sina.com.cn/s/blog_a5b3ed560101ba1s.html
 

图像锐化

    kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32) #锐化
    dst = cv.filter2D(image, -1, kernel=kernel)
https://blog.csdn.net/elegentbeauty/article/details/79849887

#自定义卷积核
kernel_sharpen_1 = np.array([
        [-1,-1,-1],
        [-1,9,-1],
        [-1,-1,-1]])
kernel_sharpen_2 = np.array([
        [1,1,1],
        [1,-7,1],
        [1,1,1]])
kernel_sharpen_3 = np.array([
        [-1,-1,-1,-1,-1],
        [-1,2,2,2,-1],
        [-1,2,8,2,-1],
        [-1,2,2,2,-1], 
        [-1,-1,-1,-1,-1]])/8.0
#卷积
output_1 = cv2.filter2D(image,-1,kernel_sharpen_1)
output_2 = cv2.filter2D(image,-1,kernel_sharpen_2)
output_3 = cv2.filter2D(image,-1,kernel_sharpen_3)
https://blog.csdn.net/Miracle0_0/article/details/82051497
 

透视变换

getPerspectiveTransform

warpPerspective
https://blog.csdn.net/i_chaoren/article/details/78324184
 

cv2.Canny


img = cv2.GaussianBlur(img,(3,3),0)
canny = cv2.Canny(img, 50, 150)
https://blog.csdn.net/sunny2038/article/details/9202641

im = cv2.GaussianBlur(im, (3,3), 0)
canny = cv2.Canny(im, 30, 100)
https://lizonghang.github.io/2016/07/22/Canny%E8%BE%B9%E7%BC%98%E6%A3%80%E6%B5%8B/

edges = cv2.Canny(img,100,200)
https://www.jianshu.com/p/e8e1f2f1c605
 

cv2.resize

interpolation 选项    所用的插值方法
INTER_NEAREST    最近邻插值
INTER_LINEAR    双线性插值(默认设置)
INTER_AREA    使用像素区域关系进行重采样。 它可能是图像抽取的首选方法,因为它会产生无云纹理的结果。 但是当图像缩放时,它类似于INTER_NEAREST方法。
INTER_CUBIC    4x4像素邻域的双三次插值
INTER_LANCZOS4    8x8像素邻域的Lanczos插值

pic = cv2.resize(pic, (1080, 1920), interpolation=cv2.INTER_CUBIC)
https://blog.csdn.net/JNingWei/article/details/78218837

resized = cv2.resize(img, None, fx=0.6, fy=0.6, interpolation=cv2.INTER_AREA)
https://www.cnblogs.com/lfri/p/10596530.html

在 shape 属性中,第1 个值对应的是行数,第2 个值对应的是列数
https://www.jianshu.com/p/a2fac9ef2a67?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

cv.INTER_NEAREST    最近邻插值
cv.INTER_LINEAR    双线性插值
cv.INTER_CUBIC    双线性插值
cv.INTER_AREA    使用像素区域关系重新采样。它可能是图像抽取的首选方法,因为它可以提供无莫尔条纹的结果。但是当图像被缩放时,它类似于INTER_NEAREST方法。

方式一:
dim = (width, height)
resized = cv2.resize(img, dim, interpolation = cv2.INTER_CUBIC)

方式二:
resized = cv2.resize(img, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_LINEAR)
https://www.cnblogs.com/lfri/p/10596530.html

cv2.cvtColor

img = cv2.imread('messi5.jpg', 0)
https://vimsky.com/article/3729.html

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
https://vimsky.com/article/3729.html

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

npy文件读取和保存

import numpy as np

np.save("a.npy", a)
c = np.load( "a.npy" )
https://blog.csdn.net/xiewenbo/article/details/73832027
 

将图像从CV_64F转换为CV_8U

在Python中使用OpenCV将CV_64FC1类型的图像转换为CV_8UC1
arr = np.uint8(image) 
http://cn.voidcc.com/question/p-ojkmmlrf-tq.html
 

图片连接

横向连接
image = np.concatenate((gray1, gray2))

纵向连接
image = np.vstack((gray1, gray2))
https://www.cnblogs.com/ailexy/p/6806622.html
 

检测黑边

利用python,opencv来去除图像的黑边(上下左右都有黑边的)
https://www.cnblogs.com/yumoye/p/10512540.html
 

cv2.imshow

# 顯示圖片
cv2.imshow('My Image', img)

# 按下任意鍵則關閉所有視窗
cv2.waitKey(0)
cv2.destroyAllWindows()

https://blog.gtwang.org/programming/opencv-basic-image-read-and-write-tutorial/
 

PIL.Image.open与cv2.imread格式互转

img = cv2.imread("plane.jpg") # opencv打开的是BRG,是numpy类型
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

https://blog.csdn.net/Gavinmiaoc/article/details/83614833

解决  OSError: [Errno 24] Too many open files

fp = open('./bird.jpg','rb')
pic = Image.open(fp)
pic_array = np.array(pic)  #  Convert to numpy array
fp.close()
pic = Image.fromarray(pic_array) # convert to Image type
https://blog.csdn.net/ruguowoshiyu/article/details/79872997

PIL中的Image和numpy中的数组array

from PIL import Image
Image.fromarray(np.uint8(img))

注意img如果是uint16的矩阵而不转为uint8的话,Image.fromarray这句会报错

File "/usr/local/lib/python2.7/site-packages/PIL/Image.py", line 1884, in fromarray
raise TypeError("Cannot handle this data type")
TypeError: Cannot handle this data type
https://www.jianshu.com/p/18dabefa6778
 

astype改变类型

img = cv2.imread("imori.jpg").astype(np.float)
https://blog.csdn.net/SpadgerZ/article/details/103297962
 

threshold函数

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

选择满足numpy中多个条件的rgb图像像素
mask = np.logical_and.reduce((im[:,:,0]<100,im[:,:,1]<100,im[:,:,2]>100))
im[mask] # returns all pixels satisfying these conditions
https://www.soinside.com/question/5MqZXtaM8jv54RPrqM9jFQ
mask = ((r["dt"] >= startdate) & (r["dt"] <= enddate))
selected = r[mask]
https://codeday.me/bug/20170720/43394.html
 

psnr计算

def cal_psnr(im1, im2):
    mse = (np.abs(im1 - im2) ** 2).mean()
    psnr = 10 * np.log10(255 * 255 / mse)
    return psnr
https://www.aiuai.cn/aifarm1245.html

def psnr(im1, im2):
    mse = (np.abs(im1 - im2) ** 2).mean()
    if mse == 0:
        return 100
    psnr = 10 * np.log10(255 * 255 / mse)
    return psnr


官网
https://github.com/scikit-image/scikit-image/blob/v0.12.2/skimage/measure/simple_metrics.py#L94
 

cv2.rectangle

# D·C 191119:绘制装载文字的矩形实心框
cv2.rectangle(image, c1, (c1[0] + t_size[0], c1[1] - t_size[1] - 3), bbox_color, thickness=-1)  # filled
https://blog.csdn.net/Dontla/article/details/103137391
 

cv2.copyMakeBorder 加黑边

cv2.copyMakeBorder(src, top, bottom, left, right, borderType)函数

src : 输入的图片
top, bottom, left, right :相应方向上的边框宽度
borderType:定义要添加边框的类型
https://blog.csdn.net/qq_36560894/article/details/105416273

img6 = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_CONSTANT)
#添加红色边界
#RED = [255,0,0]
#img6 = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_CONSTANT,value=RED)
https://blog.csdn.net/weixin_35732969/article/details/83714492
 

tiff格式保存

我还发现cv2.imwrite可以写16位,三通道TIFF文件。

>>> cv2.imwrite('out.tif', im)
https://cloud.tencent.com/developer/ask/52680
 

你可能感兴趣的:(python,python,开发语言,后端)