openCV-Task07:25-28 25:像素重映射;26 :图像二值化;:27:全局与自适应二值化;28:实时人脸检测

目录

像素重映射

函数及参数

 代码实现

运行结果 

 图像二值化

函数及参数

 代码实现

运行结果

 全局与自适应二值化

全局二值化方法

 三角法

 全局二值化函数及参数

代码实现

自适应二值化

方法:

函数及参数

代码实现

运行结果

 实时人脸检测

图片人脸检测

视频实时检测


像素重映射

把像素从一个位置重新映射到一个新的位置。

函数及参数

cv.remap(src, map1, map2, interpolation[, dst[, borderMode[, borderValue]]] ) -> dst
src表示图像
map1表示x,y方向映射规则,或者x方向映射
Map2如果map1表示x,y映射时为空,否则表示y
表示映射时候的像素插值方法 支持:INTER_NEAREST 、NTER_LINEAR 、NTER_CUBIC 

 代码实现

    image = cv.imread('.\\data\\home.jpg')
    cv.namedWindow("remap-demo",cv.WINDOW_AUTOSIZE)
    cv.createTrackbar("remap-type",'remap-demo',0,3,trackbar_callback)
    h,w,c = image.shape
    cv.imshow('input',image)
    map_x = np.zeros((h,w), dtype=np.float32)
    map_y = np.zeros((h,w),dtype=np.float32)
    while True:
        pos = cv.getTrackbarPos("remap-type","remap-demo")
        if pos == 0:  # 倒立图像
            for i in range(h):
                map_x[i,:] = [x for x in range(map_x.shape[1])]   # 每次改变一行
            for j in range(w):
                # 对他每列对应位置的像素值进行改变
                map_y[:, j] = [map_y.shape[0] - y for y in range(map_y.shape[0])]    # 每次改变一列
        elif pos == 1:  # 镜像图像
            for i in range(map_x.shape[0]):
                map_x[i,:] = [map_x.shape[1] - x for x in range(map_x.shape[1])]
            for j in range(map_y.shape[1]):
                map_y[:,j] = [y for y in range(map_y.shape[0])]
        elif pos == 2:  # 对角线对称
            for i in range(map_x.shape[0]):
                map_x[i,:] = [map_x.shape[1] - x for x in range(map_x.shape[1])]
            for j in range(map_y.shape[1]):
                map_y[:,j] = [map_y.shape[0] - y for y in range(map_y.shape[0])]
        elif pos == 3:  # 放大两倍
            for i in range(map_x.shape[0]):
                map_x[i,:] = [int(x/2) for x in range(map_x.shape[1])]
            for j in range(map_y.shape[1]):
                map_y[:,j] = [int(y/2) for y in range(map_y.shape[0])]
        dst = cv.remap(image,map_x,map_y,cv.INTER_LINEAR)
        cv.imshow('remap-demo',dst)
        c = cv.waitKey(100)
        if c == 27:
            break
    cv.destroyAllWindows()

运行结果 

openCV-Task07:25-28 25:像素重映射;26 :图像二值化;:27:全局与自适应二值化;28:实时人脸检测_第1张图片 原图

 

openCV-Task07:25-28 25:像素重映射;26 :图像二值化;:27:全局与自适应二值化;28:实时人脸检测_第2张图片 倒立图像

 

openCV-Task07:25-28 25:像素重映射;26 :图像二值化;:27:全局与自适应二值化;28:实时人脸检测_第3张图片 镜像图片

 

openCV-Task07:25-28 25:像素重映射;26 :图像二值化;:27:全局与自适应二值化;28:实时人脸检测_第4张图片 对角线对称

 

openCV-Task07:25-28 25:像素重映射;26 :图像二值化;:27:全局与自适应二值化;28:实时人脸检测_第5张图片 放大两倍后

 图像二值化

函数及参数

