Python image processing (6): separating soil from plants

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.