Advertisement
pongfactory

Landscape paper coding (v1_8august2016)

Aug 7th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 14.23 KB | None | 0 0
  1. clear;
  2. clc;
  3. nlist1 = {'all_used/1.png'};  
  4. nlist2 = {'all_used/2.png'};
  5. nlist3 = {'all_used/3.png'};
  6. nlist4 = {'all_used/4.png'};
  7. nlist5 = {'all_used/5.png'};  
  8. nlist6 = {'all_used/6.png'};
  9. nlist7 = {'all_used/7.png'};
  10. nlist8 = {'all_used/8.png'};
  11. nlist9 = {'all_used/9.png'};
  12. nlist10 = {'all_used/10.png'};
  13.  
  14. shape_value = 2.25;
  15. isolation_value = 0.4;
  16. isolation_value_based = 0.2;
  17. picture = nlist1; % Choose for test picture.
  18.  
  19. % test function kmeans
  20. X = [randn(20,2)+ones(20,2); randn(20,2)-ones(20,2)];
  21.       opts = statset('Display','final');
  22.       [cidx, ctrs] = kmeans(X, 4, 'Distance','city', ...
  23.                             'Replicates',5, 'Options',opts);
  24.  
  25. for cta = 1:1 % all in my img folder is 7
  26.     nm = picture{cta};
  27.     rgb = imread(nm);
  28.     normImage = im2double(rgb);
  29.     nm_input = rgb2gray(normImage);  
  30.  
  31. % Step 1: Read Image
  32.     img_resize = imresize(rgb, 1);
  33.     he = img_resize;
  34.     he2 = img_resize;
  35.  %  figure, imshow(he), title('1'); % Showing a original image.
  36.    
  37.     img_graythresh = rgb2gray(he );
  38.     threshold = graythresh(img_graythresh);
  39.     bw_img = im2bw(he,threshold);
  40. %   figure, imshow(bw_img), title('2');
  41. %  figure, imhist(img_graythresh), title('3');
  42.  
  43. % Step 2: Convert Image from RGB Color Space to L*a*b* Color Space
  44.     cform = makecform('srgb2lab');
  45.     cform_rgb = makecform('lab2srgb');
  46.     lab_he = applycform(he,cform);
  47.     rgb_he = applycform(he,cform_rgb);
  48.    
  49. % Step 3: Classify the Colors in 'a*b*' Space Using K-Means Clustering
  50.     ab = double(rgb_he(:,:,2:3));
  51.     ab_all_band = double(rgb_he(:,:,2:3));
  52.     nrows = size(ab,1);
  53.     ncols = size(ab,2);
  54.     ab = reshape(ab,nrows*ncols,2);
  55.      
  56.     nColors = 3;
  57.     [cluster_idx, cluster_center] = kmeans(ab,nColors, ...
  58.                                     'Replicates',1, ...
  59.                                     'Options',opts, ...
  60.                                     'start',[118 143; 127 140; 133 127]);
  61.                                 % 'start',[118 143; 127 140; 133 127]);
  62.                          
  63. % Step 4: Label Every Pixel in the Image Using the Results from KMEANS
  64.     pixel_labels = reshape(cluster_idx,nrows,ncols);
  65.     %figure(2), imshow(pixel_labels,[]), title('image labeled by cluster index')
  66.    
  67. % Step 5: Create Images that Segment the H&E Image by Color.
  68.     segmented_images = cell(1,3);
  69.     rgb_label = repmat(pixel_labels,[1 1 3]);
  70.  
  71.     for k = 1:3
  72.          color = rgb;
  73.          color(rgb_label ~= k) = 0;
  74.          segmented_images{k} = color;
  75.     end
  76.  
  77.     %figure, imshow(segmented_images{1}), title('objects in cluster 1')
  78.     %figure, imshow(segmented_images{2}), title('objects in cluster 2')
  79.     figure, imshow(segmented_images{3}), title('objects in cluster 3')  % Show only grey cluster (road cluster)
  80.    
  81.     %Normaliztion for clustering road area.    
  82.     normImage_c3 = mat2gray(segmented_images{3});
  83.     normImage_c3_gray =rgb2gray(segmented_images{3});
  84.     %figure, imhist(normImage_c3_gray);
  85.  
  86.     % to Binary image
  87.     gray_convert_image = rgb2gray(normImage_c3);
  88.     binary_convert_image = im2bw(gray_convert_image);
  89.     figure, imshow(binary_convert_image)
  90.     bwarea(binary_convert_image);
  91.    
  92.     %bw = imread('text.tif');
  93.     %se = strel('line',20,150);
  94.     se = strel('line',10,10);
  95.     bw2 = imdilate(binary_convert_image,se);% graythresh = 'bw_img' % kmeans = 'binary_convert_image'
  96.     %imshow(bw), title('Original')
  97.     figure, imshow(bw2), title('Dilated')
  98.     bw3 = ~bw2;
  99.     bw4 = im2bw(bw3);
  100.     %figure, imshow(bw4);
  101.     CC = bwconncomp(bw2); %Connected Component Finding all area.
  102.     %CC = bwconncomp(my_image);
  103.     L = labelmatrix(CC);
  104.     A = cell( size(CC.PixelIdxList,1) , size(CC.PixelIdxList,2) );
  105.     A = CC.PixelIdxList;
  106.     size(CC.PixelIdxList,2);
  107.  
  108.     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
  109.    
  110.  
  111.     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  112.  
  113.     La=bwlabel(bw2,8); %%% labeledImage is a binary image
  114.     figure,imshow(La,[]);
  115.     coloredLabel = label2rgb(La, 'hsv', 'k', 'shuffle');
  116.     imshow(coloredLabel);
  117.    
  118.   %  pr = regionprops( La, 'Area', 'PixelIdxList' );
  119.    
  120.     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
  121.    
  122.     stats = regionprops(bw2,'Area')
  123.     stats2 = regionprops(bw2,'Centroid')
  124.  
  125.  
  126.     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  127.     smallAreaA = La;
  128.     smallAreaSh = La;
  129.     smallArea = La;
  130.     smallArea2 = La;
  131.     smallArea3 = La;
  132.     smallArea4 = La;
  133.     smallArea5 = La;
  134.     s = regionprops(La, 'Orientation','Area','PixelIdxList','Perimeter', 'BoundingBox',...
  135.         'MajorAxisLength', 'MinorAxisLength', 'Eccentricity', 'Centroid');
  136.  
  137.     %%%%%%%%%%%%%%%%%%%%%%%  Regionprop to matrix %%%%%%%%%%%%%%%%%%%%%%%%%
  138.     num_boject = 1:1:size(s);
  139.     s_centroid = vertcat(s.Centroid);
  140.     cen1 = s_centroid(:,1);
  141.     cen2 = s_centroid(:,2);
  142.     region_matrix = [s.Area; s.Perimeter; num_boject;]';
  143.    
  144.     s_area = region_matrix(:,1);
  145.     s_perimeter = region_matrix(:,2);
  146.     s_num = region_matrix(:,3);
  147.     min_perimeter = (sqrt(region_matrix(:,1)))*4;
  148.     shape_index = s_perimeter./min_perimeter; % formula of shape index
  149.     % region_matrix_used will contain 1. number of objects 2. perimeter and
  150.     % min_perimeter and calculating to shape_index
  151.     region_matrix_used = [s_num s_area s_perimeter min_perimeter shape_index];
  152.     threshold_use = max(s_area)/2;
  153.    
  154.     score = shape_index;
  155.     score2 = s_area;
  156.     num2 = 0;
  157.     area2 = 0;
  158.     shape2 = 0;
  159.     cent1 = 0;
  160.     cent2 = 0;
  161.     j = 1;
  162.     k = 1;
  163.    
  164.     % Loop 1 for "region_state1"
  165.     for i=1:size(s)
  166.         if s_area(i) > threshold_use & shape_index(i) > shape_value;
  167.            q1 = s_num(i);
  168.            q2 = s_area(i);
  169.            q3 = shape_index(i);
  170.            q4 = cen1(i);
  171.            q5 = cen2(i);
  172.            num(j) = q1;
  173.            area(j) = q2;
  174.            shape(j) = q3;
  175.            centA(j) = q4;
  176.            centB(j) = q5;
  177.            j = j+1;
  178.         end
  179.     end
  180.    
  181.      % Loop 2 for "region_state2"
  182.     for i=1:size(s)
  183.         if s_area(i) < threshold_use & shape_index(i) > shape_value;
  184.            q1 = s_num(i);
  185.            q2 = s_area(i);
  186.            q3 = shape_index(i);
  187.            q4 = cen1(i);
  188.            q5 = cen2(i);
  189.            num2(k) = q1;
  190.            area2(k) = q2;
  191.            shape2(k) = q3;
  192.            cent1(k) = q4;
  193.            cent2(k) = q5;
  194.            k = k+1;
  195.         end
  196.     end
  197.     region_state1 = [num' area' shape' centA' centB']; % Find Main Road
  198.     region_state2 = [num2' area2' shape2' cent1' cent2']; % Find Local Road
  199.    
  200.     temp1 = 1;
  201.     all_object = max(size(region_state2(region_state2(:,3)>0)));
  202.          area = region_state1(:,2);
  203.          cen_road1 = region_state1(:,4);
  204.          cen_road2 = region_state1(:,5);
  205.          cen_check1 = region_state2(:,4);
  206.          cen_check2 = region_state2(:,5);
  207.  
  208.   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
  209.   %%%%%%%%%%%%%%%%%%%%%%% Formula of Isolation Index %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  210.   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
  211.     isolation_form  = 0;
  212.     num_main_road = size(num',1)
  213.     num_local_road = size(num2',1)
  214.        for local=1:num_local_road   %% i = number of checking for local road.
  215.            for main=1:num_main_road
  216.                  distance = [cen_road1(main),cen_road2(main);cen_check1(local),cen_check2(local)];
  217.                  dis = pdist(distance,'euclidean');
  218.                  isolation_form = isolation_form +area(main)/(dis^2);
  219.            end        
  220.            similarity(local) = isolation_form / num_main_road ;
  221.            isolation_form = 0;
  222.        end
  223.   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
  224.   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  225.   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
  226.  
  227.     region_state3 = [num2' area2' shape2' similarity']; % Calculating to be a similarity index
  228.    
  229.     iso_num = region_state3(:,1);
  230.     iso_iso = region_state3(:,4);
  231.     indexIso=1;
  232.    
  233.     % This loop was created for keep similarity index to matrix.
  234.     for i=1:size(s_num)
  235.        Test(i) = 0;
  236.        for j=1:num_local_road  % take isolation following numners of it.
  237.            if i == iso_num(j)
  238.                Test(i) = iso_iso(j);
  239.            end
  240.        end
  241.        
  242.        
  243.     end
  244.    
  245.     region_state4 = [s_num Test']; % Created for checking above loop.
  246.     isolate = region_state4(:,2);
  247.        
  248.  
  249.     threshold_max = max(region_matrix_used(:,2))/2
  250.  
  251.     small_select = ((s_area >= threshold_max) & (shape_index > shape_value));
  252.     smallArea( vertcat( s(~small_select).PixelIdxList ) ) = 0; %// set all other regions to zero
  253.  
  254.     % This is Main Road.
  255.     MainRoad = (s_area >= threshold_max) & (shape_index > shape_value);
  256.  
  257.     small_select3 = ((shape_index > shape_value)); %all
  258.     smallArea3( vertcat( s(~small_select3).PixelIdxList ) ) = 0;
  259.  
  260.     small_select4 = (isolate > isolation_value_based);
  261.     smallArea4( vertcat( s(~small_select4).PixelIdxList ) ) = 0;
  262.  
  263.     % This is Local Road.
  264.     LocalRoad = (isolate > isolation_value);
  265.  
  266.     % This is Results.
  267.     small_select5 = (MainRoad + LocalRoad);
  268.     smallArea5( vertcat( s(~small_select5).PixelIdxList ) ) = 0;
  269.  
  270.  
  271.     figure, imshow( smallArea ); colormap( summer ); % Main_Road Result
  272.     figure, imshow( smallArea3 ); %all
  273.     figure, imshow( smallArea4 ); colormap( summer ); % Local_Road Result
  274.     figure, imshow( smallArea5 ); % Correct_Road Result
  275.  
  276.     figure, imshow(nm), hold on, himage = imshow(smallArea5), set(himage, 'AlphaData', 0.6);
  277.  
  278.  
  279.     %%%%%%%%%%%%%%%%%%%%%% Thining the image %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
  280.     % Thining the image
  281.     skeletionization_image = bwmorph(smallArea5,'thin',Inf);
  282.     se2 = strel('line',2,2); % 'line',5,8  'best',2,2
  283.     bw2 = imdilate(skeletionization_image,se2);
  284.     coloredLabel1 = label2rgb(bw2, 'hsv', 'k', 'shuffle');
  285.     % figure, imshow(coloredLabel1);
  286.     % ps     = dpsimplify(skeletionization_image,1);
  287.    
  288.     % SHOW ---------------- Thining the image ---------------------
  289.    
  290.     figure, imshow( smallArea5 ); colormap( summer ), hold on
  291.     himage = imshow(coloredLabel1);
  292.     set(himage, 'AlphaData', 0.7);
  293.    
  294. %     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
  295. %    
  296. %
  297. %     %%%%%%%%%%%%%%% Skeleton the image (Not using in this paper) %%%%%%%
  298. %    
  299. %     % Skeleton the image
  300. %     skeletionization_image = bwmorph(smallArea,'skel',Inf);
  301. %     se2 = strel('line',2,2); % 'line',5,8  'best',2,2
  302. %     bw2 = imdilate(skeletionization_image,se2);
  303. %     coloredLabel2 = label2rgb(bw2, 'hsv', 'k', 'shuffle');
  304. %     % figure, imshow(coloredLabel1);
  305. %     % ps     = dpsimplify(skeletionization_image,1);
  306. %
  307. %     % SHOW ---------------- Skeleton the image ---------------------
  308. %     figure, imshow(nm), hold on
  309. %     himage = imshow(coloredLabel2);
  310. %     set(himage, 'AlphaData', 0.7);
  311. %    
  312.     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
  313.    
  314.  
  315.     %%%%%%%%%%%%%%%%%%%%%% Simplipfy with DP %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  316.    
  317. %  - - - - - - - - - - - - - Find brach I - - - - - - - - - - - - - - - -
  318.     skelImg   = bwmorph(smallArea5, 'thin', 'inf');
  319.     branchImg = bwmorph(skelImg, 'branchpoints');
  320.     endImg    = bwmorph(skelImg, 'endpoints');
  321.  
  322.     [row, column] = find(endImg);
  323.     endPts        = [row column];
  324.     [row, column] = find(branchImg);
  325.     branchPts     = [row column];
  326.    
  327.     figure; imshow(skelImg); hold on;
  328.     plot(branchPts(:,2),branchPts(:,1),'r*'); hold on;
  329.     plot(endPts(:,2),endPts(:,1),'*');
  330.    
  331.   % - - - - Simplipfy with DP - - Find branch II - deleting some node - - %
  332.     skel2= bwmorph(smallArea5,'skel',Inf);
  333.  
  334.     B = bwmorph(skel2, 'branchpoints');
  335.     E = bwmorph(skel2, 'endpoints');
  336.  
  337.     [y,x] = find(E);
  338.     B_loc = find(B);
  339.  
  340.     Dmask = false(size(skel2));
  341.     for k = 1:numel(x)
  342.         D = bwdistgeodesic(skel2,x(k),y(k));
  343.         distanceToBranchPt = min(D(B_loc));
  344.         Dmask(D < distanceToBranchPt) =true;
  345.     end
  346.     skelD = skel2 - Dmask;
  347.     coloredLabel2 = label2rgb(skelD, 'hsv', 'k', 'shuffle');
  348.   %  skelC     = dpsimplify(skelD, 0.5); %Simplipfy with DP
  349.     figure, imshow(skelD);
  350.     hold all;
  351.     [y,x] = find(B); plot(x,y,'ro')
  352.    
  353.    % figure, imshow(nm), hold on
  354.     figure, imshow( smallArea5 ); colormap( summer ), hold on % my output
  355.     himage = imshow(coloredLabel2);
  356.     set(himage, 'AlphaData', 0.7);
  357.    
  358.    
  359.     %     figure, imshow( smallArea ), hold on % my output
  360.     %     himage = imshow(coloredLabel2);
  361.     %     set(himage, 'AlphaData', 0.7);
  362.  
  363.     %  - - - - - - - -  Simplipfy with DP (Uisng function dpsimplify) - - - - - - - - - -
  364.     %  
  365. %     B2 = num2cell(B);
  366. %     tol    = 0.5;
  367. %     ps     = dpsimplify(B2,tol);
  368. %     hold on
  369. %     figure, plot(ps(:,1),ps(:,2),'r','LineWidth',2);
  370. %     legend('original polyline','simplified')
  371.  
  372.  
  373.     %%%%%%%% Pseudocode (refer to
  374.     %%%%%%%% https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm)
  375.     %%%%%%%%
  376.     % function DouglasPeucker(PointList[], epsilon)
  377.     %     // Find the point with the maximum distance
  378.     %     dmax = 0
  379.     %     index = 0
  380.     %     end = length(PointList)
  381.     %     for i = 2 to ( end - 1) {
  382.     %         d = perpendicularDistance(PointList[i], Line(PointList[1], PointList[end]))
  383.     %         if ( d > dmax ) {
  384.     %             index = i
  385.     %             dmax = d
  386.     %         }
  387.     %     }
  388.     %     // If max distance is greater than epsilon, recursively simplify
  389.     %     if ( dmax > epsilon ) {
  390.     %         // Recursive call
  391.     %         recResults1[] = DouglasPeucker(PointList[1...index], epsilon)
  392.     %         recResults2[] = DouglasPeucker(PointList[index...end], epsilon)
  393.     %  
  394.     %         // Build the result list
  395.     %         ResultList[] = {recResults1[1...length(recResults1)-1], recResults2[1...length(recResults2)]}
  396.     %     } else {
  397.     %         ResultList[] = {PointList[1], PointList[end]}
  398.     %     }
  399.     %     // Return the result
  400.     %     return ResultList[]
  401.     % end
  402.  
  403.  
  404.  end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement