-
-
Save stoensin/765fe728671b5ca0ac6f21738820e3a6 to your computer and use it in GitHub Desktop.
opencv图像常规修改
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 步骤:灰度图,高斯滤波,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) | |
#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) | |
# style transform 风格转换 | |
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import cv2 | |
img = cv2.imread('X.jpg',0) | |
cv2.imshow('image',img) | |
k = cv2.waitKey(0) | |
k = cv2.waitKey(0) & 0xFF # 64位机器 | |
if k == 27: # 按下esc时,退出 | |
cv2.destroyAllWindows() | |
elif k == ord('s'): # 按下s键时保存并退出 | |
cv2.imwrite('M.png',img) | |
cv2.destroyAllWindows() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#缩放1 | |
import cv2 | |
img=cv2.imread('E:/python_cv/01.jpg',1) | |
img_info=img.shape | |
print(img_info) | |
image_height=img_info[0] | |
image_weight=img_info[1] | |
image_mode=img_info[2] | |
dstHeight=int(0.5*image_height) | |
dstWeight=int(0.5*image_weight) | |
dst=cv2.resize(img,(dstHeight,dstWeight)) | |
cv2.imshow('image_dst',dst) | |
cv2.waitKey(0) | |
#缩放2 | |
import cv2 | |
import numpy as np | |
img=cv2.imread('E:/python_cv/01.jpg',1) | |
img_info=img.shape | |
print(img_info) | |
image_height=img_info[0] | |
image_weight=img_info[1] | |
image_mode=img_info[2] | |
dstHeight=int(0.5*image_height) | |
dstWeight=int(0.5*image_weight) | |
dst=np.zeros((dstHeight,dstWeight,3),np.uint8) #0-255 | |
for i in range(dstHeight): | |
for j in range(dstWeight): | |
img_i=int(i*(image_height)/dstHeight) | |
img_j=int(j*(image_weight)/dstWeight) | |
dst[i,j]=img[img_i,img_j] | |
cv2.imshow('image_dst',dst) | |
cv2.waitKey(0) | |
#缩放3 | |
import cv2 | |
import numpy as np | |
img=cv2.imread('E:/python_cv/01.jpg',1) | |
img_info=img.shape | |
print(img_info) | |
image_height=img_info[0] | |
image_weight=img_info[1] | |
image_mode=img_info[2] | |
mat_scale=np.float32([[0.5,0,0],[0,0.5,0]]) | |
dst=cv2.warpAffine(img,mat_scale,(int(image_height/2),int(image_weight/2))) | |
cv2.imshow('image_dst',dst) | |
cv2.waitKey(0) | |
#图像剪切(x-->100到200,y-->100到300 | |
import cv2 | |
import numpy as np | |
img=cv2.imread('E:/python_cv/01.jpg',1) | |
img_info=img.shape | |
dst=img[100:200,100:300] | |
cv2.imshow('image_dst',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] | |
cv2.imshow('image_src',img) | |
#移位矩阵 | |
mat_shift=np.float32([[1,0,100],[0,1,200]]) | |
dst=cv2.warpAffine(img,mat_shift,(image_height,image_weight)) | |
cv2.imshow('image_dst',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] | |
cv2.imshow('image_src',img) | |
dst=np.zeros(img.shape,np.uint8) | |
for i in range(image_height-200): | |
for j in range(image_weight-100): | |
dst[i+200,j+100]=img[i,j] | |
cv2.imshow('image_dst',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] | |
cv2.imshow('image_src',img) | |
dst=np.zeros(img.shape,np.uint8) | |
for i in range(image_height): | |
for j in range(image_weight): | |
dst[i,j]=img[image_height-i-1,j] | |
cv2.imshow('image_dst',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] | |
#src3-->dst3(左上角,左下角,右上角) | |
mat_src=np.float32([[0,0],[0,image_height-1],[image_weight-1,0]]) | |
mat_dst=np.float32([[50,50],[300,image_height-200],[image_weight-300,100]]) | |
mat_Affine=cv2.getAffineTransform(mat_src,mat_dst) | |
dst=cv2.warpAffine(img,mat_Affine,(image_height,image_weight)) | |
cv2.imshow('image_dst',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] | |
mat_rotate=cv2.getRotationMatrix2D((image_height*0.5,image_weight*0.5),45,1) #center angle 3scale | |
dst=cv2.warpAffine(img,mat_rotate,(image_height,image_weight)) | |
cv2.imshow('image_dst',dst) | |
cv2.waitKey(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#方式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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment