opencvのいくつかの一般的な高度な操作

opencvの原則

import cv2 as cv
img = cv.imread('a.png',1)
#imread有返回值,返回当前内容,有两个参数,参数一是图片名称,参数二是读取方式,0灰度1彩色
cv.imshow('image',img)
#png jpg等是图片文件的封装格式(不压缩图片会使得图片过大几兆左右),其分为两部分:文件头(数据部分的解码信息和附加信息) 文件数据(图片经过压缩编码后的数据)
#文件头可以将文件数据还原成图片最原始的信息
#imshow有两个参数,参数一是窗体名称,参数二是展示的对象
cv.imwrite('b.jpg',img,[cv.IMWRITE_JPEG_QUALITY,0])#参数一是图片名称,参数二当前图片的数据(解码之后的数据) 参数三可以设置当前保存图片的质量
#IMWRITE_JPEG_QUALITY范围为0~100,0时图片的质量非常差,越高图片质量越好,占用内存越大
#jpg是有损压缩,png是无损压缩
cv.imwrite('b.png',img,[cv.IMWRITE_PNG_COMPRESSION,0])#对于png,压缩范围为0~9 0时图片质量最好
# 图片有8位颜色深度表示0~255,对于RGB图片来说,每一种都有256种颜色
# 640*480 表示图片水平方向有640个像素点 垂直方向上有480个像素点
# PNG图片除了RGB之外还有alpha通道,其描绘了图片的透明度
# 颜色存储格式除了RGB之外还有bgr,其第一个像素值是蓝色
# 如何读取图片中的像素:》》》》》
# opencv存储图片时,其坐标轴原点在左上角,x轴是垂直的,y轴是水平的
img = cv.imread('a.png',1)
(b,g,r) = img[200,200] #img是一个矩阵结构,因此使用中括号,读取过来后,是以元组的形式存储的,但opencv读取图片时顺序是bgr
print(b,g,r)
# 如何实现像素的写入?如绘制一条直线? 如从10,100 到110 100
for i in range(1,100):
    img[10+i,100] = (255,0,0)
cv.imshow('image',img)
cv.waitKey(0)#waitKey函数:实现程序的暂停也可以给其他数,代表多少毫秒

バックグラウンド機能のフィルタリング:

def select_rgb_white_yellow(self,image): 
    #过滤掉背景
    lower = np.uint8([120, 120, 120])
    upper = np.uint8([255, 255, 255])
    # lower_red和高于upper_red的部分分别变成0,lower_red~upper_red之间的值变成255,相当于过滤背景
    white_mask = cv2.inRange(image, lower, upper)#对什么数据变换 最小阈值 最大阈值 把高于最大或低于最小的全都变成0
    self.cv_show('white_mask',white_mask)
    masked = cv2.bitwise_and(image, image, mask = white_mask)#对图像(灰度图像或彩色图像均可)每个像素值进行二进制“与”操作
    self.cv_show('masked',masked)
    return masked#最后出来的图片是彩色的 应用时还要转为灰度

関心のあるカスタムエリア

def select_region(self,image):
    """
   感兴趣边界每一条边都会有对应的端点 我们把这些点拿出来连成线就得到了感兴趣区域
    """
    rows, cols = image.shape[:2]#拿到图像的长和宽
    pt_1  = [cols*0.05, rows*0.90]
    pt_2 = [cols*0.05, rows*0.70]
    pt_3 = [cols*0.30, rows*0.55]
    pt_4 = [cols*0.6, rows*0.15]
    pt_5 = [cols*0.90, rows*0.15] 
    pt_6 = [cols*0.90, rows*0.90]
    vertices = np.array([[pt_1, pt_2, pt_3, pt_4, pt_5, pt_6]], dtype=np.int32) #一行六列 深度为二的矩阵
    point_img = image.copy()       #不影响原始图
    point_img = cv2.cvtColor(point_img, cv2.COLOR_GRAY2RGB)#一般情况下 在图中画图要把灰度图转换为RGB要不可能会有异常
    for point in vertices[0]:#遍历每一行
        cv2.circle(point_img, (point[0],point[1]), 10, (0,0,255), 4)#画图函数参数二指定是一个元组 用圆圈画出每一个点
    self.cv_show('point_img',point_img)
    return self.filter_region(image, vertices)

不要な場所を除外する

def filter_region(self,image, vertices):
    """
            过滤掉不需要的地方
    """
    mask = np.zeros_like(image)#输出一个大小一样的纯黑底板
    if len(mask.shape)==2:
        cv2.fillPoly(mask, vertices, 255)#基于顶点坐标进行填充 填充值为255
        self.cv_show('mask', mask)    
    return cv2.bitwise_and(image, mask)#只有mask为255的地方才能留下来

ハフライン検出Api

def hough_lines(self,image):
    #输入的图像需要是边缘检测后的结果
    #minLineLengh(线的最短长度,比这个短的都被忽略)和MaxLineCap(两条直线之间的最大间隔,小于此值,认为是一条直线)
    #rho距离精度 ,theta角度精度,目标越小此值二者越小,threshod超过设定阈值才被检测出线段
    return cv2.HoughLinesP(image, rho=0.1, theta=np.pi/10, threshold=15, minLineLength=9, maxLineGap=4)#函数名加+P速度更快

線分をフィルタリングして描画します

def draw_lines(self,image, lines, color=[255, 0, 0], thickness=2, make_copy=True):
    # 过滤霍夫变换检测到直线
    if make_copy:
        image = np.copy(image) 
    cleaned = []
    for line in lines:#两个图
        for x1,y1,x2,y2 in line:#一个线由两个点组成
            if abs(y2-y1) <=1 and abs(x2-x1) >=25 and abs(x2-x1) <= 55:#停车位当中没有斜的线 横线中大于25小于55可能是边界或其他
                cleaned.append((x1,y1,x2,y2))
                cv2.line(image, (x1, y1), (x2, y2), color, thickness)#画线
    print(" No lines detected: ", len(cleaned))#打印一下剩了多少条线
    return image

ネットワークトレーニング用のトリミング画像

裁剪图片,用于网络训练:
def save_images_for_cnn(self,image, spot_dict, folder_name ='cnn_data'):
    for spot in spot_dict.keys():
        (x1, y1, x2, y2) = spot#spot 每一个停车位
        (x1, y1, x2, y2) = (int(x1), int(y1), int(x2), int(y2))
        #裁剪
        spot_img = image[y1:y2, x1:x2]
        spot_img = cv2.resize(spot_img, (0,0), fx=2.0, fy=2.0) #放缩的稍微大一点
        spot_id = spot_dict[spot]
        filename = 'spot' + str(spot_id) +'.jpg'
        print(spot_img.shape, filename, (x1,x2,y1,y2))
        cv2.imwrite(os.path.join(folder_name, filename), spot_img)#路径拼接函数

おすすめ

転載: blog.csdn.net/qq_42308217/article/details/109380739