首页 分水岭算法——MATLAB(Watershed algorithm - MATLAB)

分水岭算法——MATLAB(Watershed algorithm - MATLAB)

举报
开通vip

分水岭算法——MATLAB(Watershed algorithm - MATLAB)分水岭算法——MATLAB(Watershed algorithm - MATLAB) 分水岭算法——MATLAB(Watershed algorithm - MATLAB) % 1. The Compute a segmentation function. This is an image whose dark regions are the objects you are trying to segment. Calculate the partition function. The darker are...

分水岭算法——MATLAB(Watershed algorithm - MATLAB)
分水岭算法——MATLAB(Watershed algorithm - MATLAB) 分水岭算法——MATLAB(Watershed algorithm - MATLAB) % 1. The Compute a segmentation function. This is an image whose dark regions are the objects you are trying to segment. Calculate the partition function. The darker areas in the image are objects to be divided. % 2. Compute foreground markers. These are connected blobs of pixels within each of the objects. 2. Calculate the foreground mark. These are the dot pixels of the internal connections of each object. 3 Compute background markers. These are pixels that are not part of any object. Calculate the background flag. These are pixels that don't belong to any object. % 4. Modify the segmentation function so that it only has minima at the foreground and background marker locations. % 4. Modify the partition function to make it only have a minimum value in the foreground and backview mark position. % 5. Compute the watershed transform of the modified segmentation function. 5. Calculate the watershed transformation for the modified partition function. % Use by Matlab Image Processing Toolbox % use MATLAB image processing toolbox % note: during the period, many functions of image processing toolbox are used, such as fspecial, imfilter, watershed, label2rgb, imopen, % imclose and imreconstruct, imcomplement, imregionalmax, bwareaopen, graythresh, imimposemin function and so on. Read in the Color Image and Convert it to Grayscale % the first step: read the color image and convert it to grayscale image CLC. Clear all; Close all; RGB = imread (' pears. PNG '); If ndims (RGB) = = 3 I = rgb2gray (RGB); The else I = RGB. The end Figure (' units', 'position', 'position', [0, 0, 1]); Subplot (1, 2, 1); Imshow (RGB); The title (' artwork '); Subplot (1, 2, 2); Imshow (I); Title (' grayscale '); Step 2: % Use the Gradient Magnitude as the Segmentation Function % 2: the gradient is used as a partition function % Use the Sobel edge, imfilter, and some simple products to compute the gradient. The gradient is high at The borders of The objects and low (mostly) inside The objects. % use the Sobel edge operator for the horizontal and vertical direction filter, and then calculate the modulus value, Sobel operator to filter the image in the border will be displayed after the larger value, in the absence of border value will be small. Hy = fspecial (' sobel '); Hx = hy '; Iy = imfilter (I), hy, 'get'); Ix = imfilter (I), hx, 'ream'); Gradmag = SQRT (Ix. ^ 2 + Iy. ^ 2); Figure (' units', 'position', 'position', [0, 0, 1]); Subplot (1, 2, 1); Imshow (I, []), title (' grayscale image ') Subplot (1, 2, 2); "Imshow" (gradmag, []), title (' gradient image ') Can you segment the image by using the watershed transform directly on the gradient? Can I use watershed algorithm directly for gradient image image? L = watershed (gradmag); Lrgb = label2rgb (L); Figure (' units', 'position', 'position', [0, 0, 1]); Subplot (1, 2, 1); Imshow (gradmag, []), title (' gradient image ') Subplot (1, 2, 2); Imshow (Lrgb); Title (' gradient amplitude for watershed transformation ') % No. Without additional preprocessing to the as the marker computations below, using the watershed transform directly, the results in "oversegmentation." The results of the watershed algorithm can be over-segmented by using the image of gradient modulus. Therefore, it is usually necessary to mark the foreground objects and background objects separately to get a better segmentation effect. Step 3: % Mark the Foreground Objects Step 3: mark the foreground object % A variety of procedures could be applied here to find the foreground markers, % which must be connected blobs of pixels inside each of the foreground objects. This example you'll use morphological techniques called "opening - by reconstruction" and "closing - by - reconstruction" to "clean" up the image. These operations will create flat maxima inside each object that can be located using imregionalmax. There are multiple ways to apply here to obtain the foreground markup, which must be the connection dot pixels within the foreground object. In this example, we will use morphological techniques "based on open reconstruction" and "closed reconstruction" to clean up images. These operations will create units of maximum value within each object, enabling the use of imregionalmax to locate. % open operation and closed operation: after corrosion, the expansion is called open; After expansion, corrosion is called closure. Open and closed operations can remove specific image details that are smaller than structural elements, while ensuring that there is no global geometric distortion. The % open operation can filter out the little spikes that are smaller than the structural elements, cutting off the long, thin ones and separating them. The closed operation can connect the small gap or hole fill of the structure element, and connect the short intervals. It is an erosion followed by a morphological reconstruction. Let's compare the two. First, compute the opening using imopen. % open operation is an expansion after corrosion, based on open reconstruction (based on the operation of reconstruction), after corrosion, morphological reconstruction is carried out. Let's compare these two ways. First, open an operation with imopen. Se = strel (disk, 20). Io = imopen (I, se); Figure (' units', 'position', 'position', [0, 0, 1]); Subplot (1, 2, 1); Imshow (I, []); Title (' grayscale image '); Subplot (1, 2, 2); Imshow (Io), title (' image operation ') % Next compute the opening - by - reconstruction using imerode and imreconstruct. % next, build on the open reconstruction calculation by corrosion. Ie = imerode (I, se); Iobr = imreconstruct (Ie, I); Figure (' units', 'position', 'position', [0, 0, 1]); Subplot (1, 2, 1); Imshow (I, []); Title (' grayscale image '); Subplot (1, 2, 2); Imshow (Iobr, []), title (' based on open reconstructed image ') Following the opening with a closing can remove the dark spots and stem marks. Compare a regular morphological closing with a closing - by - remission. After the % open operation, the closed operation can be removed to remove the darker spots and boughs. Contrast the normal morphology closure and the closed - based reconstruction operation. First, use imclose: The Ioc = imclose (Io, se); Ic = imclose (I, se); Figure (' units', 'position', 'position', [0, 0, 1]); Subplot (2, 2, 1); Imshow (I, []); Title (' grayscale image '); Subplot (2, 2, 2); Imshow (Io, []); Title (' open operation image '); Subplot (2, 2, 3); Imshow (Ic, []); Title (' closed operation image '); Subplot (2, 2, 4); Imshow (Ioc, []), title (' open closed operation '); % Now use imdilate followed by imreconstruct. Notice you must complement the image inputs and the output of imreconstruct. The complement of the image IM can be a binary, intensity, or RGB image.im2 has the same class and size as IM. % now use imdilate, then use the imreconstruct. Attention must be complementary to the input images, for complementary imreconstruct output images. % IM2 = imcomplement (IM) to compute the complement of image IM. IM can be binary image, or RGB image. IM2 and IM have the same data type and size. Iobrd = imdilate (Iobr, se); Iobrcbr = imreconstruct (imcomplement (Iobrd), imcomplement (Iobr)); Iobrcbr = imcomplement (Iobrcbr); Figure (' units', 'normalized', 'the position', [0 0 1 1)); Subplot (2, 2, 1); Imshow (I, []); Title (' grayscale image '); Subplot (2, 2, 2); Imshow (Ioc, []); Title (' open and closed operation '); Subplot (2, 2, 3); Imshow (Iobr, []); Title (' based on open reconstructed image '); Subplot (2, 2, 4); Imshow (Iobrcbr, []), title (' based on closed reconstruction images'); You can see by comparing Iobrcbr with Ioc, reconstruction and closing are more effective than At the end of the day, the whole of the objects was made up of the whole of the objects. % Calculate the regional maxima of Iobrcbr to obtain good foreground markers. By comparing Iobrcbr and loc, it can be seen that under the application of removing the smudge without affecting the global shape of the object, the open and closed operation based on reconstruction is more effective than the standard open and closed reconstruction. Calculate the local maxims of Iobrcbr to get a better outlook. FGM = imregionalmax (Iobrcbr); Figure (' units', 'position', 'position', [0, 0, 1]); Subplot (1, 3, 1); Imshow (I, []); Title (' grayscale image '); Subplot (1, 3, 2); Imshow (Iobrcbr, []); Title (' open closed operation based on reconstruction '); Subplot (1, 3, 3); Imshow (FGM, []); Title (' local maximum image '); % To help interpret the result, superimpose (stack) the foreground marker image on the the original image. % to help understand this result, the stack foreground is marked to the original. It1 = RGB (:, : 1); It2 = RGB (:, 2); It3 = RGB (:, :, 3); It1 (FGM) = 255; It2 (FGM) = 0; It3 (FGM) = 0; I2 = cat (3, It1, It2, It3); Figure (' units', 'position', 'position', [0, 0, 1]); Subplot (2, 2, 1); Imshow (RGB, []); Title (' original image '); Subplot (2, 2, 2); Imshow (Iobrcbr, []); Title (' open closed operation based on reconstruction '); Subplot (2, 2, 3); Imshow (FGM, []); Title (' local maximum image '); Subplot (2, 2, 4); Imshow (I2); Title (' local maximum superimposed to the original image '); % Notice that some of the mostly - occluded and Shadowed objects are not marked, which means that these objects will not be segmented properly in the end result. % of the foreground markers in some objects go right up to the objects' edge. That means you should clean the edges of the marker blobs and then shrink them a bit. You can do this by a closing followed by an erosion. % note that most closed and shadow objects are not marked, which means that these objects will not be properly partitioned in the results. Furthermore, the foreground markup of some objects will always be on the edge of the object. This means clearing the edges of the mark spots and then shrinking them. It can be done through closed operations and corrosion operations. Se2 = strel (ones (5, 5)); Fgm2 = imclose (FGM, se2); Fgm3 = imerode (fgm2 se2); Figure (' units', 'position', 'position', [0, 0, 1]); Subplot (2, 2, 1); Imshow (Iobrcbr, []); Title (' open closed operation based on reconstruction '); Subplot (2, 2, 2); Imshow (FGM, []); Title (' local maximum image '); Subplot (2, 2, 3); Imshow (fgm2, []); Title (' closed operation '); Subplot (2, 2, 4); Imshow (fgm3, []); Title (' corrosion operation '); This procedure tends to leave some stray pixels that must be removed. You can do This using bwareaopen. % which removes all blobs that have fewer than a certain number of pixels. BW2 = bwareaopen (BW, P) removes the from A binary image all connected components (objects) that have fewer than P pixels, producing another binary image, BW2. This process will leave some isolated pixels that are off and should be removed. Bwareaopen can be used to remove spots that are less than a certain number of pixels. % BW2 = bwareaopen (BW, P) removes the connected block of the value of P pixel from the binary image, and obtains another binary image BW2. Fgm4 = bwareaopen (fgm3, 20); It1 = RGB (:, : 1); It2 = RGB (:, 2); It3 = RGB (:, :, 3); It1 (fgm4) = 255; It2 (fgm4) = 0; It3 (fgm4) = 0; I3 = cat (3, It1, It2, It3); Figure (' units', 'position', 'position', [0, 0, 1]); Subplot (2, 2, 1); Imshow (I2, []); Title (' local maximum superimposed to the original image '); Subplot (2, 2, 2); Imshow (fgm3, []); Title (' closed etching operation '); Subplot (2, 2, 3); Imshow (fgm4, []); Title (' remove speck operation '); Subplot (2, 2, 4); Imshow (I3, []); Title (' modify local maximum superimposed to the original image '); % Step 4: Compute Background Markers Now you need to mark the background. In the cleaner-up image, the dark pixels belong to the background, so you could start with a thresholding operation. Step 4: calculate the background tag % now, you need to mark the background. In the image Iobrcbr after cleaning, the dark pixels belong to the background, so you can start from the threshold operation. Bw = im2bw (Iobrcbr, graythresh (Iobrcbr)); Figure (' units', 'position', 'position', [0, 0, 1]. Subplot (1, 2, 1); Imshow (Iobrcbr, []); Title (' open closed operation based on reconstruction '); Subplot (1, 2, 2); Imshow (bw, []); Title (' threshold segmentation '); % The background pixels are in black, but ideally we don 't want The background markers to be too close to The edges of The objects we are trying to segment. % We 'l l "thin" the background by computing the "skeleton by influence zones", or SKIZ, of the foreground of bw. This can be done by computing the watershed transform of the distance transform of bw. (D) and then looking for the watershed ridge lines (DL = 0) of the result. D = bwdist (BW) computes the Euclidean distance transform of the binary image BW. % For each pixel in BW, the distance transform assigns a number that is the distance between that pixel and the nearest nonzero pixel of BW. The Euclidean distance metric by default.bw can have any dimension. The % background pixels are in the black area, but ideally, you don't need to require background tags to be too close to the edges of the object being split. By computing the "skeleton impact range" to "refine" the background, or the prospect of SKIZ, bw. % this can be achieved by calculating the watershed transformation of bw's distance transformation, and then looking for the watershed ridge line of the results (DL = = 0). D = bwdist (BW) calculates the Euclidean matrix of BW of binary image. % for each pixel of BW, the distance between the specified pixel and the nearest BW non-zero pixel. Bwdist USES Euclidean distance formula by default. BW can be of any dimension, D is the same size as BW. D = bwdist (bw); DL = watershed (D); BGM = DL = = 0; Figure (' units', 'position', 'position', [0, 0, 1]); Subplot (2, 2, 1); Imshow (Iobrcbr, []); Title (' open closed operation based on reconstruction '); Subplot (2, 2, 2); Imshow (bw, []); Title (' threshold segmentation '); Subplot (2, 2, 3); Imshow (label2rgb (DL), []); Title (' watershed transformation diagram '); Subplot (2, 2, 4); Imshow (BGM, []); Title (' watershed transform ridge map '); % Step 5: Compute the Watershed Transform of the Segmentation Function. The function imimposemin can be used to modify an image so that it has regional minima only in certain desired locations. Here you can use imimposemin to modify the gradient magnitude image so that its only regional minima occur at foreground and background marker pixels. Step 5: calculate the watershed transformation of the partition function The % function imimposemin can be used to modify the image to make it only locally minimal at a specific request position. Imimposemin can be used to modify the gradient image so that it only has a local minimum in the foreground and rear view. Gradmag2 = imimposemin (gradmag, BGM | fgm4); Figure (' units', 'position', 'position', [0, 0, 1]); Subplot (2, 2, 1); Imshow (BGM, []); Title (' watershed transform ridge map '); Subplot (2, 2, 2); Imshow (fgm4, []); Title (' foreground marker '); Subplot (2, 2, 3); Imshow (gradmag, []); Title (' gradient image '); Subplot (2, 2, 4); Imshow (gradmag2, []); Title (' modify gradient image '); % the Finally we are ready to compute the watershed - -based segmentation. Finally, the image segmentation based on the watershed can be calculated. % Step 6: Visualize the Result One % the visualization technique is to superimpose the foreground markers, background markers, and segmented object boundaries on the the original image. You can use dilation as needed to make certain aspects, such as the object boundaries, more visible. Object boundaries are located where L = = 0. Step 6: view the results % one visualization technique is to overlay foreground markers, background markers, split object boundaries to initial images. You can use the expansion to implement certain requirements, such as object boundaries, to be more visible. The object boundary is positioned at the position of L = = 0. L = watershed (gradmag2); It1 = RGB (:, : 1); It2 = RGB (:, 2); It3 = RGB (:, :, 3); Fgm5 = imdilate (L = 0, ones (3, 3). It1 (fgm5) = 255; It2 (fgm5) = 0; It3 (fgm5) = 0; I4 = cat (3, It1, It2, It3); Figure (' units', 'position', 'position', [0, 0, 1]); Subplot (1, 2, 1); Imshow (RGB, []); Title (' original image '); Subplot (1, 2, 2); Imshow (i2, []); Title (' tag and object edge superimposed to the original image '); % This visualization illustrates how the locations of the foreground and background markers affect the result. To send In a locations, % partially occluded part objects were merged with their brighter neighbor objects because the occluded objects did not have foreground markers. % visualization shows how the foreground and the back - view markers affect the results. In several locations, some of the darker objects fuse with the brighter adjacent objects that are adjacent to them, because the occluded objects have no foreground flag. Another useful visualization technique is to display the label matrix as a color image.label matrices, such as those produced by watershed and bwlabel, % can be converted to truecolor images for the visualization purposes by using label2rgb. Another useful visualization technique is to display the tag matrix as a color image. The tag matrix, such as those obtained through watershed and bwlabel, can be converted to a true color image using label2rgb. Lrgb = label2rgb (L, 'jet', 'w', 'shuffle'); Figure (' units', 'position', 'position', [0, 0, 1]); Subplot (1, 2, 1); Imshow (RGB, []); Title (' original image '); Subplot (1, 2, 2); Imshow (Lrgb); Title (' color watershed marking matrix '); You can use transparency to superimpose this pseudo-color label matrix on top of the original intensity image. % can use transparency to overlay the pseudo-color markup matrix on the original brightness image. Figure (' units', 'position', 'position', [0, 0, 1]); Subplot (1, 2, 1); Imshow (RGB, []); Title (' original image '); Subplot (1, 2, 2); Imshow (RGB, []); Hold on. Himage = imshow (Lrgb); Set (himage, 'AlphaData, 0.3); Title (' tag matrix overlay to the original image ');
本文档为【分水岭算法——MATLAB(Watershed algorithm - MATLAB)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_614050
暂无简介~
格式:doc
大小:53KB
软件:Word
页数:17
分类:生活休闲
上传时间:2017-10-27
浏览量:108