怎么样在电脑上把jpg改成yuv(从零学AI之图像算法3:使用 opencv 进行图片颜色转换)
RGB色彩就是常说的三原色,R代表Red(红色),G代表Green(绿色),B代表Blue(蓝色)。自然界中肉眼所能看到的任何色彩都可以由这三种色彩混合叠加而成,因此也称为加色模式。在计算机当中,颜色处理在计算机图形学中,一个RGB颜色模型的真彩图形,也是由红、绿、蓝三个色彩信息通道合成的,每个通道用了8位色彩深度,共计24位,也就是2**24=16777216种颜色,达到人眼分辨的极限,除了24位还有8位、16位等,这些信息后续我们再汇总起来分享,不是本次的重点。
OpenCV中有数百种关于在不同色彩空间之间转换的方法,常用的色彩空间:灰度、BGR以及HSV(Hue,Saturation,Value)等转换。
不同颜色的转换的应用:
1.1灰度色彩空间是通过去除彩色信息来将其转换成灰阶,灰度色彩空间对中间处理特别有效,比如人脸检测。
1.2BGR,即蓝一绿一红色彩空间,每一个像素点都由一个三元数组来表示,分别代表蓝、绿、红三种颜色「。网页开发者可能熟悉另一个与之相似的颜色空间:RGB,它们只是在颜色的顺序上不同。
1.3HSV,H(Hue)是色调,S(Saturation)是饱和度,V(Value)表示黑暗的程度(或光谱另一端的明亮程度)。
2.具体用法:
cvtColor函数的功能:用cvtColor()函数对图像进行色彩空间的转换:
语法:cv2.cvtColor(src,code[,dst[,dstCn]])
参数:
src:它是要更改其色彩空间的图像。
code:这是色彩空间转换代码。
dst:与src图像大小和深度相同的输出图像。它是一个可选参数。
dstCn:是目标图像中的频道数。如果参数为0,则通道数自动从src和代码得出。它是一个可选参数。
返回值:返回图像。
flags=[xforxindir(cv2)ifx.startswith("COLOR_")]print(f"支持的颜色转换:{flags}")支持的颜色转换:['COLOR_BAYER_BG2BGR','COLOR_BAYER_BG2BGRA','COLOR_BAYER_BG2BGR_EA','COLOR_BAYER_BG2BGR_VNG','COLOR_BAYER_BG2GRAY','COLOR_BAYER_BG2RGB','COLOR_BAYER_BG2RGBA','COLOR_BAYER_BG2RGB_EA','COLOR_BAYER_BG2RGB_VNG','COLOR_BAYER_GB2BGR','COLOR_BAYER_GB2BGRA','COLOR_BAYER_GB2BGR_EA','COLOR_BAYER_GB2BGR_VNG','COLOR_BAYER_GB2GRAY','COLOR_BAYER_GB2RGB','COLOR_BAYER_GB2RGBA','COLOR_BAYER_GB2RGB_EA','COLOR_BAYER_GB2RGB_VNG','COLOR_BAYER_GR2BGR','COLOR_BAYER_GR2BGRA','COLOR_BAYER_GR2BGR_EA','COLOR_BAYER_GR2BGR_VNG','COLOR_BAYER_GR2GRAY','COLOR_BAYER_GR2RGB','COLOR_BAYER_GR2RGBA','COLOR_BAYER_GR2RGB_EA','COLOR_BAYER_GR2RGB_VNG','COLOR_BAYER_RG2BGR','COLOR_BAYER_RG2BGRA','COLOR_BAYER_RG2BGR_EA','COLOR_BAYER_RG2BGR_VNG','COLOR_BAYER_RG2GRAY','COLOR_BAYER_RG2RGB','COLOR_BAYER_RG2RGBA','COLOR_BAYER_RG2RGB_EA','COLOR_BAYER_RG2RGB_VNG','COLOR_BGR2BGR555','COLOR_BGR2BGR565','COLOR_BGR2BGRA','COLOR_BGR2GRAY','COLOR_BGR2HLS','COLOR_BGR2HLS_FULL','COLOR_BGR2HSV','COLOR_BGR2HSV_FULL','COLOR_BGR2LAB','COLOR_BGR2LUV','COLOR_BGR2Lab','COLOR_BGR2Luv','COLOR_BGR2RGB','COLOR_BGR2RGBA','COLOR_BGR2XYZ','COLOR_BGR2YCR_CB','COLOR_BGR2YCrCb',..."_BGRA2BGR555','COLOR_BGRA2BGR565','COLOR_BGRA2GRAY','COLOR_BGRA2RGB','COLOR_BGRA2RGBA','COLOR_BGRA2YUV_I420','COLOR_BGRA2YUV_IYUV','COLOR_BGRA2YUV_YV12','COLOR_BayerBG2BGR','COLOR_BayerBG2BGRA','COLOR_BayerBG2BGR_EA','COLOR_BayerBG2BGR_VNG','COLOR_BayerBG2GRAY','COLOR_BayerBG2RGB','COLOR_BayerBG2RGBA','COLOR_BayerBG2RGB_EA','COLOR_BayerBG2RGB_VNG','COLOR_BayerGB2BGR','COLOR_BayerGB2BGRA','COLOR_BayerGB2BGR_EA','COLOR_BayerGB2BGR_VNG','COLOR_BayerGB2GRAY','COLOR_BayerGB2RGB','COLOR_BayerGB2RGBA','COLOR_BayerGB2RGB_EA','COLOR_BayerGB2RGB_VNG','COLOR_BayerGR2BGR','COLOR_BayerGR2BGRA','COLOR_BayerGR2BGR_EA','COLOR_BayerGR2BGR_VNG','COLOR_BayerGR2GRAY','COLOR_BayerGR2RGB','COLOR_BayerGR2RGBA','COLOR_BayerGR2RGB_EA','COLOR_BayerGR2RGB_VNG','COLOR_BayerRG2BGR','COLOR_BayerRG2BGRA','COLOR_BayerRG2BGR_EA','COLOR_BayerRG2BGR_VNG','COLOR_BayerRG2GRAY','COLOR_BayerRG2RGB','COLOR_BayerRG2RGBA','COLOR_BayerRG2RGB_EA','COLOR_BayerRG2RGB_VNG','COLOR_COLORCVT_MAX','COLOR_GRAY2BGR','COLOR_GRAY2BGR555','COLOR_GRAY2BGR565','COLOR_GRAY2BGRA','COLOR_GRAY2RGB','COLOR_GRAY2RGBA','COLOR_HLS2BGR','COLOR_HLS2BGR_FULL','COLOR_HLS2RGB','COLOR_HLS2RGB_FULL','COLOR_HSV2BGR','COLOR_HSV2BGR_FULL','COLOR_HSV2RGB','COLOR_HSV2RGB_FULL','COLOR_LAB2BGR','COLOR_LAB2LBGR','COLOR_LAB2LRGB','COLOR_LAB2RGB','COLOR_LBGR2LAB','COLOR_LBGR2LUV','COLOR_LBGR2Lab','COLOR_LBGR2Luv','COLOR_LRGB2LAB','COLOR_LRGB2LUV','COLOR_LRGB2Lab','COLOR_LRGB2Luv','COLOR_LUV2BGR','COLOR_LUV2LBGR','COLOR_LUV2LRGB','COLOR_LUV2RGB','COLOR_Lab2BGR','COLOR_Lab2LBGR','COLOR_Lab2LRGB','COLOR_Lab2RGB','COLOR_Luv2BGR','COLOR_Luv2LBGR','COLOR_Luv2LRGB','COLOR_Luv2RGB','COLOR_M_RGBA2RGBA','COLOR_RGB2BGR','COLOR_RGB2BGR555','COLOR_RGB2BGR565','COLOR_RGB2BGRA','COLOR_RGB2GRAY','COLOR_RGB2HLS','COLOR_RGB2HLS_FULL','COLOR_RGB2HSV','COLOR_RGB2HSV_FULL','COLOR_RGB2LAB','COLOR_RGB2LUV','COLOR_RGB2Lab','COLOR_RGB2Luv','COLOR_RGB2RGBA','COLOR_RGB2XYZ','COLOR_RGB2YCR_CB','COLOR_RGB2YCrCb','COLOR_RGB2YUV','COLOR_RGB2YUV_I420','COLOR_RGB2YUV_IYUV','COLOR_RGB2YUV_YV12','COLOR_RGBA2BGR','COLOR_RGBA2BGR555','COLOR_RGBA2BGR565','COLOR_RGBA2BGRA','COLOR_RGBA2GRAY','COLOR_RGBA2M_RGBA','COLOR_RGBA2RGB','COLOR_RGBA2YUV_I420','COLOR_RGBA2YUV_IYUV','COLOR_RGBA2YUV_YV12','COLOR_RGBA2mRGBA','COLOR_XYZ2BGR','COLOR_XYZ2RGB','COLOR_YCR_CB2BGR','COLOR_YCR_CB2RGB','COLOR_YCrCb2BGR','COLOR_YCrCb2RGB','COLOR_YUV2BGR','COLOR_YUV2BGRA_I420','COLOR_YUV2BGRA_IYUV','COLOR_YUV2BGRA_NV12','COLOR_YUV2BGRA_NV21','COLOR_YUV2BGRA_UYNV','COLOR_YUV2BGRA_UYVY','COLOR_YUV2BGRA_Y422','COLOR_YUV2BGRA_YUNV','COLOR_YUV2BGRA_YUY2','COLOR_YUV2BGRA_YUYV','COLOR_YUV2BGRA_YV12','COLOR_YUV2BGRA_YVYU','COLOR_YUV2BGR_I420','COLOR_YUV2BGR_IYUV','COLOR_YUV2BGR_NV12','COLOR_YUV2BGR_NV21','COLOR_YUV2BGR_UYNV','COLOR_YUV2BGR_UYVY','COLOR_YUV2BGR_Y422','COLOR_YUV2BGR_YUNV','COLOR_YUV2BGR_YUY2','COLOR_YUV2BGR_YUYV','COLOR_YUV2BGR_YV12','COLOR_YUV2BGR_YVYU','COLOR_YUV2GRAY_420','COLOR_YUV2GRAY_I420','COLOR_YUV2GRAY_IYUV','COLOR_YUV2GRAY_NV12','COLOR_YUV2GRAY_NV21','COLOR_YUV2GRAY_UYNV','COLOR_YUV2GRAY_UYVY','COLOR_YUV2GRAY_Y422','COLOR_YUV2GRAY_YUNV','COLOR_YUV2GRAY_YUY2','COLOR_YUV2GRAY_YUYV','COLOR_YUV2GRAY_YV12','COLOR_YUV2GRAY_YVYU','COLOR_YUV2RGB','COLOR_YUV2RGBA_I420','COLOR_YUV2RGBA_IYUV','COLOR_YUV2RGBA_NV12','COLOR_YUV2RGBA_NV21','COLOR_YUV2RGBA_UYNV','COLOR_YUV2RGBA_UYVY','COLOR_YUV2RGBA_Y422','COLOR_YUV2RGBA_YUNV','COLOR_YUV2RGBA_YUY2','COLOR_YUV2RGBA_YUYV','COLOR_YUV2RGBA_YV12','COLOR_YUV2RGBA_YVYU','COLOR_YUV2RGB_I420','COLOR_YUV2RGB_IYUV','COLOR_YUV2RGB_NV12','COLOR_YUV2RGB_NV21','COLOR_YUV2RGB_UYNV','COLOR_YUV2RGB_UYVY','COLOR_YUV2RGB_Y422','COLOR_YUV2RGB_YUNV','COLOR_YUV2RGB_YUY2','COLOR_YUV2RGB_YUYV','COLOR_YUV2RGB_YV12','COLOR_YUV2RGB_YVYU','COLOR_YUV420P2BGR','COLOR_YUV420P2BGRA','COLOR_YUV420P2GRAY','COLOR_YUV420P2RGB','COLOR_YUV420P2RGBA','COLOR_YUV420SP2BGR','COLOR_YUV420SP2BGRA','COLOR_YUV420SP2GRAY','COLOR_YUV420SP2RGB','COLOR_YUV420SP2RGBA','COLOR_YUV420p2BGR','COLOR_YUV420p2BGRA','COLOR_YUV420p2GRAY','COLOR_YUV420p2RGB','COLOR_YUV420p2RGBA','COLOR_YUV420sp2BGR','COLOR_YUV420sp2BGRA','COLOR_YUV420sp2GRAY','COLOR_YUV420sp2RGB','COLOR_YUV420sp2RGBA','COLOR_mRGBA2RGBA']###代码演示:img_path="src/imgs/2.jpg"img=cv2.imread(img_path)gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)###灰色hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)cv2.namedWindow("input",0)cv2.imshow("input",img)cv2.namedWindow("gray",0)cv2.imshow("gray",gray)cv2.namedWindow("hsv",0)cv2.imshow("hsv",hsv)cv2.waitKey(0)
颜色提取:
importnumpyasnpimportargparseimportcv2image=cv2.imread('src/imgs/jz.jpg')color=[([0,70,70],[70,255,255])#黄色范围~这个是我自己试验的范围,可根据实际情况自行调整~注意:数值按[b,g,r]排布]#如果color中定义了几种颜色区间,都可以分割出来for(lower,upper)incolor:#创建NumPy数组lower=np.array(lower,dtype="uint8")#颜色下限upper=np.array(upper,dtype="uint8")#颜色上限#根据阈值找到对应颜色mask=cv2.inRange(image,lower,upper)output=cv2.bitwise_and(image,image,mask=mask)#展示图片cv2.imshow("images",np.hstack([image,output]))cv2.waitKey(0)