python openCv(三)图像操作信息

import cv2
from skimage import morphology
import numpy as np 
from matplotlib import pyplot as plt

def cv_show(name , img):
            cv2.imshow(name,img)
            cv2.waitKey(0)           #等待时间,毫秒级
            # cv2.destroyAllWindow()    
# cv2.IMREAD_COLOR: 以彩色模式读取(默认)
# cv2.IMREAD_GRAYSCALE: 以灰色图模式读取
# cv2.IMREAD_UNCHANGED: 加载图像包含alpha通道
# 这三个参数可以分别用 1、0、-1 简化表示


img_cat = cv2.imread('cats.png')
img_dog = cv2.imread('dog.png')
img = cv2.imread('cats.png', cv2.IMREAD_UNCHANGED)

img = cv2.imread('cats.png', -1)
# cv_show("color",img_cat)
print(img.dtype)

from PIL import Image

# img = Image.open("cats.png")
# print(img.mode)
#  灰度化COLOR_BGR2GRAY
img_cat = cv2.imread("cats.png")
gray = cv2.cvtColor(img_cat,cv2.COLOR_RGB2GRAY)

	# 图像二值化函数cv2.threshold函数的介绍。
	# 图像的二值化,就是将图像上的像素点的灰度值设置为0或255,也就是将整个图像呈现出明显的只有黑和白的视觉效果(灰度值0:黑,灰度值255:白).
    # 在图像中除了目标物体和背景区域,还有噪声,这都会对于我们对图像的识别造成困扰,所以我们要通过图像二值化函数将多值的数字图像中直接提取出目标图像,也就是说设定一个阈值T,用T将图像的象素群一分为二。

import cv2

image = cv2.imread('cats.png')

gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
ret, binary = cv2.threshold(gray, 175, 255, cv2.THRESH_BINARY)
ret1, binaryinv = cv2.threshold(gray, 175, 255, cv2.THRESH_BINARY_INV)
ret2, trunc = cv2.threshold(gray, 175, 255, cv2.THRESH_TRUNC)
ret3, tozero = cv2.threshold(gray, 175, 255, cv2.THRESH_TOZERO)
ret4, tozeroinv = cv2.threshold(gray, 175, 255, cv2.THRESH_TOZERO_INV)
"""上面代码的作用是,将灰度图img2gray中灰度值小于175的点置0,灰度值大于175的点置255"""
cv2.imshow('original', image)
cv2.imshow('gray',gray)
cv2.imshow('binary', binary)
cv2.imshow('binaryinv', binaryinv)
cv2.imshow('trunc', trunc)
cv2.imshow('tozero', tozero)
cv2.imshow('tozeroinv', tozeroinv)

cv2.waitKey(0)

img = cv2.imread('cats.png',0)
edges = cv2.Canny(img, 100, 255)

# cv2.imshow('Edges',edges)
# cv2.waitKey(0)


# # 先腐蚀在膨胀操作从而消除噪声信息
kernel = np.ones((10,10),np.uint8)  
# kernal = np.zeros((3,3),dtype=uint8)
edges = cv2.erode(edges,kernel=kernel,iterations=1)
# pirnt(edges)
kernel = np.ones((10,10),np.uint8)  
# kernal = np.zeros((3,3),dtype=uint8)
edges = cv2.dilate(edges,kernel=kernel,iterations=1)

cv2.imshow("bianyuan",edges)
cv2.waitKey(0)
# # 获取直接使用
# dst = cv2.morphologyEx(src,op,kernerl[,anchor[,iterations,[,borderType[,borderValue]]]])
dts = cv2.morphologyEx(edges,cv2.MORPH_CLOSE,kernel)

cv2.imshow("bianyuan",dts)
cv2.waitKey(0)


import cv2
import numpy as np

higher_reso = cv2.imread('cats.png')
cv2.imshow('yuantu ', higher_reso)
# 函数 cv2.pyrDown() 从一个 分辨率大尺寸的图像向上构建一个金子塔
# 尺寸变小 分辨率降低 。
lower_reso = cv2.pyrDown(higher_reso)
cv2.imshow('lower_reso', lower_reso)

# 从一个低分 率小尺寸的图像向下构建一个子塔 尺寸变大 但分 率不会增加
higher_reso2 = cv2.pyrUp(lower_reso)
cv2.imshow('higher_reso2', higher_reso2)

# 你  住的是是 higher_reso2 和 higher_reso 是不同的。因为一旦使 用 cv2.pyrDown() 图像的分辨率就会 低 ,信息就会 丢失
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np

higher_reso = cv2.imread('cats.png')
# cv2.imshow('yuantu ', higher_reso)
for index in range(3):
    img = cv2.pyrDown(higher_reso)
    higher_reso = img
    cv2.imshow("img"+str(index),img)
# 可以获取不同层次的信息

# 你  住的是是 higher_reso2 和 higher_reso 是不同的。因为一旦使 用 cv2.pyrDown() 图像的分辨率就会 低 ,信息就会 丢失
cv2.waitKey(0)
cv2.destroyAllWindows()

# 目标
# • 学习使用非局部平均值去噪算法去除图像中的噪声
# • 学习函数 cv2.fastNlMeansDenoising() cv2.fastNlMeansDenoisingColored() 等

# 噪声是平均值为1的随机变量
# 对于彩色图像  先转换到 CIELAB 颜色空间 然后对 L 和 AB 成分 分别去噪
# OpenCV 中的图像去噪
# 1. cv2.fastNlMeansDenoising() 使用对象为灰度图。
# 2. cv2.fastNlMeansDenoisingColored() 使用对象为彩色图。
# 3. cv2.fastNlMeansDenoisingMulti() 用于短时 的图像序列 灰度图像
# 4. cv2.fastNlMeansDenoisingColoredMulti()  用于短时 的图 像序列 彩色图像

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('cats.png')
img = cv2.cvtColor(img, code=cv2.COLOR_BGR2RGB)

dst = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)
dst2=cv2.cvtColor(dst,code=cv2.COLOR_BGR2RGB)

plt.subplot(121), plt.imshow(img)
plt.subplot(122), plt.imshow(dst)
plt.subplot(122), plt.imshow(dst2)
plt.show()

import numpy as np
import cv2
from matplotlib import pyplot as plt

cap = cv2.VideoCapture('../data/vtest.avi')
# create a list of first 5 frames
img = [cap.read()[1] for i in range(5)]
# convert all to grayscale
gray = [cv2.cvtColor(i, cv2.COLOR_BGR2GRAY) for i in img]
# convert all to float64
gray = [np.float64(i) for i in gray]



# create a noise of variance 25
noise = np.random.randn(*gray[1].shape) * 10
# Add this noise to images
noisy = [i + noise for i in gray]
# Convert back to uint8
noisy = [np.uint8(np.clip(i, 0, 255)) for i in noisy]
# Denoise 3rd frame considering all the 5 frames
dst = cv2.fastNlMeansDenoisingMulti(noisy, 2, 5, None, 4, 7, 35)
plt.subplot(131), plt.imshow(gray[2], 'gray')
plt.subplot(132), plt.imshow(noisy[2], 'gray')
plt.subplot(133), plt.imshow(dst, 'gray')
plt.show()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值