荐 opencv进阶学习笔记2:numpy操作图像,色彩空间,查找指定颜色范围,通道分离与合并
基础部分传送门
python3+opencv学习笔记汇总目录(适合基础入门学习)
Numpy操作图像
1.遍历图像每一个像素,并颜色取反
import cv2 as cv
import numpy as np
def access_pixels(image):
print(image.shape);
height = image.shape[0]
width = image.shape[1]
channels = image.shape[2]
print("width : %s, height : %s channels : %s"%(width, height, channels))
for row in range(height):
for col in range(width):
for c in range(channels):
pv = image[row, col, c]#遍历每一个像素
image[row, col, c] = 255 - pv#取反
cv.imshow("pixels_demo", image)
print("--------- Hello Python ---------")
src = cv.imread("1.jpg") # blue, green red
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
#计时装置
t1 = cv.getTickCount()#
access_pixels(src)
t2 = cv.getTickCount()
time = (t2-t1)/cv.getTickFrequency();
print("time : %s ms"%(time*1000))#
#计算结束
cv.waitKey(0)
cv.destroyAllWindows()
OpenCV实现像素取反
cv2.bitwise_not(image)
bitwise:按位计算
not:取反
比numpy取反快几十倍
def inverse(image):
dst = cv.bitwise_not(image)
cv.imshow("inverse demo", dst)
2.numpy创建图
多通道图
import cv2 as cv
import numpy as np
def create_image():
img = np.zeros([400, 400, 3], np.uint8)#创建一张全黑图
img[: , : , 0] = np.ones([400, 400])*255#修改第一个通道值
img[:, :, 2] = np.ones([400, 400]) * 255#修改第三通道值
cv.imshow("new image", img)
#cv.imwrite('test.jpg',img)
print("--------- Hello Python ---------")
create_image()
cv.waitKey(0)
cv.destroyAllWindows()
单通道图
import cv2 as cv
import numpy as np
def create_image():
img=np.zeros([400,400,1],np.uint8)
img[:,:,0]=np.ones([400, 400])*100
cv.imshow("new image", img)
#cv.imwrite('test.jpg',img)
print("--------- Hello Python ---------")
create_image()
cv.waitKey(0)
cv.destroyAllWindows()
色彩空间
HSI、HSV、RGB、CMY、CMYK、HSL、HSB、Ycc、XYZ、Lab、YUV色彩空间(颜色模型)
色彩空间相互转换
使用cv2.cvtColor()
HSV中:H[0-100],S[0-255],V[0-255]
import cv2 as cv
import numpy as np
def color_space_demo(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.imshow("gray", gray)
hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
cv.imshow("hsv", hsv)
yuv = cv.cvtColor(image, cv.COLOR_BGR2YUV)
cv.imshow("yuv", yuv)
Ycrcb = cv.cvtColor(image, cv.COLOR_BGR2YCrCb)
cv.imshow("ycrcb", Ycrcb)
print("--------- Hello Python ---------")
src = cv.imread("1.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
color_space_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()
inRange查找颜色范围
cv2.inRange(img,low,high)
HSV中:H[0-100],S[0-255],V[0-255]
1.先把图像转换成hsv中,根据表来查找具体颜色处于什么区间,设置区间。
2.结合cv2.inRange()可以清晰看到某个颜色区域图像位于图像的什么地方。
用法cv2.inRange(img,low,high),函数会将位于两个区域间的值置为255,位于区间外的值置为0。比如想要看到青色的区域处于图像中的什么位置。
如绿色的区域是
lower_red = np.array([35, 43, 46])
upper_red = np.array([77, 255, 255])
import numpy as np
def color_space_demo(image):
hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
#cv.imshow("hsv", hsv)
lower_red = np.array([35, 43, 46])
upper_red = np.array([77, 255, 255])
mask = cv.inRange(hsv, lower_red, upper_red)
cv.imshow('hsvred', mask)
src = cv.imread("3.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
color_space_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()
如如所示,绿色被很好的找出来啦。为右图白色部分。
应用场景:得到的是二值图像,可以用于查找物体轮廓。
让绿色显示出来
dst = cv2.bitwise_and(image1, image2, mask=mask)
让两个图像除去mask部分的图像进行与运算。)
import cv2 as cv
import numpy as np
def color_space_demo(image):
hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
#cv.imshow("hsv", hsv)
lower_red = np.array([35, 43, 46])
upper_red = np.array([77, 255, 255])
mask = cv.inRange(hsv, lower_red, upper_red)
dst = cv.bitwise_and(image, image, mask=mask)#除去mask部分的图像进行与运算。得到绿色
cv.imshow('hsvred', dst)
src = cv.imread("3.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
color_space_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()
通道分离与合并
通道分离
b, g, r = cv2.split(src)#分离
import cv2
src = cv2.imread("2.jpg")
cv2.namedWindow("input image", cv2.WINDOW_AUTOSIZE)
cv2.imshow("input image", src)
b, g, r = cv2.split(src)#分离
cv2.imshow("blue", b)
cv2.imshow("green", g)
cv2.imshow("red", r)
cv2.waitKey(0)
cv2.destroyAllWindows()
>
通道合并
src = cv2.merge([b, g, r])
import cv2
src = cv2.imread("2.jpg")
cv2.namedWindow("input image", cv2.WINDOW_AUTOSIZE)
cv2.imshow("input image", src)
b, g, r = cv2.split(src)#分离
#通道合并
src = cv2.merge([b, g, r])
src[:, :, 0] = 0#对最后一个通道赋值0
cv2.imshow("changed image", src)
cv2.waitKey(0)
cv2.destroyAllWindows()
电气专业的计算机小白,写博文不容易。如果你觉得本文对你有用,请点个赞支持下。谢谢。
本文地址:https://blog.csdn.net/kobeyu652453/article/details/107242116