PYTHON+OPENCV image Processing (v)--roi and flood fill

Source: Internet
Author: User
Tags function prototype

First, ROI

ROI (Region of interest), area of interest. Machine vision, image processing, from the processed images in boxes, circles, ellipses, irregular polygons and other ways to outline the areas to be processed, known as the area of interest, ROI.

The code is as follows:

#to capture, merge, and fill picturesImportCv2 as Cvsrc=cv.imread ('E:\imageload\lena.jpg') Cv.namedwindow ('First_image', CV. window_autosize) Cv.imshow ('First_image', SRC) face= src[200:300, 200:400]#Select 200:300 row, 200:400 column area as intercept objectGray = Cv.cvtcolor (Face, CV. Color_rgb2gray)#The resulting grayscale is a single-channel imageBackface = Cv.cvtcolor (Gray, CV. COLOR_GRAY2BGR)#converts a single-channel image to a three-channel RGB grayscale image because only three-channel backface can be assigned to three-channel SRCsrc[200:300, 200:400] =Backfacecv.imshow (" Face", SRC) cv.waitkey (0) cv.destroyallwindows ()

Operation Result:

Note: Color_rgb2gray is converting a three-channel RGB object into a single-channel grayscale object

Second, flood fill (color image fill)

The code is as follows:

#Flood Fill (color image fill)ImportCv2 as CVImportNumPy as NPdefFill_color_demo (image): Copyimg=image.copy () H, W= Image.shape[:2] Mask= Np.zeros ([h+2, W+2],np.uint8)#mask must be both row and column plus 2, and must be a uint8 single-channel array    #Why add 2 This is understandable: when a flood fill scan starts from 0 rows and 0 columns, the mask-out 2 guarantees that the pixels on the scanned boundary will be processedCv.floodfill (Copyimg, Mask, (220, 250), (0, 255, 255), (100, 100, 100), (50, 50, 50), CV. Floodfill_fixed_range) Cv.imshow ("Fill_color_demo", copyimg) src= Cv.imread ('e:/imageload/baboon.jpg') Cv.namedwindow ('Input_image', CV. window_autosize) Cv.imshow ('Input_image', SRC) fill_color_demo (src) cv.waitkey (0) cv.destroyallwindows ()

Operation Result:

Attention:

The mask in 1.OPENCV is a single channel array for the UIN8 type

2. Flood fill algorithm is also called diffuse water filling algorithm. OpenCV FloodFill function Prototype: FloodFill (image, Mask, Seedpoint, newval[, lodiff[, updiff[, flags]]), retval, image, Mask, re Ct

The image parameter represents input/output 1 or 3 channels, 8-bit or floating-point images.

The Mask parameter represents a bitmask, which is a single-channel 8-bit image, 2 pixels more than the height of the image, and 2 pixels in width. The padding cannot pass through a non-0 pixel in the input mask.

The Seedpoint parameter represents the starting point of the flood algorithm (diffuse fill algorithm).

The newval parameter represents the new value in the redraw area pixel.

The Lodiff parameter represents the maximum value of the luminance or color difference between the current observed pixel value and its part neighborhood pixel values or the seed pixels to be added to the component.

The Updiff parameter represents the maximum value of the luminance or color difference between the current observed pixel value and its part neighborhood pixel values or the seed pixels to be added to the component.

Flags parameter: Action marker, contains three parts: (Reference https://www.cnblogs.com/little-monkey/p/7598529.html)

Low eight-bit (0~7 bit): Used to control the connectivity of the algorithm, preferably 4 (default) or 8.

Middle eight bit (8~15 bit): Used to specify the value of the mask image, but if the middle eight bit is 0 The mask is populated with one.

High eight-bit (16~32 bit): can be 0 or a combination of the following two types of markers:

Floodfill_fixed_range: Indicates that this flag takes into account the difference between the current pixel and the seed pixel, otherwise the difference between the current pixel and the neighboring pixel is considered. Floodfill_mask_only: Indicates that the function does not go to fill the original image, but instead fills the mask image mask,mask the specified position is zero and does not fill with zero.

3. Personal understanding: The pixel value of the starting point of the parameter 3 minus the pixel value of parameter 5 represents the lowest value of the pixel that is searched for the perimeter range starting from the starting point, and the pixel value of the starting point of the parameter 3 plus the pixel value of parameter 5 represents the maximum number of pixels from the starting point to search for the perimeter range. With this range, the function can then fill the specified color newval parameter values within this contiguous range of pixels.

4. Set floodfill_fixed_range– change image, flood fill

Setting floodfill_mask_only– does not change the image, only fills the mask layer itself, ignoring the new color value parameter

Three, flood fill (binary image fill)

The code is as follows:

#Flood Fill (two-value image fill)ImportCv2 as CVImportNumPy as NPdeffill_binary (): Image= Np.zeros ([400, 400, 3], np.uint8) image[100:300, 100:300] = 255Cv.imshow ("fill_binary", image) Mask= Np.ones ([402, 402], np.uint8)#mask to ensure that the original image is higher and wider than 2mask[101:301, 101:301] =0 Cv.floodfill (image, Mask, (200,200), (255, 0, 0), CV. FLOODFILL_MASK_ONLY)#Mask is not 0 of the area will not be filled, mask is 0 of the area will be filledCv.imshow ("filled_binary", image) Fill_binary () cv.waitkey (0) cv.destroyallwindows ()

Operation Result:

Attention:

1. Personal opinion, whether it is floodfill_fixed_range or floodfill_mask_only operation, flood fill will not fill the mask masks of non-0 pixel area

2. mask[101:301, 101:301] = 0 Why is this statement 101:301 instead of 100:300? I think it should be masking mask is more than the original image left and right up to 1, so mask masks about 2 more than the original image, up and down also more than the original image 2. Then 100 of the original image will naturally correspond to 101 of the mask, the same 300 of the original image will naturally correspond to the mask of 301.

3. When the floodfill_mask_only is set, the original image will not change and will only fill the MASK with the median eight digits. The middle eight-bit value of the FloodFill flags parameter is used to specify the value of the fill mask image, but if the value of the middle eight bit of the flags is 0, the mask is populated with one.

PYTHON+OPENCV image Processing (v)--roi and flood fill

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.