Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clear;
- clc;
- nlist1 = {'all_used/1.png'};
- nlist2 = {'all_used/2.png'};
- nlist3 = {'all_used/3.png'};
- nlist4 = {'all_used/4.png'};
- nlist5 = {'all_used/5.png'};
- nlist6 = {'all_used/6.png'};
- nlist7 = {'all_used/7.png'};
- nlist8 = {'all_used/8.png'};
- nlist9 = {'all_used/9.png'};
- nlist10 = {'all_used/10.png'};
- shape_value = 2.25;
- isolation_value = 0.4;
- isolation_value_based = 0.2;
- picture = nlist1; % Choose for test picture.
- % test function kmeans
- X = [randn(20,2)+ones(20,2); randn(20,2)-ones(20,2)];
- opts = statset('Display','final');
- [cidx, ctrs] = kmeans(X, 4, 'Distance','city', ...
- 'Replicates',5, 'Options',opts);
- for cta = 1:1 % all in my img folder is 7
- nm = picture{cta};
- rgb = imread(nm);
- normImage = im2double(rgb);
- nm_input = rgb2gray(normImage);
- % Step 1: Read Image
- img_resize = imresize(rgb, 1);
- he = img_resize;
- he2 = img_resize;
- % figure, imshow(he), title('1'); % Showing a original image.
- img_graythresh = rgb2gray(he );
- threshold = graythresh(img_graythresh);
- bw_img = im2bw(he,threshold);
- % figure, imshow(bw_img), title('2');
- % figure, imhist(img_graythresh), title('3');
- % Step 2: Convert Image from RGB Color Space to L*a*b* Color Space
- cform = makecform('srgb2lab');
- cform_rgb = makecform('lab2srgb');
- lab_he = applycform(he,cform);
- rgb_he = applycform(he,cform_rgb);
- % Step 3: Classify the Colors in 'a*b*' Space Using K-Means Clustering
- ab = double(rgb_he(:,:,2:3));
- ab_all_band = double(rgb_he(:,:,2:3));
- nrows = size(ab,1);
- ncols = size(ab,2);
- ab = reshape(ab,nrows*ncols,2);
- nColors = 3;
- [cluster_idx, cluster_center] = kmeans(ab,nColors, ...
- 'Replicates',1, ...
- 'Options',opts, ...
- 'start',[118 143; 127 140; 133 127]);
- % 'start',[118 143; 127 140; 133 127]);
- % Step 4: Label Every Pixel in the Image Using the Results from KMEANS
- pixel_labels = reshape(cluster_idx,nrows,ncols);
- %figure(2), imshow(pixel_labels,[]), title('image labeled by cluster index')
- % Step 5: Create Images that Segment the H&E Image by Color.
- segmented_images = cell(1,3);
- rgb_label = repmat(pixel_labels,[1 1 3]);
- for k = 1:3
- color = rgb;
- color(rgb_label ~= k) = 0;
- segmented_images{k} = color;
- end
- %figure, imshow(segmented_images{1}), title('objects in cluster 1')
- %figure, imshow(segmented_images{2}), title('objects in cluster 2')
- figure, imshow(segmented_images{3}), title('objects in cluster 3') % Show only grey cluster (road cluster)
- %Normaliztion for clustering road area.
- normImage_c3 = mat2gray(segmented_images{3});
- normImage_c3_gray =rgb2gray(segmented_images{3});
- %figure, imhist(normImage_c3_gray);
- % to Binary image
- gray_convert_image = rgb2gray(normImage_c3);
- binary_convert_image = im2bw(gray_convert_image);
- figure, imshow(binary_convert_image)
- bwarea(binary_convert_image);
- %bw = imread('text.tif');
- %se = strel('line',20,150);
- se = strel('line',10,10);
- bw2 = imdilate(binary_convert_image,se);% graythresh = 'bw_img' % kmeans = 'binary_convert_image'
- %imshow(bw), title('Original')
- figure, imshow(bw2), title('Dilated')
- bw3 = ~bw2;
- bw4 = im2bw(bw3);
- %figure, imshow(bw4);
- CC = bwconncomp(bw2); %Connected Component Finding all area.
- %CC = bwconncomp(my_image);
- L = labelmatrix(CC);
- A = cell( size(CC.PixelIdxList,1) , size(CC.PixelIdxList,2) );
- A = CC.PixelIdxList;
- size(CC.PixelIdxList,2);
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- La=bwlabel(bw2,8); %%% labeledImage is a binary image
- figure,imshow(La,[]);
- coloredLabel = label2rgb(La, 'hsv', 'k', 'shuffle');
- imshow(coloredLabel);
- % pr = regionprops( La, 'Area', 'PixelIdxList' );
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- stats = regionprops(bw2,'Area')
- stats2 = regionprops(bw2,'Centroid')
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- smallAreaA = La;
- smallAreaSh = La;
- smallArea = La;
- smallArea2 = La;
- smallArea3 = La;
- smallArea4 = La;
- smallArea5 = La;
- s = regionprops(La, 'Orientation','Area','PixelIdxList','Perimeter', 'BoundingBox',...
- 'MajorAxisLength', 'MinorAxisLength', 'Eccentricity', 'Centroid');
- %%%%%%%%%%%%%%%%%%%%%%% Regionprop to matrix %%%%%%%%%%%%%%%%%%%%%%%%%
- num_boject = 1:1:size(s);
- s_centroid = vertcat(s.Centroid);
- cen1 = s_centroid(:,1);
- cen2 = s_centroid(:,2);
- region_matrix = [s.Area; s.Perimeter; num_boject;]';
- s_area = region_matrix(:,1);
- s_perimeter = region_matrix(:,2);
- s_num = region_matrix(:,3);
- min_perimeter = (sqrt(region_matrix(:,1)))*4;
- shape_index = s_perimeter./min_perimeter; % formula of shape index
- % region_matrix_used will contain 1. number of objects 2. perimeter and
- % min_perimeter and calculating to shape_index
- region_matrix_used = [s_num s_area s_perimeter min_perimeter shape_index];
- threshold_use = max(s_area)/2;
- score = shape_index;
- score2 = s_area;
- num2 = 0;
- area2 = 0;
- shape2 = 0;
- cent1 = 0;
- cent2 = 0;
- j = 1;
- k = 1;
- % Loop 1 for "region_state1"
- for i=1:size(s)
- if s_area(i) > threshold_use & shape_index(i) > shape_value;
- q1 = s_num(i);
- q2 = s_area(i);
- q3 = shape_index(i);
- q4 = cen1(i);
- q5 = cen2(i);
- num(j) = q1;
- area(j) = q2;
- shape(j) = q3;
- centA(j) = q4;
- centB(j) = q5;
- j = j+1;
- end
- end
- % Loop 2 for "region_state2"
- for i=1:size(s)
- if s_area(i) < threshold_use & shape_index(i) > shape_value;
- q1 = s_num(i);
- q2 = s_area(i);
- q3 = shape_index(i);
- q4 = cen1(i);
- q5 = cen2(i);
- num2(k) = q1;
- area2(k) = q2;
- shape2(k) = q3;
- cent1(k) = q4;
- cent2(k) = q5;
- k = k+1;
- end
- end
- region_state1 = [num' area' shape' centA' centB']; % Find Main Road
- region_state2 = [num2' area2' shape2' cent1' cent2']; % Find Local Road
- temp1 = 1;
- all_object = max(size(region_state2(region_state2(:,3)>0)));
- area = region_state1(:,2);
- cen_road1 = region_state1(:,4);
- cen_road2 = region_state1(:,5);
- cen_check1 = region_state2(:,4);
- cen_check2 = region_state2(:,5);
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%% Formula of Isolation Index %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- isolation_form = 0;
- num_main_road = size(num',1)
- num_local_road = size(num2',1)
- for local=1:num_local_road %% i = number of checking for local road.
- for main=1:num_main_road
- distance = [cen_road1(main),cen_road2(main);cen_check1(local),cen_check2(local)];
- dis = pdist(distance,'euclidean');
- isolation_form = isolation_form +area(main)/(dis^2);
- end
- similarity(local) = isolation_form / num_main_road ;
- isolation_form = 0;
- end
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- region_state3 = [num2' area2' shape2' similarity']; % Calculating to be a similarity index
- iso_num = region_state3(:,1);
- iso_iso = region_state3(:,4);
- indexIso=1;
- % This loop was created for keep similarity index to matrix.
- for i=1:size(s_num)
- Test(i) = 0;
- for j=1:num_local_road % take isolation following numners of it.
- if i == iso_num(j)
- Test(i) = iso_iso(j);
- end
- end
- end
- region_state4 = [s_num Test']; % Created for checking above loop.
- isolate = region_state4(:,2);
- threshold_max = max(region_matrix_used(:,2))/2
- small_select = ((s_area >= threshold_max) & (shape_index > shape_value));
- smallArea( vertcat( s(~small_select).PixelIdxList ) ) = 0; %// set all other regions to zero
- % This is Main Road.
- MainRoad = (s_area >= threshold_max) & (shape_index > shape_value);
- small_select3 = ((shape_index > shape_value)); %all
- smallArea3( vertcat( s(~small_select3).PixelIdxList ) ) = 0;
- small_select4 = (isolate > isolation_value_based);
- smallArea4( vertcat( s(~small_select4).PixelIdxList ) ) = 0;
- % This is Local Road.
- LocalRoad = (isolate > isolation_value);
- % This is Results.
- small_select5 = (MainRoad + LocalRoad);
- smallArea5( vertcat( s(~small_select5).PixelIdxList ) ) = 0;
- figure, imshow( smallArea ); colormap( summer ); % Main_Road Result
- figure, imshow( smallArea3 ); %all
- figure, imshow( smallArea4 ); colormap( summer ); % Local_Road Result
- figure, imshow( smallArea5 ); % Correct_Road Result
- figure, imshow(nm), hold on, himage = imshow(smallArea5), set(himage, 'AlphaData', 0.6);
- %%%%%%%%%%%%%%%%%%%%%% Thining the image %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % Thining the image
- skeletionization_image = bwmorph(smallArea5,'thin',Inf);
- se2 = strel('line',2,2); % 'line',5,8 'best',2,2
- bw2 = imdilate(skeletionization_image,se2);
- coloredLabel1 = label2rgb(bw2, 'hsv', 'k', 'shuffle');
- % figure, imshow(coloredLabel1);
- % ps = dpsimplify(skeletionization_image,1);
- % SHOW ---------------- Thining the image ---------------------
- figure, imshow( smallArea5 ); colormap( summer ), hold on
- himage = imshow(coloredLabel1);
- set(himage, 'AlphaData', 0.7);
- % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %
- % %%%%%%%%%%%%%%% Skeleton the image (Not using in this paper) %%%%%%%
- %
- % % Skeleton the image
- % skeletionization_image = bwmorph(smallArea,'skel',Inf);
- % se2 = strel('line',2,2); % 'line',5,8 'best',2,2
- % bw2 = imdilate(skeletionization_image,se2);
- % coloredLabel2 = label2rgb(bw2, 'hsv', 'k', 'shuffle');
- % % figure, imshow(coloredLabel1);
- % % ps = dpsimplify(skeletionization_image,1);
- %
- % % SHOW ---------------- Skeleton the image ---------------------
- % figure, imshow(nm), hold on
- % himage = imshow(coloredLabel2);
- % set(himage, 'AlphaData', 0.7);
- %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%% Simplipfy with DP %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % - - - - - - - - - - - - - Find brach I - - - - - - - - - - - - - - - -
- skelImg = bwmorph(smallArea5, 'thin', 'inf');
- branchImg = bwmorph(skelImg, 'branchpoints');
- endImg = bwmorph(skelImg, 'endpoints');
- [row, column] = find(endImg);
- endPts = [row column];
- [row, column] = find(branchImg);
- branchPts = [row column];
- figure; imshow(skelImg); hold on;
- plot(branchPts(:,2),branchPts(:,1),'r*'); hold on;
- plot(endPts(:,2),endPts(:,1),'*');
- % - - - - Simplipfy with DP - - Find branch II - deleting some node - - %
- skel2= bwmorph(smallArea5,'skel',Inf);
- B = bwmorph(skel2, 'branchpoints');
- E = bwmorph(skel2, 'endpoints');
- [y,x] = find(E);
- B_loc = find(B);
- Dmask = false(size(skel2));
- for k = 1:numel(x)
- D = bwdistgeodesic(skel2,x(k),y(k));
- distanceToBranchPt = min(D(B_loc));
- Dmask(D < distanceToBranchPt) =true;
- end
- skelD = skel2 - Dmask;
- coloredLabel2 = label2rgb(skelD, 'hsv', 'k', 'shuffle');
- % skelC = dpsimplify(skelD, 0.5); %Simplipfy with DP
- figure, imshow(skelD);
- hold all;
- [y,x] = find(B); plot(x,y,'ro')
- % figure, imshow(nm), hold on
- figure, imshow( smallArea5 ); colormap( summer ), hold on % my output
- himage = imshow(coloredLabel2);
- set(himage, 'AlphaData', 0.7);
- % figure, imshow( smallArea ), hold on % my output
- % himage = imshow(coloredLabel2);
- % set(himage, 'AlphaData', 0.7);
- % - - - - - - - - Simplipfy with DP (Uisng function dpsimplify) - - - - - - - - - -
- %
- % B2 = num2cell(B);
- % tol = 0.5;
- % ps = dpsimplify(B2,tol);
- % hold on
- % figure, plot(ps(:,1),ps(:,2),'r','LineWidth',2);
- % legend('original polyline','simplified')
- %%%%%%%% Pseudocode (refer to
- %%%%%%%% https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm)
- %%%%%%%%
- % function DouglasPeucker(PointList[], epsilon)
- % // Find the point with the maximum distance
- % dmax = 0
- % index = 0
- % end = length(PointList)
- % for i = 2 to ( end - 1) {
- % d = perpendicularDistance(PointList[i], Line(PointList[1], PointList[end]))
- % if ( d > dmax ) {
- % index = i
- % dmax = d
- % }
- % }
- % // If max distance is greater than epsilon, recursively simplify
- % if ( dmax > epsilon ) {
- % // Recursive call
- % recResults1[] = DouglasPeucker(PointList[1...index], epsilon)
- % recResults2[] = DouglasPeucker(PointList[index...end], epsilon)
- %
- % // Build the result list
- % ResultList[] = {recResults1[1...length(recResults1)-1], recResults2[1...length(recResults2)]}
- % } else {
- % ResultList[] = {PointList[1], PointList[end]}
- % }
- % // Return the result
- % return ResultList[]
- % end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement