python+opencv+图像特效(图像灰度处理、颜色翻转、图片融合,边缘检测,浮雕效果,颜色映射)
程序员文章站
2024-01-28 09:39:34
...
原图
图像灰度处理
#方式1
import cv2
#读取彩色原图
img0=cv2.imread('E:/python_cv/01.jpg',1)
#读取灰度图
img1=cv2.imread('E:/python_cv/01.jpg',0)
print(img0.shape)
print(img1.shape)
cv2.imshow('gary',img1)
cv2.waitKey(0)
#方式2
import cv2
#读取彩色原图
src=cv2.imread('E:/python_cv/01.jpg',1)
dst=cv2.cvtColor(src,cv2.COLOR_BGR2GRAY)
cv2.imshow('gary',dst)
cv2.waitKey(0)
#方式3
import cv2
import numpy as np
img=cv2.imread('E:/python_cv/01.jpg',1)
img_info=img.shape
image_height=img_info[0]
image_weight=img_info[1]
dst=np.zeros((image_height,image_weight,3),np.uint8)
#当彩色图像三个通道的值都相同时,即为灰度图像
for i in range(image_height):
for j in range(image_weight):
(b,g,r)=img[i][j]
gray=(int(b)+int(g)+int(r))/3 #防止越界,转换类型
dst[i,j]=np.uint8(gray)
cv2.imshow('gary',dst)
cv2.waitKey(0)
#方式4
import cv2
import numpy as np
img=cv2.imread('E:/python_cv/01.jpg',1)
img_info=img.shape
image_height=img_info[0]
image_weight=img_info[1]
dst=np.zeros((image_height,image_weight,3),np.uint8)
#当彩色图像三个通道的值都相同时,即为灰度图像
for i in range(image_height):
for j in range(image_weight):
(b,g,r)=img[i][j]
gray=0.299*int(b)+0.587*int(g)+0.114*int(r)
dst[i,j]=np.uint8(gray)
cv2.imshow('gary',dst)
cv2.waitKey(0)
灰度处理很重要,也是图像处理中的基础操作,在实际运用中要求实时性,涉及优化,比如:
- 定点操作比浮点操作要快
- +-比*/快,移位比*/快
import cv2
import numpy as np
img=cv2.imread('E:/python_cv/01.jpg',1)
img_info=img.shape
image_height=img_info[0]
image_weight=img_info[1]
dst=np.zeros((image_height,image_weight,3),np.uint8)
#当彩色图像三个通道的值都相同时,即为灰度图像
for i in range(image_height):
for j in range(image_weight):
(b,g,r)=img[i][j]
# gray=0.299*int(b)+0.587*int(g)+0.114*int(r)
#优化
b=int(b)
g=int(g)
r=int(r)
# gray=(b+2*g+r)/4
gray=(b+(g<<1)+r)>>2
dst[i,j]=np.uint8(gray)
cv2.imshow('gary',dst)
cv2.waitKey(0)
颜色翻转
灰度图像素值为0到255,若当前的像素值为i,翻转过后为255-i
import cv2
import numpy as np
#读取彩色原图
src=cv2.imread('E:/python_cv/01.jpg',1)
gray=cv2.cvtColor(src,cv2.COLOR_BGR2GRAY)
img_info=src.shape
image_height=img_info[0]
image_weight=img_info[1]
dst=np.zeros((image_height,image_weight,1),np.uint8)
for i in range(image_height):
for j in range(image_weight):
grayPixel=gray[i][j]
dst[i][j]=255-grayPixel
cv2.imshow('gary',dst)
cv2.waitKey(0)
原灰度图 | 反转灰度图 |
彩色图反转
import cv2
import numpy as np
#读取彩色原图
src=cv2.imread('E:/python_cv/01.jpg',1)
img_info=src.shape
image_height=img_info[0]
image_weight=img_info[1]
dst=np.zeros((image_height,image_weight,3),np.uint8)
for i in range(image_height):
for j in range(image_weight):
(b,g,r)=src[i][j]
dst[i][j]=(255-b,255-g,255-r)
cv2.imshow('src',src)
cv2.imshow('dst',dst)
cv2.waitKey(0)
彩色图 | 反转图 |
图片融合
import cv2
import numpy as np
#读取彩色原图
src=cv2.imread('E:/python_cv/01.jpg',1)
src1=cv2.imread('E:/python_cv/02.jpg',1)
img_info=src.shape
image_height=img_info[0]
image_weight=img_info[1]
roi_h=int(image_height/2)
roi_w=int(image_weight/2)
src_roi=src[0:roi_h,0:roi_w]
src1_roi=src1[0:roi_h,0:roi_w]
dst=np.zeros((roi_h,roi_w,3),np.uint8)
dst=cv2.addWeighted(src_roi,0.5,src1_roi,0.5,0)
cv2.imshow('dst',dst)
cv2.waitKey(0)
canny 边缘检测
步骤:灰度图,高斯滤波,canny
import cv2
import numpy as np
gray=cv2.imread('E:/python_cv/01.jpg',0)
img=cv2.GaussianBlur(gray,(3,3),0)
dst=cv2.Canny(img,50,50)
cv2.imshow('gray',gray)
cv2.imshow('gauss',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)
灰度图 | 高斯滤波 | canny |
soble边缘检测
import cv2
import numpy as np
import math
gray=cv2.imread('E:/python_cv/01.jpg',0)
imgInfo=gray.shape
height=imgInfo[0]
weight=imgInfo[1]
cv2.imshow('src',gray)
dst=np.zeros((height,weight,1),np.uint8)
'''
sobel
1.算子模板 2.图像卷积 3.阈值判决
竖直模板 水平模板
[1 2 1 [1 0 -1
0 0 0 2 0 -2
-1-2-1] 1 0 -1]
'''
for i in range(0,height-2):
for j in range(0,weight-2):
gy=gray[i,j]*1+gray[i,j+1]*2+gray[i,j+2]*1-gray[i+2,j]-2*gray[i+2,j+1]-gray[i+2,j+2]*1
gx=gray[i,j]*1-gray[i,j+2]+gray[i+1,j]*2-2*gray[i+1,j+2]+gray[i+2,j]-gray[i+2,j+2]
grad=math.sqrt(gx*gx+gy*gy)
if grad>50:
dst[i,j]=255
else:
dst[i,j]=0
cv2.imshow('dst',dst)
cv2.waitKey(0)
原图 | sobel |
import cv2
import numpy as np
gray=cv2.imread('E:/python_cv/01.jpg',0)
imgInfo=gray.shape
height=imgInfo[0]
weight=imgInfo[1]
#浮雕效果
dst=np.zeros((height,weight,1),np.uint8)
for i in range(0,height):
for j in range(0,weight-1):
gray0=gray[i,j]
gray1=gray[i,j+1]
newp=gray0-gray1+150
if newp>255:
newp=255
else:
newp=0
dst[i,j]=newp
cv2.imshow('dst',dst)
cv2.waitKey(0)
原图 | 浮雕效果 |
|
风格转换
import cv2
import numpy as np
img=cv2.imread('E:/python_cv/01.jpg',1)
imgInfo=img.shape
height=imgInfo[0]
weight=imgInfo[1]
cv2.imshow('src',img)
'''
颜色风格,rgb-->RGB
b=b*1.5
g=g*1.3
'''
dst=np.zeros((height,weight,3),np.uint8)
for i in range(0,height):
for j in range(0,weight):
(b,g,r)=img[i,j]
b=1.5*b
g=1.3*g
if b>255:
b=255
if g>255:
g=255
dst[i,j]=(b,g,r)
cv2.imshow('dst',dst)
cv2.waitKey(0)
原图 | 颜色风格 |