Git:https://github.com/linyi0604/computer-vision
1 ImportNumPy as NP2 ImportCv23 ImportMatplotlib.pyplot as Plt4 5 #read in picture6img = Cv2.imread (".. /data/mm2.jpeg")7 #creates a mask with a fill of 0 as a shape loaded with an image8Mask = Np.zeros (img.shape[:2], np.uint8)9 Ten #create a foreground and background model filled with 0 OneBgdmodel = Np.zeros ((1, 65), Np.float64) AFgdmodel = Np.zeros ((1, 65), Np.float64) - - #Define a rectangle theRect = (100, 50, 421, 378) - -Cv2.grabcut (IMG, Mask, rect, Bgdmodel, Fgdmodel, 5, Cv2. Gc_init_with_rect) - + """ - cv2.grabcut () + Parameters: A img: Input Image at Mask: Mask image, determine foreground area, background area, indeterminate area, - can be set to CV2. Gc_bgd,cv2. Gc_fgd,cv2. Gc_pr_bgd,cv2. GC_PR_FGD, - You can also enter 0,1,2,3 - rect: A rectangular format containing the foreground (x, Y, W, h) - Bdgmodel: An array used internally by the algorithm. You just need to create an array of two sizes (1,65) with data type Np.float64 - Fgdmodel: An array used internally by the algorithm. You just need to create an array of two sizes (1,65) with data type Np.float64 in Itercount: Number of iterations of the algorithm - mode: Can be set to CV2. Gc_init_with_rect or CV2. Gc_init_with_mask can also be used in combination. to This is used to determine the way we make changes, either in rectangular mode or in mask mode + - The algorithm modifies the mask image, in the new mask image, the all the pixels are divided into four categories: background, foreground, may be background/foreground using 4 different tag tags. * then we'll modify the mask image, $ all 0 and 1 pixels are classified as 0 (for example, backgrounds), and all 1 pixels and 3 pixels are classified as 1 (foreground). Panax Notoginseng Our final mask image is ready. By multiplying it with the input image, you get the segmented image. - the principle: + 1 Enter the rectangle, and the outer area of the rectangle is the background. The interior must contain the foreground. A 2 The computer initializes the input image, marking the foreground and background pixels. the 3 Using the Gaussian mixture model (GMM), the foreground and background are modeled. + 4 Depending on the input, Gmm learns and creates a new pixel distribution. - for unknown pixels (foreground or background uncertainties), classify them according to their known classification pixel relationships. (similar to cluster operations) $ 5 This creates a picture based on the distribution of the pixels, which are pixels. $ In addition to the pixel points are nodes, there are also Source_node and sink_node two nodes. - all foreground images are connected to the Source_node. The background is connected to the Sink_node. - 6 pixels connected to the source_node/end_node depends on the weight value, the This weight is determined by the probability that the pixels belong to the same class, that is, the foreground or the background. - if the color of the pixel is very different, then the weights between them are very small. Wuyi 7 The image is segmented using the mincut algorithm. the it divides the image into Source_node and sink_node according to the minimum cost equation. - The cost equation refers to the sum of the weights on all sides of the crop. Wu after cropping is complete, all connections to Source_node are judged as foreground, sink_node on the background. - continue this process until the classification is convergent. About $ - - """ -Mask2 = Np.where ((mask = = 2) | (Mask = = 0), 0, 1). Astype ("uint8") AIMG = img*mask2[:,:, Np.newaxis] + thePlt.subplot (121), Plt.imshow (IMG) -Plt.title ("Grabcut"), Plt.xticks ([]), Plt.yticks ([]) $Plt.subplot (122), Plt.imshow (Cv2.cvtcolor (Cv2.imread (".. /data/mm2.jpeg"), Cv2. COLOR_BGR2RGB)) thePlt.title ("Original"), Plt.xticks ([]), Plt.yticks ([]) thePlt.show ()
Python opencv3 grabcut foreground detection