OpecCV color segmentation

Color segmentation

Normally read pictures are in RGB format, which means that each color is composed of three RGB combinations. The same commonly used format is HSV. For details, see the following posted by Baidu:
Hue H
is measured in angle, and the value range is 0°~360°. Starting from red, it is calculated in counterclockwise direction. Red is 0°, and green is is 120° and blue is 240°. Their complementary colors are: yellow is 60°, cyan is 180°, and purple is 300°;
Saturation S
Saturation S indicates how close the color is to the spectral color. A color can be thought of as the result of mixing a certain spectral color with white. The greater the proportion of spectral colors, the closer the color is to spectral colors, and the higher the saturation of the color. The saturation is high and the color is deep and vivid. The white light component of the spectral color is 0, and the saturation reaches the highest level. Usually the value range is 0% ~ 100%. The larger the value, the more saturated the color.
Value V
value represents the brightness of the color. For the light source color, the brightness value is related to the brightness of the luminous body; for the object color, this value is related to the transmittance or reflectance of the object. Usually the value range is 0% (black) to 100% (white).

The following shows how to use HSV to color segment pictures. The pictures to be segmented are as follows:
insert image description here
In HSV format, H means related to color, so if segmentation is based on color, it is mainly to perform threshold segmentation on the H channel. Looking at the picture, the birds are of one color, and the background is roughly the same color. It can be roughly guessed that the histogram of the H channel has two peaks, and the background pixels are larger than the bird pixels, so the peak with a smaller amplitude is chosen to be the area where the bird is. :
Write code below to implement related content:

	img_hsv = cv2.cvtColor(img2,cv2.COLOR_BGR2HSV)
    H,S,V = cv2.split(img_hsv)
    plt.bar([i for i in range(256)],calHist(H))
	plt.show()

First, let’s take a look at the histogram of the H channel:
insert image description here
Same as the prediction results, we can see that the wave peaks are mainly concentrated around 100, so the threshold selection needs to be on both sides of 100:
Let’s try the results of different threshold selections: Different thresholds are [ 50,120], [80-120], [100,120],
insert image description here
it can be seen that as long as the wavelet peak is completely included in the threshold selection range, the bird can be selected.
Then use this mask to perform a bitwise AND operation on the image:

res = cv2.bitwise_and(img,img,mask=mask)

Get the segmented bird:
insert image description here
The complete code is as follows:

def split_baseColor(img):
    img_blur = cv2.blur(img,ksize=(5,5))
    img_median = cv2.medianBlur(img_blur,5)
    img_gaussian = cv2.GaussianBlur(img_median,(5,5),0)
    img2 = cv2.bilateralFilter(img_gaussian,9,75,75)

    img_hsv = cv2.cvtColor(img2,cv2.COLOR_BGR2HSV)
    H,S,V = cv2.split(img_hsv)
    plt.bar([i for i in range(256)],calHist(H))
    plt.bar([i for i in range(256)],calHist(S))
    plt.bar([i for i in range(256)],calHist(V))
    plt.show()

    low = np.array([50,0,0])
    high = np.array([120,255,255])
    mask = cv2.inRange(img_hsv,low,high)


    res = cv2.bitwise_and(img,img,mask=mask)
    s = np.hstack((img,res))
    s = cv2.pyrDown(s)
    show(s)

Guess you like

Origin blog.csdn.net/qq_25105061/article/details/108731319