Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clear;
- clc;
- %nlist = {'aerial6.jpg';'aerial10.jpg';'roadA.jpg';'aerial8.jpg'};
- nlist1 = {'all_used/1.png'}; % theshold = 23000
- nlist2 = {'all_used/2.png'}; % theshold =
- nlist3 = {'all_used/3.png'}; % theshold =
- nlist4 = {'all_used/4.png'}; % theshold = 7500
- nlist5 = {'all_used/5.png'}; % theshold = 29000
- 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'};
- nlistDeep = {'datasets/Test_set/Input/18328735_15.tiff'};
- nlistDeep2 = {'datasets/Test_set/Target/18328735_15.tif'};
- threshold_fix = 23000;
- % test 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 = nlist10{cta};
- rgb = imread(nm);
- normImage = im2double(rgb);
- nm_input = rgb2gray(normImage);
- target = imread(nlistDeep2{1});
- % figure, imshow(target);
- % a=imread('test1.jpg');
- % b=imresize(a,[500 800]);
- % 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')
- %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 %%%%%%%%%%%%%%%%%%%%%%%%%
- % numel(s.Area)
- % for i=1:numel(s)
- % s(i).Centroid(1)
- % s(i).Centroid(2)
- % end
- 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;
- % region_matrix_used = [s_num s_area s_perimeter min_perimeter shape_index s_centroid(:,1) s_centroid(:,2)];
- region_matrix_used = [s_num s_area s_perimeter min_perimeter shape_index];
- threshold_use = max(s_area)/2;
- % x(x(:,1)>2 & x(:,1)<6 , :)
- %%AA = region_matrix_used(region_matrix_used(:,5)>2.5);
- %s_area > threshold_use & shape_index > 2.5;
- score = shape_index;
- score2 = s_area;
- num2 = 0;
- area2 = 0;
- shape2 = 0;
- cent1 = 0;
- cent2 = 0;
- j = 1;
- k = 1;
- for i=1:size(s)
- if s_area(i) > threshold_use & shape_index(i) > 2.5;
- 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;
- % figure, imshow(bw4);
- % text(s(i).Centroid(1),s(i).Centroid(2),num2str(score(i)),'FontSize',15,'color','red');
- j = j+1;
- %region_state1 = [num' area' shape'];
- % else
- % q1 = s_num(i);
- % q2 = 0;
- % q3 = 0;
- % q4 = 0;
- % q5 = 0;
- % num(j) = q1;
- % area(j) = q2;
- % shape(j) = q3;
- % centA(j) = q4;
- % centB(j) = q5;
- % % figure, imshow(bw4);
- % % text(s(i).Centroid(1),s(i).Centroid(2),num2str(score(i)),'FontSize',15,'color','red');
- % j = j+1;
- end
- end
- for i=1:size(s)
- %if shape_index(i) > 2.5;
- if s_area(i) < threshold_use & shape_index(i) > 2.5;
- 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;
- % figure, imshow(bw4);
- % text(s(i).Centroid(1),s(i).Centroid(2),num2str(score(i)),'FontSize',15,'color','red');
- k = k+1;
- % else
- % q1 = s_num(i);
- % q2 = 0;
- % q3 = 0;
- % q4 = 0;
- % q5 = 0;
- % num2(k) = q1;
- % area2(k) = q2;
- % shape2(k) = q3;
- % cent1(k) = q4;
- % cent2(k) = q5;
- % % figure, imshow(bw4);
- % % text(s(i).Centroid(1),s(i).Centroid(2),num2str(score(i)),'FontSize',15,'color','red');
- % k = k+1;
- end
- end
- region_state1 = [num' area' shape' centA' centB'];
- region_state2 = [num2' area2' shape2' cent1' cent2'];
- % x = [0,0;1,1];
- % d = pdist(x,'euclidean');
- temp1 = 1;
- all_object = max(size(region_state2(region_state2(:,3)>0)));
- % for i=1:2
- area = region_state1(:,2);
- cen_road1 = region_state1(:,4);
- cen_road2 = region_state1(:,5);
- cen_check1 = region_state2(:,4);
- cen_check2 = region_state2(:,5);
- for i=1:3 %% i = number of checking for local road.
- dis11 = [cen_road1(1),cen_road2(1);cen_check1(i),cen_check2(i)];
- dis12 = [cen_road1(2),cen_road2(2);cen_check1(i),cen_check2(i)];
- d11 = pdist(dis11,'euclidean');
- d12 = pdist(dis12,'euclidean');
- similarity(i) = ((area(1)/(d11^2))+(area(2)/(d12^2)))/2
- end
- % end
- % similarity = similarity;
- region_state3 = [num2' area2' shape2' similarity'];
- iso_num = region_state3(:,1);
- iso_iso = region_state3(:,4);
- indexIso=1;
- for i=1:size(s_num)
- Test(i) = 0;
- for j=1:3 % take isolation following numners of it.
- if i == iso_num(j)
- Test(i) = iso_iso(j);
- end
- end
- end
- region_state4 = [s_num Test'];
- isolate = region_state4(:,2);
- % used_column = region_matrix_used(:,1);
- % test_column = region_state3(:,1);
- % isolate = region_state3(:,7);
- % for i=1:size(region_matrix_used)
- % if used_column == test_column
- % region_matrix_used = [region_matrix_used region_state3(:,7)];
- % end
- % end
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%% Show value on objects %%%%%%%%%%%%%%%%%%%%%%
- % score = (min(sqrt([s.Area]),[s.Perimeter]/4)./(max(sqrt([s.Area]),[s.Perimeter]/4))).^2;
- % score = shape_index;
- % figure, imshow(bw4);
- % for cnt = 1:length(s)
- % text(s(cnt).Centroid(1),s(cnt).Centroid(2),num2str(score(cnt)),'FontSize',15,'color','red');
- % end
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- threshold_max2 = max(region_matrix_used(:,2))
- threshold_max = max(region_matrix_used(:,2))/2
- %shape_index = 2.5;
- %small_selectSh = ((shape_index > 2.5));
- %smallAreaSh( vertcat( s(~small_selectSh).PixelIdxList ) ) = 0; %// set all other regions to zero
- %small_selectA = ((s_area >= threshold_max));
- %smallAreaA( vertcat( s(~small_selectA).PixelIdxList ) ) = 0; %// set all other regions to zero
- small_select = ((s_area >= threshold_max) & (shape_index > 2.5));
- smallArea( vertcat( s(~small_select).PixelIdxList ) ) = 0; %// set all other regions to zero
- MainRoad = (s_area >= threshold_max) & (shape_index > 2.5);
- %small_select2 = ((s_area < threshold_max) & (shape_index > 2.49));
- %smallArea2( vertcat( s(~small_select2).PixelIdxList ) ) = 0; %// set all other regions to zero
- small_select3 = ((shape_index > 2.5)); %all
- smallArea3( vertcat( s(~small_select3).PixelIdxList ) ) = 0; %// set all other regions to zero
- small_select4 = (isolate > 0.4);
- smallArea4( vertcat( s(~small_select4).PixelIdxList ) ) = 0; %// set all other regions to zero
- LocalRoad = (isolate > 0.4);
- small_select5 = (MainRoad + LocalRoad);
- smallArea5( vertcat( s(~small_select5).PixelIdxList ) ) = 0; %// set all other regions to zero
- %figure, imshow( smallAreaSh ); colormap( summer );
- %figure, imshow( smallAreaA ); colormap( summer );
- figure, imshow( smallArea ); colormap( summer );
- %figure, imshow( smallArea2 ); colormap( summer );
- %figure, imshow( smallArea3 ); colormap( summer );
- figure, imshow( smallArea3 ); %all
- figure, imshow( smallArea4 ); colormap( summer );
- figure, imshow( smallArea5 );
- %figure, imshow( ~smallArea4 );
- % if (s_area >= threshold_max) & (shape_index > 2.5)
- % smallArea( vertcat( s(~small_select).PixelIdxList ) ) = 0;
- % coloredLabelA = label2rgb(smallArea, 'hsv', 'g');
- % elseif (s_area < threshold_max) & (shape_index > 2.5)
- % smallArea( vertcat( s(~small_select).PixelIdxList ) ) = 0;
- % coloredLabelA = label2rgb(smallArea, 'hsv', 'b');
- % end
- % figure, imshow( coloredLabelA );
- figure, imshow(nm), hold on, himage = imshow(smallArea5), set(himage, 'AlphaData', 0.6);
- % Comment %%%
- %
- % % Thining the image
- % skeletionization_image = bwmorph(smallArea3,'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( smallArea ); colormap( summer ), hold on
- % % himage = imshow(coloredLabel1);
- % % set(himage, 'AlphaData', 0.7);
- % %
- % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % % % 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);
- % %
- % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %
- % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % % - - - - - - - - - - - - - Find brach I - - - - - - - - - - - - - - - -
- % skelImg = bwmorph(smallArea3, '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),'*');
- %
- % % - - - - - - - - - - - - Find branch II - deleting some node - - - - - -
- % skel2= bwmorph(smallArea3,'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');
- % figure, imshow(skelD);
- % hold all;
- % [y,x] = find(B); plot(x,y,'ro')
- %
- % % figure, imshow(nm), hold on
- % figure, imshow( smallArea3 ); 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);
- % % - - - - - - - - - - - - Find HOUGH lines - - - - - - - - - - - - - -
- % % % rotI = imrotate(smallArea,1,'crop');
- % % BW = edge(smallArea,'canny');
- % % [H,T,R] = hough(BW);
- % % figure, imshow(~BW);
- % % % figure, imshow(H,[],'XData',T,'YData',R,...
- % % % 'InitialMagnification','fit');
- % % xlabel('\theta'), ylabel('\rho');
- % % axis on, axis normal, hold on;
- % % P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
- % % x = T(P(:,2)); y = R(P(:,1));
- % % plot(x,y,'s','color','white');
- % % % Find lines and plot them
- % % lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
- % % figure, imshow(smallArea), hold on
- % % max_len = 0;
- % % for k = 1:length(lines)
- % % xy = [lines(k).point1; lines(k).point2];
- % % plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
- % %
- % % % Plot beginnings and ends of lines
- % % plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
- % % plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
- % %
- % % % Determine the endpoints of the longest line segment
- % % len = norm(lines(k).point1 - lines(k).point2);
- % % if ( len > max_len)
- % % max_len = len;
- % % xy_long = xy;
- % % end
- % % end
- % %
- % % % - - - - - - - - Simplipfy with DP - - - - - - - - - - - - - - - -
- % % % 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')
- %
- %
- %
- %
- %
- % % Comment %%
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement