PYOpenCV学习笔记

OpenCV学习笔记

  • 001 图像读写
  • 002 颜色空间转换
  • 003 图像对象的创建与赋值
  • 004 图像像素遍历
  • 005 图像的算术运算操作(加减乘除)
  • 006 Look Up Table(LUT)查找表

001 图像读写

读取: src = cv.imread(“文件路径”)
写入: result = cv.imwrite(“文件路径”,src)#src 是要写入的图片,result是bool型表示是否写入成功

import cv2 as cv
src = cv.imread("../miao.bmp")
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
cv.imshow("input", src)
cv.cvtColor(src, cv.COLOR_BGR2RGB, src)
print(cv.imwrite("../miao-1.bmp",src))
cv.imshow("output", src)
cv.waitKey(0)
cv.destroyAllWindows()

002 颜色空间转换

函数:
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
色彩空间转换函数- cvtColor
COLOR_BGR2GRAY = 6 彩色到灰度
COLOR_GRAY2BGR = 8 灰度到彩色
COLOR_BGR2HSV = 40 BGR到HSV
COLOR_HSV2BGR = 54 HSV到 BGR

import cv2 as cv
src = cv.imread("../miao.bmp")
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
cv.imshow("input", src)
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
cv.imshow("gray", gray)
cv.waitKey(0)
cv.destroyAllWindows()
"""
图像保存 - imwrite
第一个参数是图像保存路径
第二个参数是图像内存对象
"""
cv.imwrite("miao_gray.bmp",gray)

效果:
PYOpenCV学习笔记_第1张图片PYOpenCV学习笔记_第2张图片

003 图像对象的创建与赋值

创建灰度图像:
m4 = np.zeros([512,512], np.uint8)
创建三通道彩色图像:
m5 = np.ones(shape=[512,512,3], dtype=np.uint8)
克隆图像(左侧对象和右侧对象占用不同的内存块,但内容相同):
m1 = np.copy(src)
赋值(左侧对象和右侧对象占用相同的内存块,任改其一,两者都要变):
m2 = src
图像像素区域赋值:
src[100:200,200:300,:] = 255

import cv2 as cv
import numpy as np

src = cv.imread("../miao.bmp")
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
cv.imshow("input", src)
print(id(src))
print(type(src))
print(src)
cv.waitKey(0)

# 克隆图像
m1 = np.copy(src)
print("m1 is "+str(id(m1)))

# 赋值
m2 = src
src[100:200,200:300,:] = 255
cv.imshow("m2",m2)
print("m2 is "+str(id(m2)))
print("src is "+str(id(src)))

m3 = np.zeros(src.shape, src.dtype)
cv.imshow("m3", m3)

m4 = np.zeros([512,512], np.uint8)
# m4[:,:] =127 try to give gray value 127
cv.imshow("m4", m4)

m5 = np.ones(shape=[512,512,3], dtype=np.uint8)
#m5[:,:,0] = 255
cv.imshow("m5", m5)

print(m4)
print(m5)

cv.waitKey(0)
cv.destroyAllWindows()

004 图像像素遍历

利用for循环进行整幅图像遍历:
import cv2 as cv
import numpy as np

src = cv.imread("../miao.bmp")
out = np.zeros(src.shape, src.dtype)
h, w, ch = src.shape
#像素遍历
for row in range(h):
    for col in range(w):
        b, g, r = src[row,col]
        out[row,col] = 255-b, 255-g, 255-r
#显示图像
cv.imshow("src", src)
cv.imshow("out", out)
cv.waitKey(0)
cv.destroyAllWindows()

效果:
PYOpenCV学习笔记_第3张图片

005 图像的算术运算操作(加减乘除)

加: cv.add(src1, src2, add_result);
减: cv.subtract(src1, src2, sub_result);
乘: cv.multiply(src1, src2, mul_result);
除: cv.divide(src1, src2, div_result);

import cv2 as cv
import numpy as np

src1 = cv.imread("../tao1.bmp");
src2 = cv.imread("../miao1.bmp");
cv.imshow("input1", src1)
cv.imshow("input2", src2)
h, w, ch = src1.shape
print("h , w, ch", h, w, ch)

add_result = np.zeros(src1.shape, src1.dtype);
cv.add(src1, src2, add_result);
cv.imshow("add_result", add_result);

sub_result = np.zeros(src1.shape, src1.dtype);
cv.subtract(src1, src2, sub_result);
cv.imshow("sub_result", sub_result);

mul_result = np.zeros(src1.shape, src1.dtype);
cv.multiply(src1, src2, mul_result);
cv.imshow("mul_result", mul_result);

div_result = np.zeros(src1.shape, src1.dtype);
cv.divide(src1, src2, div_result);
cv.imshow("div_result", div_result);

cv.waitKey(0)
cv.destroyAllWindows()

006 Look Up Table(LUT)查找表

PYOpenCV学习笔记_第4张图片

  • applyColorMap(src, dst, COLORMAP)

  • src 表示输入图像

  • dst表示输出图像

  • 匹配到的颜色LUT, OpenCV支持13种颜色风格的查找表映射
    PYOpenCV学习笔记_第5张图片系统自带风格及自定义灰度图像查找表:

      import cv2 as cv
      
      def pe_grayLut(image, mode):
          lut = [0]*256
          if (mode==1):
              for i in range(256):#i遍历范围:0-255
                  if (i>20):
                      lut[i] = 255
                  else:
                      lut[i]=0
      
          gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
          h,w=gray.shape
          for x in range(h):
              for y in range(w):
                  gray[x,y] = lut[gray[x,y]]
          return gray
      
      src = cv.imread("../tao1.bmp");
      cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
      cv.imshow("input", src)
      #dst = cv.applyColorMap(src, cv.COLORMAP_WINTER)
      #自定义查找表
      dst = pe_grayLut(src,1)
      cv.imshow("output", dst)
      cv.waitKey(0)
      cv.destroyAllWindows()
    

效果:
PYOpenCV学习笔记_第6张图片

你可能感兴趣的:(OpenCV)