Happy Shrimp
http://blog.csdn.net/lights_joy/
Welcome reprint, but please keep the author information
The following is an attempt to isolate the soil and plants in the image, with the goal of getting green images and turning the soil background into black. To test the image:
First Use 2g-r-b get a grayscale chart and its histogram:
#-*-coding:utf-8-*-import cv2import numpy as Npimport matplotlib.pyplot as plt# use 2g-r-b to separate soil with background src = cv2.imread (' f:\\ Tmp\\cotton.jpg ') cv2.imshow (' src ', src) # is converted to a floating-point number for calculation fsrc = Np.array (src, dtype=np.float32)/255.0 (B,g,r) = Cv2.split ( FSRC) Gray = 2 * g-b-r# The maximum and minimum values (Minval, Maxval, Minloc, Maxloc) = Cv2.minmaxloc (Gray) # calculate Histogram hist = Cv2.calchist ([Gray ], [0], None, [Plt.plot], [Minval, Maxval]) (hist) plt.show () Cv2.waitkey ()
it turns out that before the histogram is calculated, it is converted to U8 the type, now looks completely superfluous! You just need to give the series and the maximum minimum value!
finally got the 2g-r-b the histogram:
re-use OTSU the 2g-r-b grayscale graphs for binary values:
# Convert to U8 type, Otsu two value gray_u8 = Np.array ((gray-minval)/(maxval-minval) * 255, Dtype=np.uint8) (Thresh, bin_img) = cv2.th Reshold (gray_u8, -1.0, 255, Cv2. Thresh_otsu) cv2.imshow (' bin_img ', bin_img)
Then you get a good two-value image:
To get the color of the plant image as a mask:
# Get color images (B8, G8, r8) = Cv2.split (src) color_img = Cv2.merge ([B8 & Bin_img, G8 & Bin_img, R8 & Bin_img])
Look at the results:
It is basically in line with our expectations.
??
Python image processing (6): separating soil from plants