OPENCV3 Computer Vision +python (iv)

Source: Internet
Author: User

Segmentation of objects with Grabcut algorithm using watershed and Grabcut algorithm

In the OpenCV, the Grabcut segmentation algorithm is realized, the algorithm can easily divide the foreground image, the operation is simple, and the effect of segmentation is very good. The principle of the algorithm is described in Papaer: "Grabcut"-interactive Foreground Extraction using iterated Graph Cuts

For example, the following diagram, we just select a quadrilateral box, the image in the box as an input parameter of Grabcut, indicating that the pixel in the box may belong to the foreground, but the outside of the box must belong to the background.

The Grabcut algorithm implements the following steps:

1. Define a rectangle containing (one or more) objects in the picture

2. Areas outside the rectangle are automatically considered as backgrounds

3. For user-defined rectangular areas, the data in the background can be used to distinguish the foreground and background areas within it.

4. Use the Gaussian mixture model (GMM) to modeling the background and foreground, and mark the undefined pixels as possible foreground or background.

5. Each pixel in the image is considered to be connected by a virtual edge to the surrounding pixels, and each edge has a probability of being a foreground or background, based on its similarity to the color of the surrounding pixels.

6. Each pixel (that is, the node in the algorithm) is connected to a foreground or background node

7. After the node completes the connection (possibly with the background or foreground connection), if the edges between the nodes belong to different terminals (that is, one node belongs to the foreground and the other node belongs to the background), the edges between them are cut off (this is the cutting part of the algorithm name), which splits the parts of the image.

 as Npimport Cv2  from  as pltimg=cv2.imread ('1.jpg') mask=np.zeros (img.shape[:2 ],np.uint8) #创建一个掩模

#创建以0填充的前景和背景模型bgdModel=np.zeros ((1, $), Np.float64) Fgdmodel=np.zeros ((1, $), Np.float64) Rect=( -, the, -,670) #创建矩形cv2. Grabcut (Img,mask,rect,bgdmodel,fgdmodel,5, Cv2. Gc_init_with_rect) #使用了指定的空模型和掩模来运行GrabCut, and actually initializes the operation with a rectangle # after doing this, our mask has become a value that contains 0~3. Values of 0 and 2 will be converted to 0, and a value of 1,3 will be converted to 1. Then save in Mask2. This allows you to filter out all 0-valued pixels with mask2 (theoretically preserving all foreground pixels completely) Mask2=NP.where((mask==0)| (mask==2),0,1). Astype ('uint8') img=img*Mask2[:,:,np.newaxis]plt.subplot (121), Plt.imshow (IMG) plt.title ("Grabcut"), Plt.xticks ([]), Plt.yticks ([]) Plt.subplot (122), Plt.imshow (Cv2.cvtcolor (Cv2.imread ("1.jpg"), Cv2. COLOR_BGR2RGB)) Plt.title ("Original"), Plt.xticks ([]), Plt.yticks ([]) plt.show ()


Image segmentation using the watershed algorithm

Think of low-density areas in the image (with little variation) as valleys, where high-density areas (many changes) are imagined as peaks. Start pouring water into the valleys until the water in different valleys begins to converge. In order to prevent the confluence of water in different valleys, some fences can be set up, and the final fence is image segmentation.

Import NumPy asNpimport Cv2 fromMatplotlib Import Pyplot aspltimg=cv2.imread ("1.jpg") Gray=Cv2.cvtcolor (Img,cv2. Color_bgr2gray) #颜色转为灰度ret, Thresh=cv2.threshold (Gray,0,255, Cv2. thresh_binary_inv+Cv2. Thresh_otsu) #可为图像设一个阈值kernel=np.ones ((3,3), np.uint8) opening=cv2.morphologyex (Thresh,cv2. morph_open,kernel,iterations=2) #去除噪声sure_bg=cv2.dilate (opening,kernel,iterations=3) Dist_transform=cv2.distancetransform (Opening,cv2. DIST_L2,5) #可以通过distanceTransform来获取确定的前景区域. That is, this is the most likely foreground area in the image, and the more distant the boundary point from the background area is likely to be the foreground, the thresholds are used to determine which areas are the foreground RET,SURE_FG=cv2.threshold (Dist_transform,0.7*dist_transform.max (),255,0#这个阶段之后, what should be done with the overlapping parts of the foreground and background? These areas need to be identified first, which can be subtracted from the set of SURE_BG and SURE_FG SURE_FG=np.uint8 (SURE_FG) Unknown=cv2.subtract (SURE_BG,SURE_FG) #现在有了这些区域, you can set fences to stop water pooling, which is done through the connectedcomponents function. Ret,markers=cv2.connectedcomponents (SURE_FG) Markers=markers+1Markers[unknown==255]=0#把栅栏绘制成红色markers=cv2.watershed (img,markers) img[markers==-1]=[255,0,0]plt.imshow (IMG) plt.show ()

Opencv3 Computer Vision +python (iv)

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.