OpenCv-色彩域(opencv color)

网友投稿 304 2022-08-27


OpenCv-色彩域(opencv color)

常见色彩空间

RGBHSVHISYCrCbYUV

色彩空间转换

最常见的两个

HSV与RGBYUV与RGB

代码练习3(颜色空间转换,通道分割,色彩追踪):

import cv2 as cvimport numpy as npimage_path = "datasources/images/1 (1).jpg"vido_path = "E:\\视频剪辑素材\\yizhan.mp4"def replace_color(image): src = cv.imread(image) #cv.cvtColor颜色空间转换 HSV = cv.cvtColor(src, cv.COLOR_BGR2HSV) cv.imshow("HSV", HSV) HLS = cv.cvtColor(src, cv.COLOR_BGR2HLS) cv.imshow("HIS", HLS) YCrCb = cv.cvtColor(src, cv.COLOR_BGR2YCrCb) cv.imshow("YCrCb", YCrCb) YUV = cv.cvtColor(src, cv.COLOR_BGR2YUV) cv.imshow("YUV", YUV) cv.waitKey(0) cv.destroyAllWindows()def colcor_track(vido): caputer = cv.VideoCapture(vido) while(True): ret, frame = caputer.read() if ret==False: break #原图RGB转换为HSV hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV) hmain = np.array([35, 43, 46]) hmax = np.array([77, 255, 255]) msk = cv.inRange(hsv, hmain, hmax) cv.imshow("yizhan_video", frame) cv.imshow("mask_vedo", msk) c = cv.waitKey(40) if c==27: breakdef split_iamge(image): src = cv.imread(image, 1) # 分隔bgr通道 b, g, r = cv.split(src) cv.imshow("b", b) cv.imshow("g", g) cv.imshow("r", r) src[:, :, 0] = 255 cv.imshow("change_channels2", src) #合并通道 new_image = cv.merge([b, g, r]) cv.imshow("new_imge", new_image) cv.waitKey(0) cv.destroyAllWindows()if __name__ =="__main__": # replace_color(image_path) # colcor_track(vido_path) split_iamge(image_path)

人脸检测

# -*- coding=GBK -*-import cv2 as cvimport numpy as npimport osimage_path = "../personimage/AFLW/0004-image64929.jpg"xml_path = "../opencvcore/opencv/data/haarcascades/haarcascade_frontalface_alt_tree.xml"video_path = "E:\\视频剪辑素材\\lzlh.ts"def person_face_detect(image, xml): src = cv.imread(image) #和后面的imshow名字对应 cv.namedWindow("input image", cv.WINDOW_AUTOSIZE) cv.namedWindow("result", cv.WINDOW_AUTOSIZE) cv.imshow('input image', src) #转换成灰度图像 gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) #级联检测器 face_detector = cv.CascadeClassifier(xml) #在多个尺度空间进行人脸检测,尺度变化,识别度越打越高 faces = face_detector.detectMultiScale(gray, 1.02, 5) #绘制人脸框 for x, y, w, h in faces: cv.rectangle(src, (x, y), (x+w, y+h), (0, 0, 255), 2) cv.imshow("result", src) cv.waitKey(0) cv.destroyAllWindows()def face_interfac(src, xml): #和后面的imshow名字对应 # cv.namedWindow("input video", cv.WINDOW_AUTOSIZE) cv.namedWindow("result", cv.WINDOW_AUTOSIZE) # cv.imshow('input video', src) #转换成灰度图像 gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) #级联检测器 face_detector = cv.CascadeClassifier(xml) #在多个尺度空间进行人脸检测,尺度变化,识别度越打越高 faces = face_detector.detectMultiScale(gray, 1.02, 5) #绘制人脸框 for x, y, w, h in faces: cv.rectangle(src, (x, y), (x+w, y+h), (0, 0, 255), 2) cv.imshow("result", src)#视频中的人脸识别def person_face_video(): caputer = cv.VideoCapture(video_path) while(True): ret, frame = caputer.read() if ret==False: break face_interfac(frame, xml_path) if cv.waitKey(1)==0: breakif __name__ == "__main__": person_face_detect(image_path, xml_path) # person_face_video()

验证码识别(tesseract-OCR+pytesseract )

import cv2 as cvfrom PIL import Imageimport pytesseractimage_path = "datasources/yzm/a510513a-7bc6-4217-bfb3-ad3c197c0e3d.png"def recognize_text(): src = cv.imread(image_path, 1) cv.imshow("原来", src) gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) #获得二值图 ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU) #以下的三步binary良好可省略 #获得指定形状的kernel,矩形MORPH_RECT;交叉形:MORPH_CROSS;椭圆形:MORPH_ELLIPSE; kernel = cv.getStructuringElement(cv.MORPH_RECT, (1, 2)) #先腐蚀进行开运算,去除噪声 binl = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel) #再次获取kernel的形状的 kernel = cv.getStructuringElement(cv.MORPH_RECT, (2, 1)) open_out = cv.morphologyEx(binl, cv.MORPH_OPEN, kernel) #二进制像素进行与 cv.bitwise_not(open_out, open_out) cv.imshow("转换", open_out) #array转换成为image textImage = Image.fromarray(open_out) #OCR识别image中的文字,需要由tesseract包装ocr才可以识别 text = pytesseract.image_to_string(textImage) print("This OK:%s"%text) cv.waitKey(0) cv.destroyAllWindows()if __name__ == "__main__": recognize_text()

总结(全):

基本完成,边缘提取分三个步骤(1.图像降噪2.计算图像梯度3.非极大值抑制4.阈值筛选),膨胀和腐蚀可以降低image的噪声,这里的人脸检测其实就是将人脸图像二值化,调用haarcascades的级联检测器实现的,验证码也是通过二值化然后使用Google的pytesseract +ocr实现的。重点: Canny边缘提取、人脸检测,验证码识别难点: 膨胀与腐蚀


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:动态规划(动态规划模型的建立与求解)
下一篇:显示SonarQube私有项目徽章方案流程
相关文章

 发表评论

暂时没有评论,来抢沙发吧~