clc clear all close all rgb=imread('C:\Users\admin\Desktop\images\2.jpg'); imshow(rgb) I = rgb2gray(rgb); imshow(I) I=adapthisteq(I);
hy = fspecial('sobel'); hx = hy'; Iy = imfilter(double(I), hy, 'replicate'); Ix = imfilter(double(I), hx, 'replicate'); gradmag = sqrt(Ix.^2 + Iy.^2); figure, imshow(gradmag,), title('Gradient magnitude (gradmag)')
L = watershed(gradmag); Lrgb = label2rgb(L); %figure, imshow(Lrgb), title('Watershed transform of gradient magnitude (Lrgb)')
se = strel('disk',3); Io = imopen(I, se); figure, imshow(Io), title('Opening (Io)')
Ie = imerode(I, se); Iobr = imreconstruct(Ie, I); figure, imshow(Iobr), title('Opening-by-reconstruction (Iobr)') Ioc = imclose(Io, se); figure, imshow(Ioc), title('Opening-closing (Ioc)') Iobrd = imdilate(Iobr, se); Iobrcbr = imreconstruct(imcomplement(Iobrd), imcomplement(Iobr)); Iobrcbr = imcomplement(Iobrcbr); figure, imshow(Iobrcbr), title('Opening-closing by reconstruction (Iobrcbr)') fgm = imregionalmax(Iobrcbr); %figure, imshow(fgm), title('Regional maxima of opening-closing by reconstruction (fgm)') I2 = I; I2(fgm) = 255; figure, imshow(I2), title('Regional maxima superimposed on original image (I2)') se2 = strel(ones(5,5)); fgm2 = imclose(fgm, se2); fgm3 = imerode(fgm2, se2);
fgm4 = bwareaopen(fgm3, 20); I3 = I; I3(fgm4) = 255; figure, imshow(I3) title('Modified regional maxima superimposed on original image (fgm4)') bw = im2bw(Iobrcbr, graythresh(Iobrcbr)); %figure, imshow(bw), title('Thresholded opening-closing by reconstruction (bw)') D = bwdist(bw,'euclidean'); DL = watershed(D); bgm = DL == 0; %figure, imshow(bgm), title('Watershed ridge lines (bgm)') gradmag2 = imimposemin(gradmag, bgm | fgm4); L = watershed(gradmag2); I4 = I; I4(imdilate(L == 0, ones(3, 3)) | bgm | fgm4) = 255; figure, imshow(I4) title('Markers and object boundaries superimposed on original image (I4)') Lrgb = label2rgb(L, 'jet', 'w', 'shuffle'); figure, imshow(Lrgb) title('Colored watershed label matrix (Lrgb)') figure, imshow(I), hold on himage = imshow(Lrgb); set(himage, 'AlphaData', 0.3); title('Lrgb superimposed transparently on original image')
[Merged information from duplicate question]
I hav tried to implement marker controlled watershed segmentation algorithm but the trees are not delineated correctly.plz help me with code.i hav to count no.of trees in a setellite image.is there any other alg to do this
No products are associated with this question.
I'd say that it is impossible for that photo. I can't even tell. It's just one big solid mass of green. You can't tell because you have tall trees and short trees and their branches overlap and penetrate neighboring trees, plus there's probably lots of underbrush in there as well. Any segmentation you did get probably wouldn't be very accurate. Why do you want the individual trees anyway?
There may be some hope if the trees are well separated or you have different species with different spectra. See the VisonBib: http://iris.usc.edu/Vision-Notes/bibliography/contentscartog.html#Cartography,%20Aerial%20Images,%20Remote%20Sensing,%20Buildings,%20Roads,%20Terrain,%20ATR
See these sections:
22.5.4 Trees, Individual Trees, Tree Crowns 22.214.171.124 Trees, Forest, Stem Volume, Biomass Measurements 126.96.36.199 Forest Extraction, Forest Analysis