OpenCV Grabcut Algorithm Object Segmentation (Python language)

Source: Internet
Author: User
Import NumPy as NP import CV2 from matplotlib import pyplot as plt Imgpath = ' G:/PYTHON_CODE/OPENCVSTUDY/IMAGES/GRABCUT.J Pg ' IMG = cv2.imread (imgpath) # Pre-drawn picture fig = Plt.figure () plt.subplot (121), Plt.imshow (Cv2.cvtcolor (IMG, Cv2. COLOR_BGR2RGB)) Plt.subplot (122), Plt.imshow (Cv2.cvtcolor (IMG, Cv2.
    COLOR_BGR2RGB) Plt.colorbar () plt.show () def OnClick (event): # Get when mouse "press", mouse position global Coords1x, coords1y if Event.button = = 1:coords1x = Int (event.xdata) coords1y = Int (event.ydata) # print ("1x1y" + St R (coords1x) + str (COORDS1Y) def onmousemotion (event): # Get when the mouse "moves" when the mouse position global coords2x, coords2y if Eve Nt.button = = 1:coords2x = Int (event.xdata) coords2y = Int (event.ydata) # print ("2x2y" + str (Coord S2X) + str (COORDS2Y) def onmouserelease (event): if Event.button = = 1:fig = PLT.GCA () img = Cv2.imre

AD (Imgpath) # creates a mask mask = Np.zeros (Img.shape[:2], np.uint8) that is the same shape as the loaded image        # inside the array used within the algorithm, you must create two np.float64 types of 0 arrays, the size is (1, Bgdmodel) = Np.zeros ((1,), Np.float64) Fgdmodel = Np.zeros ((1,), Np.float64) # rectangular region (rect.x,rect.y,rect.width,rect.height) for the calculation of artificial foreground rect = (Coords1x, Coo rds1y, Coords2x-coords1x, coords2y-coords1y) print (rect) Itercount = 5 Cv2.grabcut (img, Mask, Rect, Bgdmodel, Fgdmodel, Itercount, Cv2. Gc_init_with_rect) Mask2 = Np.where ((mask = = 2) | (Mask = = 0), 0, 1). Astype (' uint8 ') img = img * mask2[:,:, Np.newaxis] Plt.subplot (121), Plt.imshow (CV2.CV TColor (IMG, cv2.) COLOR_BGR2RGB)) Plt.subplot (122), Plt.imshow (Cv2.cvtcolor (Cv2.imread), Imgpath. COLOR_BGR2RGB) Fig.figure.canvas.draw () # Connect mouse click event fig.canvas.mpl_connect (' Button_press_event ', OnClick) # Connect Mouse Mobile Event Fig.canvas.mpl_connect (' Motion_notify_event ', onmousemotion) fig.canvas.mpl_connect (' Button_release_event ',
 Onmouserelease)

Using the method, you can use a rectangular frame in the picture to make the object you choose.

The effect chart is as follows:


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.