只有两个像素值 0 1(0 表示黑色, 1-255 表示白色 )
cv.threshold(	src, thresh, maxval, type[, dst]) ->	retval, dst
src表示输入图像
thresh表示阈值
maxval表示最大值
type表示二值化THRESH_BINARY或者二值化反THRESH_BINARY_INV
retval表示返回阈值,dst返回的二值图像

 代码实现

    #灰度图像
    image = cv.imread(".\\data\\girl.jpg")
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    cv.imshow("gray",gray)
    # 手动网值,二值化
    ret,binary = cv.threshold(gray,127,255,cv.THRESH_BINARY)
    cv.imshow("binary1",binary)
    cv.waitKey(0)
    # 均值,二值化
    m = cv.mean(gray)[0]
    print("mean threshold: ", m)
    ret, binary = cv.threshold(gray,m,255,cv.THRESH_BINARY)
    print(ret)
    print(binary)
    cv.imshow("binary2", binary)
    cv.waitKey(0)
    cv.destroyAllWindows()

运行结果

openCV-Task07:25-28 25:像素重映射;26 :图像二值化;:27:全局与自适应二值化;28:实时人脸检测_第6张图片 原图

 

openCV-Task07:25-28 25:像素重映射;26 :图像二值化;:27:全局与自适应二值化;28:实时人脸检测_第7张图片 阈值为127的二值图

 

openCV-Task07:25-28 25:像素重映射;26 :图像二值化;:27:全局与自适应二值化;28:实时人脸检测_第8张图片 随机生成的阈值

 全局与自适应二值化

全局二值化方法

大津法:0~5六个灰度级别,根据直方图分布,以每个灰度等级分割直方图分布为两个部分,分别求取均值跟方差,如图示,最小方法差和对应的灰度值为,分割阈值。

openCV-Task07:25-28 25:像素重映射;26 :图像二值化;:27:全局与自适应二值化;28:实时人脸检测_第9张图片

 三角法

openCV-Task07:25-28 25:像素重映射;26 :图像二值化;:27:全局与自适应二值化;28:实时人脸检测_第10张图片

 全局二值化函数及参数

cv.threshold(src, thresh, maxval, type[, dst]) ->	retval, dst
type表示二值化
THRESH_BINARY | THRESH_OTSU        # 大津法     大于阈值表示max,白色;小于阈值表示0,纯黑
THRESH_BINARY | THRESH_TRIANGLE    # 三角法
THRESH_BINARY_INV | THRESH_OTSU
表示不同的全局二值化方法

代码实现

自适应二值化

方法:

①模糊图像-D:均值模糊/高斯模糊

②原图S+偏置常量C

T = S –D > -C ?  255 : 0

函数及参数

cv.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst]	) ->	dst
cv.ADAPTIVE_THRESH_MEAN_C   # 均值模糊
cv.ADAPTIVE_THRESH_GAUSSIAN_C # 高斯模糊
blockSize     必须为奇数
C表示要减去的权重,可以是正数,负数,0

代码实现

    # 灰度图像
    image = cv.imread(".\\data\\ml.png")
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    cv.imshow("gray",gray)

    # 手动阈值::大津法
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
    cv.imshow("binary", binary)
    cv.waitKey(0)

    # 手动阈值::三角法
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_TRIANGLE)
    cv.imshow("binary", binary)
    cv.waitKey(0)
    # 自适应法
    binary = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY_INV, 25, 10)
    cv.imshow("binary",binary)
    cv.waitKey(0)

    cv.waitKey(0)
    cv.destroyAllWindows()

运行结果

openCV-Task07:25-28 25:像素重映射;26 :图像二值化;:27:全局与自适应二值化;28:实时人脸检测_第11张图片 原图

                  

             openCV-Task07:25-28 25:像素重映射;26 :图像二值化;:27:全局与自适应二值化;28:实时人脸检测_第12张图片openCV-Task07:25-28 25:像素重映射;26 :图像二值化;:27:全局与自适应二值化;28:实时人脸检测_第13张图片

                                  大津法                                                三角法

openCV-Task07:25-28 25:像素重映射;26 :图像二值化;:27:全局与自适应二值化;28:实时人脸检测_第14张图片 自适应法

 实时人脸检测

