0
点赞
收藏
分享

微信扫一扫

Python opencv 图像特效处理

最近处理视觉相关的项目,分享一下通过opencv的图像特效处理,

原图镇楼

Python opencv 图像特效处理_灰度

1、灰度处理

将cv2.imread()方法的第二参数设为0即可得到灰色图像。

import cv2img0 = cv2.imread('1.jpg', 0)img1 = cv2.imread('1.jpg', 1)print(img0.shape)print(img1.shape)cv2.imshow('img0', img0)cv2.waitKey(0)

Python opencv 图像特效处理_彩色图像_02

通过​​cv2.cvtColor​​方法对图像进行灰度转换

​cv2.cvtColor(src, code, dst, dstCN):​

-src: 目标图像-code: 颜色转换方式-dst: 图像大小-dstCN: 颜色通道大小

import cv2img = cv2.imread('1.jpg', 1)dat = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY )cv2.imshow('dat', dat)cv2.waitKey(0)

Python opencv 图像特效处理_图像融合_03

灰色图像的所有颜色通道的值相等,所以要想将彩色图像变为灰色图像,只需将他们颜色通道的值相等即可。

通过设置图片的颜色均值来使彩色图像变为灰色图像

import cv2import numpy as npimg = cv2.imread('1.jpg', 1)dat = np.zeros((img.shape[0], img.shape[1]), np.uint8)for i in range(0, img.shape[0]):    for j in range(0, img.shape[1]):        (b, g, r) = img[i, j]        gray = (int(b) + int(g) + int(r)) / 3        dat[i, j] = np.uint(gray)cv2.imshow('dat', dat)cv2.waitKey(0)

Python opencv 图像特效处理_图像融合_04

2、颜色反转

将图像的颜色反转也就是让255减去当前的颜色值

import cv2import numpy as npimg = cv2.imread('1.jpg', 1)gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)dat = np.zeros((img.shape[0], img.shape[1]), np.uint8)for i in range(gray.shape[0]):    for j in range(gray.shape[1]):        dat[i, j] = 255 - gray[i, j]cv2.imshow('dat', dat)cv2.waitKey(0)

Python opencv 图像特效处理_彩色图像_05

彩色图像的颜色反转也是一样的道理

import cv2import numpy as npimg = cv2.imread('1.jpg', 1)
dat = np.zeros((img.shape[0], img.shape[1], img.shape[2]), np.uint8)for i in range(img.shape[0]): for j in range(img.shape[1]): (b, g, r) = img[i, j] dat[i, j] = (255 - b, 255 - g, 255 - r)cv2.imshow('src', img)cv2.imshow('dat', dat)cv2.waitKey(0)

Python opencv 图像特效处理_图像融合_06

3、马赛克

原理: 取一个指定大小的窗口,将该窗口填充为一个颜色

下面将窗口设置为10*10来生成一个马赛克图像

import cv2img = cv2.imread('1.jpg', 1)height = img.shape[0]width = img.shape[1]for m in range(100, 200):for n in range(200, 300):if m % 10 == 0 and n % 10 == 0:for i in range(10):    for j in range(10):b, g, r)        img[m + i, n + j] = (b, g, r)cv2.imshow('img', img)cv2.waitKey(0)

Python opencv 图像特效处理_彩色图像_07

4、毛玻璃效果

原理: 将当前的像素颜色随机设置为窗口中的一个颜色

这里设置窗口为8*8来生成一个毛玻璃图像

import cv2import randomimport numpy as npimg = cv2.imread('1.jpg', 1)height = img.shape[0]width = img.shape[1]dat = np.zeros(img.shape, np.uint8)for m in range(height - 8):    for n in range(width - 8):        index = int(random.random() * 8)        (b, g, r) = img[m + index, n + index]        dat[m, n] = (b, g, r)cv2.imshow('dat', dat)cv2.waitKey(0)

Python opencv 图像特效处理_灰度_08

5、图像融合

图片乘以比例系数相加得到俩张图片的融合效果,公式如下

target = src1 * a +src2 * (1 - a)cv2.addWeighted(src1, alpha, src2, beta, gamma):

import cv2import numpy as npimg1 = cv2.imread('1.jpg', 1)img2 = cv2.imread("2.jpg", 1)img3 = cv2.resize(img2, (img1.shape[1], img1.shape[0]), interpolatinotallow=cv2.INTER_AREA)dat = np.zeros(img1.shape, np.uint8)dat = cv2.addWeighted(img1, 0.8, img3, 0.2, 0)
cv2.imshow('dat', dat)cv2.waitKey(0)

Python opencv 图像特效处理_图像融合_09



举报

相关推荐

0 条评论