OpenCV Learning Record (3): Simple use of image masks to extract objects of interest in images

There are many ways to extract object features in OpenCV.
There are many from simple image color block > image threshold segmentation > contour search > feature point detection > histogram detection and so on. These simple methods seem to have no practical scenarios that can be used directly, but it is very appropriate to use them for learning when learning.
Next, find the blue object in the image according to the color threshold, extract the mask of the object and cover it.

Gamut conversion

Usually, the image directly collected by the camera is in the RGB color gamut (in Opencv, the three-channel color is arranged in BGR by default). For the human eye, the RGB color gamut is a better choice, and the difference between the three colors is significant, but it is not very obvious for the computer. It is equivalent to a color gamut space with obvious color characteristics when observed by the human eye. For computers, the HSV color gamut space is usually used. Color gamut conversion can be easily realized in Opencv.

hsv = cv.cvtColor(imagein, cv.COLOR_BGR2HSV)

In this way, we have completed the conversion of the color gamut.
insert image description here

After converting the color gamut, we can see that the color difference of the image is more intuitive.

threshold extraction

This is the simplest way to extract image features, and a fixed color can be extracted directly by thresholding a specific color in the image. There are some Raspberry Pi ball tracking cars that extract the coordinates of the ball relative to the camera by directly processing the threshold of the image and adding contour features, and then control the car to turn forward, backward or left and right.
Here we directly set the threshold

    # 定义HSV中蓝色的范围
    lower_blue = np.array([110,50,50])
    upper_blue = np.array([130,255,255])
    # 设置HSV的阈值使得只取蓝色
    mask = cv.inRange(hsv, lower_blue, upper_blue)
    blur = cv.GaussianBlur(mask,(5,5),0)

Mask extraction and original image overlay

After thresholding here, we get an image that only contains the color part we are interested in. This image can be called a mask. Add the mask image and the original image to get an image that only contains the region of interest in the original image.
Fuse the mask with the original image:

    # 将掩膜和图像逐像素相加
    res = cv.bitwise_and(frame,frame, mask= mask)

insert image description here
Mask is the image after thresholding is the mask we will use, and RES is the result of adding the mask to the original image.

Finally, compare the two pictures intuitively:
insert image description here

insert image description here
It can be seen that the extraction effect is quite good.
The factors that determine the quality of the mask here areThreshold value and color gamut space selection

source code

The source code uses matplotlib to draw the picture. The image after thresholding is smoothed.

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

cap = cv.VideoCapture(0)
while(1):
    # 读取帧
    _, frame = cap.read()
    # 转换颜色空间 BGR 到 HSV
    hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
    # 定义HSV中蓝色的范围
    lower_blue = np.array([110,50,50])
    upper_blue = np.array([130,255,255])
    # 设置HSV的阈值使得只取蓝色
    mask = cv.inRange(hsv, lower_blue, upper_blue)
    blur = cv.GaussianBlur(mask,(5,5),0)

    # 将掩膜和图像逐像素相加
    res = cv.bitwise_and(frame,frame, mask= mask)
    cv.imshow('frame',frame)
    cv.imshow('mask',mask)
    cv.imshow('res',res)

    plt.subplot(221), plt.imshow(frame), plt.title('Original')
    plt.xticks([]), plt.yticks([])
    plt.subplot(222), plt.imshow(hsv), plt.title('HSV')
    plt.xticks([]), plt.yticks([])
    plt.subplot(223), plt.imshow(mask), plt.title('Mask')
    plt.xticks([]), plt.yticks([])
    plt.subplot(224), plt.imshow(res), plt.title('RES')
    plt.xticks([]), plt.yticks([])
    plt.show()

    k = cv.waitKey(5) & 0xFF
    if k == 27:
        break
cv.destroyAllWindows()

Guess you like

Origin blog.csdn.net/weixin_47407066/article/details/124742933