首先导入模型以及恢复系统的配置

model_bin = "F:/Python/OpenCV/opencv_tutorial_data-master/models/face_detector/opencv_face_detector_uint8.pb"
config_text = "F:/Python/OpenCV/opencv_tutorial_data-master/models/face_detector/opencv_face_detector.pbtxt"

图片人脸检测

    net = cv.dnn.readNetFromTensorflow(model_bin, config=config_text)
    e1 = cv.getTickCount()
    frame = cv.imread("./data/lena.jpg")
    print(frame.shape)
    h, w, c = frame.shape
    blobImage = cv.dnn.blobFromImage(frame, 1.0, (300, 300), (104, 177, 123.0), False, False)
    net.setInput(blobImage)
    cvOut = net.forward()
    print(cvOut.shape)

    # Put efficiency information.
    t, _ = net.getPerfProfile()
    label = 'Inference time: %.2f ms' % (t * 1000.0 / cv.getTickFrequency())

    # 绘制检测矩形
    for detection in cvOut[0, 0, :, :]:
        score = float(detection[2])
        objIndex = int(detection[1])
        if score > 0.5:
            left = detection[3] * w
            top = detection[4] * h
            right = detection[5] * w
            bottom = detection[6] * h
            # 绘制矩形
            cv.rectangle(frame, (int(left), int(top)), (int(right), int(bottom)), (255, 0, 0), thickness=2)
            # 绘制文本
            cv.putText(frame, "score:%.2f" % score, (int(left), int(top)), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255), 1)
    e2 = cv.getTickCount()
    fps = cv.getTickFrequency() / (e2 - e1)

    cv.putText(frame, label + (" FPS: %.2f" % fps), (10, 50), cv.FONT_HERSHEY_COMPLEX_SMALL, 1.0, (255, 0, 0), 2)
    cv.imshow('face-detection-demo', frame)
    cv.waitKey(0)
    cv.destroyAllWindows()

运行结果

openCV-Task07:25-28 25:像素重映射;26 :图像二值化;:27:全局与自适应二值化;28:实时人脸检测_第15张图片

视频实时检测

    # 导入 tensorflow model
    net = cv.dnn.readNetFromTensorflow(model_bin, config=config_text)
    capture = cv.VideoCapture(".\\data\\myvideo1.avi")

    # 人脸检测
    while True:
        e1 = cv.getTickCount()
        ret, frame = capture.read()
        frame = cv.flip(frame, 1)
        if ret is not True:
            break
        h, w, c = frame.shape
        blobImage = cv.dnn.blobFromImage(frame, 1.0, (300, 300), (104.0, 177.0, 123.0), False, False);
        net.setInput(blobImage)
        cvOut = net.forward()

        # Put efficiency information.
        t, _ = net.getPerfProfile()
        label = 'Inference time: %.2f ms' % (t * 1000.0 / cv.getTickFrequency())

        # 绘制检测矩形
        for detection in cvOut[0, 0, :, :]:
            score = float(detection[2])
            objIndex = int(detection[1])
            if score > 0.5:
                left = detection[3] * w
                top = detection[4] * h
                right = detection[5] * w
                bottom = detection[6] * h

                # 绘制矩形框
                cv.rectangle(frame, (int(left), int(top)), (int(right), int(bottom)), (255, 0, 0), thickness=2)

                # 绘制类别和得分
                cv.putText(frame, "score:%.2f" % score, (int(left), int(top)), cv.FONT_HERSHEY_SIMPLEX, 0.5,
                           (0, 0, 255), 1)
        e2 = cv.getTickCount()
        fps = cv.getTickFrequency() / (e2 - e1)
        cv.putText(frame, label + (" FPS: %.2f" % fps), (10, 50), cv.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2)
        cv.imshow('face-detection-demo', frame)
        c = cv.waitKey(1)
        if c == 27:
            break
    cv.destroyAllWindows()
    capture.release()

你可能感兴趣的:(OpenCV学习,opencv,计算机视觉,人工智能)