第二天openc的内容:图片的缩放、旋转、格式转换

1.图像类型转换:

  1.gray(灰度)————bgr(彩色)

import cv2
img=cv2.imread('D:\\cc1\\lena256.bmp',cv2.IMREAD_UNCHANGED)
img1=cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
cv2.imshow('old',img)
cv2.imshow('new',img1)
print(img.shape)
print(img1.shape)
cv2.waitKey()
cv2.destroyAllWindows()
View Code  

  2.bgr(彩色)------------gray(灰度)

import cv2
img=cv2.imread('D:\\cc1\\lena.bmp',cv2.IMREAD_UNCHANGED)
img1=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('old',img)
cv2.imshow('new',img1)
print(img.shape)
print(img1.shape)
cv2.waitKey()
cv2.destroyAllWindows()
View Code

  3.BGR---------RGB

import cv2
img=cv2.imread('D:\\cc1\\lenacolor.png',cv2.IMREAD_UNCHANGED)
img1=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
cv2.imshow('old',img)
cv2.imshow('new',img1)
print(img.shape)
print(img1.shape)
cv2.waitKey()
cv2.destroyAllWindows()
View Code

2.图像的大小:

  1.使用图像的行数和列数进行缩放:

import cv2
img=cv2.imread('D:\\cc1\\lenacolor.png',cv2.IMREAD_UNCHANGED)
img1=cv2.resize(img,(111,222))# 显示列在是行
cv2.imshow('old',img)
cv2.imshow('new',img1)
print(img.shape)
print(img1.shape)
cv2.waitKey()
cv2.destroyAllWindows()
View Code

  2.使用行数和列数的比列进行缩放:

import cv2
img=cv2.imread('D:\\cc1\\lenacolor.png',cv2.IMREAD_UNCHANGED)
rows,cols=img.shape[:2]
c1=(int(cols*0.5),int(rows*1.2))
print(c1)
img1=cv2.resize(img,(c1))# 显示列在是行
cv2.imshow('old',img)
cv2.imshow('new',img1)
print(img.shape)
print(img1.shape)
cv2.waitKey()
cv2.destroyAllWindows()
View Code

  3.使用fx和fy进行缩放:

import cv2
img=cv2.imread('D:\\cc1\\lenacolor.png',cv2.IMREAD_UNCHANGED)
img1=cv2.resize(img,None,fx=1,fy=0.4)
cv2.imshow('old',img)
cv2.imshow('new',img1)
print(img.shape)
print(img1.shape)
cv2.waitKey()
cv2.destroyAllWindows()
View Code

3图像的反转:

  1.绕x轴反转:

import cv2
img=cv2.imread('D:\\cc1\\lenacolor.png',cv2.IMREAD_UNCHANGED)
img1=cv2.flip(img,0)
cv2.imshow('old',img)
cv2.imshow('new',img1)
print(img.shape)
print(img1.shape)
cv2.waitKey()
cv2.destroyAllWindows()
View Code

  2.绕y周反转

import cv2
img=cv2.imread('D:\\cc1\\lenacolor.png',cv2.IMREAD_UNCHANGED)
img1=cv2.flip(img,1)
cv2.imshow('old',img)
cv2.imshow('new',img1)
print(img.shape)
print(img1.shape)
cv2.waitKey()
cv2.destroyAllWindows()
View Code

  3.绕对称轴旋转:

import cv2
img=cv2.imread('D:\\cc1\\lenacolor.png',cv2.IMREAD_UNCHANGED)
img1=cv2.flip(img,-5)
cv2.imshow('old',img)
cv2.imshow('new',img1)
print(img.shape)
print(img1.shape)
cv2.waitKey()
cv2.destroyAllWindows()
View Code

4.图像的阈值化:

  1.二进制阈值化: 设定一个阈值,如果大于这个值,则将这个值设定为maxvalue,否则设为0

import cv2
img=cv2.imread('D:\\cc1\\lenacolor.png',cv2.IMREAD_UNCHANGED)
R,img1=cv2.threshold(img,123,255,cv2.THRESH_BINARY)
cv2.imshow('old',img)
cv2.imshow('new',img1)
print(img.shape)
print(img1.shape)
cv2.waitKey(-5)
cv2.destroyAllWindows()
View Code

  2.反二进制阈值化: 设定一个阈值,如果小于这个值,则将这个值设定为maxvalue,否则设为0

import cv2
img=cv2.imread('D:\\cc1\\lena256.bmp',cv2.IMREAD_UNCHANGED)
R,img1=cv2.threshold(img,123,255,cv2.THRESH_BINARY_INV)
cv2.imshow('old',img)
cv2.imshow('new',img1)
print(img.shape)
print(img1.shape)
cv2.waitKey(-5)
cv2.destroyAllWindows()
View Code

  3.截断阈值化: 设定一个阈值,如果大于于这个值,则将这个值设定为maxvalue,否则值保持不变

import cv2
img=cv2.imread('D:\\cc1\\lena256.bmp',cv2.IMREAD_UNCHANGED)
R,img1=cv2.threshold(img,123,255,cv2.THRESH_TRUNC)
cv2.imshow('old',img)
cv2.imshow('new',img1)
print(img.shape)
print(img1.shape)
cv2.waitKey(-5)
cv2.destroyAllWindows()
View Code

  4.阈值化为0:设定一个阈值,如果大于于这个值,则将这个值设定为0,否则值保持不变

import cv2
img=cv2.imread('D:\\cc1\\lena256.bmp',cv2.IMREAD_UNCHANGED)
R,img1=cv2.threshold(img,123,255,cv2.THRESH_TOZERO)
cv2.imshow('old',img)
cv2.imshow('new',img1)
print(img.shape)
print(img1.shape)
cv2.waitKey(-5)
cv2.destroyAllWindows()
View Code

  5.反阈值化为0:设定一个阈值,如果小于于这个值,则将这个值设定为0,否则值保持不变

import cv2
img=cv2.imread('D:\\cc1\\lena256.bmp',cv2.IMREAD_UNCHANGED)
R,img1=cv2.threshold(img,123,255,cv2.THRESH_TOZERO_INV)
cv2.imshow('old',img)
cv2.imshow('new',img1)
print(img.shape)
print(img1.shape)
cv2.waitKey(-5)
cv2.destroyAllWindows()
View Code

 

posted @ 2020-04-12 17:11  chown  阅读(362)  评论(0编辑  收藏  举报