Advertisement
pongfactory

CU Code 7-May-2016

Jun 6th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 14.00 KB | None | 0 0
  1. clear;
  2. clc;
  3. %nlist           = {'aerial6.jpg';'aerial10.jpg';'roadA.jpg';'aerial8.jpg'};
  4. nlist1 = {'test_demo.png'};   % theshold = 23000
  5. nlist2 = {'test_paper_1.png'}; % theshold =
  6. nlist3 = {'test_per_1.png'};   % theshold =
  7. nlist4 = {'testroad3.png'};   % theshold = 7500
  8. nlist5 = {'test_demo1.png'};  % theshold = 29000
  9. nlist6 = {'test_paper_3.png'};
  10.  
  11. threshold_fix = 23000;
  12.  
  13. % test kmeans
  14. X = [randn(20,2)+ones(20,2); randn(20,2)-ones(20,2)];
  15.       opts = statset('Display','final');
  16.       [cidx, ctrs] = kmeans(X, 4, 'Distance','city', ...
  17.                             'Replicates',5, 'Options',opts);
  18.  
  19. for cta = 1:1 % all in my img folder is 7
  20.     nm = nlist5{cta};
  21.     rgb = imread(nm);
  22.     normImage = im2double(rgb);
  23.     nm_input = rgb2gray(normImage);
  24.    
  25.     %figure, imshow(rgb);
  26.    
  27. %     a=imread('test1.jpg');
  28. %     b=imresize(a,[500 800]);
  29.    
  30. % Step 1: Read Image
  31.     img_resize = imresize(rgb, 1);
  32.     he = img_resize;
  33.     he2 = img_resize;
  34.  %   figure, imshow(he), title('1'); % Showing a original image.
  35.    
  36.     img_graythresh = rgb2gray(he );
  37.     threshold = graythresh(img_graythresh);
  38.     bw_img = im2bw(he,threshold);
  39.   %  figure, imshow(bw_img), title('2');
  40.   %  figure, imhist(img_graythresh), title('3');
  41.  
  42. % Step 2: Convert Image from RGB Color Space to L*a*b* Color Space
  43.     cform = makecform('srgb2lab');
  44.     cform_rgb = makecform('lab2srgb');
  45.     lab_he = applycform(he,cform);
  46.     rgb_he = applycform(he,cform_rgb);
  47.    
  48. % Step 3: Classify the Colors in 'a*b*' Space Using K-Means Clustering
  49.     ab = double(rgb_he(:,:,2:3));
  50.     ab_all_band = double(rgb_he(:,:,2:3));
  51.     nrows = size(ab,1);
  52.     ncols = size(ab,2);
  53.     ab = reshape(ab,nrows*ncols,2);
  54.      
  55.     nColors = 3;
  56.     [cluster_idx, cluster_center] = kmeans(ab,nColors, ...
  57.                                     'Replicates',1, ...
  58.                                     'Options',opts, ...
  59.                                     'start',[118 143; 127 140; 133 127]);
  60.                                 % 'start',[118 143; 127 140; 133 127]);
  61.                          
  62. % Step 4: Label Every Pixel in the Image Using the Results from KMEANS
  63.     pixel_labels = reshape(cluster_idx,nrows,ncols);
  64.     %figure(2), imshow(pixel_labels,[]), title('image labeled by cluster index')
  65.    
  66. % Step 5: Create Images that Segment the H&E Image by Color.
  67.     segmented_images = cell(1,3);
  68.     rgb_label = repmat(pixel_labels,[1 1 3]);
  69.  
  70.     for k = 1:3
  71.          color = rgb;
  72.          color(rgb_label ~= k) = 0;
  73.          segmented_images{k} = color;
  74.     end
  75.  
  76.     %figure, imshow(segmented_images{1}), title('objects in cluster 1')
  77.     %figure, imshow(segmented_images{2}), title('objects in cluster 2')
  78.     figure, imshow(segmented_images{3}), title('objects in cluster 3')
  79.    
  80.     %Normaliztion for clustering road area.    
  81.     normImage_c3 = mat2gray(segmented_images{3});
  82.     normImage_c3_gray =rgb2gray(segmented_images{3});
  83.     %figure, imhist(normImage_c3_gray);
  84.  
  85.     % to Binary image
  86.     gray_convert_image = rgb2gray(normImage_c3);
  87.     binary_convert_image = im2bw(gray_convert_image);
  88.     figure, imshow(binary_convert_image)
  89.     bwarea(binary_convert_image)
  90.    
  91.     %bw = imread('text.tif');
  92.     %se = strel('line',20,150);
  93.     se = strel('line',10,10);
  94.     bw2 = imdilate(binary_convert_image,se);% graythresh = 'bw_img' % kmeans = 'binary_convert_image'
  95.     %imshow(bw), title('Original')
  96.     figure, imshow(bw2), title('Dilated')
  97.     bw3 = ~bw2;
  98.     bw4 = im2bw(bw3);
  99.     %figure, imshow(bw4);
  100.     CC = bwconncomp(bw2); %Connected Component Finding all area.
  101.     %CC = bwconncomp(my_image);
  102.     L = labelmatrix(CC);
  103.     A = cell( size(CC.PixelIdxList,1) , size(CC.PixelIdxList,2) );
  104.     A = CC.PixelIdxList;
  105.     size(CC.PixelIdxList,2);
  106.  
  107.     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
  108.    
  109.  
  110.     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  111.  
  112.     La=bwlabel(bw2,8); %%% labeledImage is a binary image
  113.     figure,imshow(La,[]);
  114.     coloredLabel = label2rgb(La, 'hsv', 'k', 'shuffle');
  115.     imshow(coloredLabel);
  116.    
  117.   %  pr = regionprops( La, 'Area', 'PixelIdxList' );
  118.    
  119.     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
  120.    
  121.     stats = regionprops(bw2,'Area')
  122.     stats2 = regionprops(bw2,'Centroid')
  123.  
  124.  
  125.     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  126.  
  127.     smallArea = La;
  128.     smallArea2 = La;
  129.     smallArea3 = La;
  130.     s = regionprops(La, 'Orientation','Area','PixelIdxList','Perimeter', 'BoundingBox',...
  131.         'MajorAxisLength', 'MinorAxisLength', 'Eccentricity', 'Centroid');
  132.  
  133.     %%%%%%%%%%%%%%%%%%%%%%%  regionprop to matrix %%%%%%%%%%%%%%%%%%%%%%%%%
  134. %     numel(s.Area)
  135. %     for i=1:numel(s)
  136. %         s(i).Centroid(1)
  137. %         s(i).Centroid(2)
  138. %     end
  139.     num_boject = 1:1:size(s);
  140.     s_centroid = vertcat(s.Centroid);
  141.     region_matrix = [s.Area; s.Perimeter; num_boject; s.Centroid;]';
  142.     s_area = region_matrix(:,1);
  143.     s_perimeter = region_matrix(:,2);
  144.     s_num = region_matrix(:,3);
  145.     min_perimeter = (sqrt(region_matrix(:,1)))*4;
  146.     shape_index = s_perimeter./min_perimeter;
  147.     region_matrix_used = [s_num s_area s_perimeter min_perimeter shape_index s_centroid(:,1) s_centroid(:,2)];
  148.     cen1 = s_centroid(:,1);
  149.     cen2 = s_centroid(:,2);
  150.     threshold_use = max(s_area)/2;
  151.     s_area > threshold_use & shape_index > 2.5;
  152.     score = shape_index;
  153.     score2 = s_area;
  154.    
  155.     j = 1;
  156.     k = 1;
  157.     for i=1:size(s)
  158.         if s_area(i) > threshold_use & shape_index(i) > 2.5;
  159.            q1 = s_num(i);
  160.            q2 = s_area(i);
  161.            q3 = shape_index(i);
  162.            num(j) = q1;
  163.            area(j) = q2;
  164.            shape(j) = q3;
  165.            figure, imshow(bw4);
  166.            text(s(i).Centroid(1),s(i).Centroid(2),num2str(score(i)),'FontSize',15,'color','red');
  167.            j = j+1;
  168.            %region_state1 = [num' area' shape'];
  169.         end
  170.     end
  171.     for i=1:size(s)
  172.         if shape_index(i) > 2.5;
  173.            q1 = s_num(i);
  174.            q2 = s_area(i);
  175.            q3 = shape_index(i);
  176.            q4 = cen1(i);
  177.            q5 = cen2(i);
  178.            num2(k) = q1;
  179.            area2(k) = q2;
  180.            shape2(k) = q3;
  181.            cent1(k) = q4;
  182.            cent2(k) = q5;
  183. %            figure, imshow(bw4);
  184. %            text(s(i).Centroid(1),s(i).Centroid(2),num2str(score(i)),'FontSize',15,'color','red');
  185.            k = k+1;
  186.          
  187.         end
  188.     end
  189.     region_state1 = [num' area' shape'];
  190.     region_state2 = [num2' area2' shape2' cent1' cent2'];
  191.     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  192.    
  193.     %%%%%%%%%%%%%%%%%%%%%%%%%% Show value on objects %%%%%%%%%%%%%%%%%%%%%%
  194.     % score = (min(sqrt([s.Area]),[s.Perimeter]/4)./(max(sqrt([s.Area]),[s.Perimeter]/4))).^2;    
  195.     % score = shape_index;
  196. %     figure, imshow(bw4);
  197. %     for cnt = 1:length(s)
  198. %         text(s(cnt).Centroid(1),s(cnt).Centroid(2),num2str(score(cnt)),'FontSize',15,'color','red');
  199. %     end
  200.     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  201.  
  202. threshold_max = max(region_matrix_used(:,2))/2
  203. %shape_index = 2.5;
  204.  
  205. small_select = ((s_area >= threshold_max) & (shape_index > 2.5));
  206. smallArea( vertcat( s(~small_select).PixelIdxList ) ) = 0; %// set all other regions to zero
  207.  
  208.  
  209. small_select2 = ((s_area < threshold_max) & (shape_index > 2.49));
  210. smallArea2( vertcat( s(~small_select2).PixelIdxList ) ) = 0; %// set all other regions to zero
  211.  
  212. small_select3 = ((shape_index > 2.5));
  213. smallArea3( vertcat( s(~small_select3).PixelIdxList ) ) = 0; %// set all other regions to zero
  214.  
  215. figure, imshow( smallArea ); colormap( summer );
  216. figure, imshow( smallArea2 ); colormap( summer );
  217. figure, imshow( smallArea3 ); colormap( summer );
  218.  
  219.  
  220. %     if (s_area >= threshold_max) & (shape_index > 2.5)
  221. %         smallArea( vertcat( s(~small_select).PixelIdxList ) ) = 0;
  222. %         coloredLabelA = label2rgb(smallArea, 'hsv', 'g');    
  223. %     elseif (s_area < threshold_max) & (shape_index > 2.5)
  224. %         smallArea( vertcat( s(~small_select).PixelIdxList ) ) = 0;
  225. %         coloredLabelA = label2rgb(smallArea, 'hsv', 'b');
  226. %     end
  227. % figure, imshow( coloredLabelA );
  228.  
  229.    
  230. figure, imshow(nm), hold on, himage = imshow(smallArea3), set(himage, 'AlphaData', 0.6);
  231.  
  232.  
  233. %%% Comment %%%
  234.    
  235.     % Thining the image
  236.     skeletionization_image = bwmorph(smallArea,'thin',Inf);
  237.     se2 = strel('line',2,2); % 'line',5,8  'best',2,2
  238.     bw2 = imdilate(skeletionization_image,se2);
  239.     coloredLabel1 = label2rgb(bw2, 'hsv', 'k', 'shuffle');
  240.     % figure, imshow(coloredLabel1);
  241.     % ps     = dpsimplify(skeletionization_image,1);
  242.    
  243.     % SHOW ---------------- Thining the image ---------------------
  244.    
  245. %     figure, imshow( smallArea ); colormap( summer ), hold on
  246. %     himage = imshow(coloredLabel1);
  247. %     set(himage, 'AlphaData', 0.7);
  248. %    
  249. %     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
  250. %    
  251. %
  252. %     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  253. %    
  254. %     % Skeleton the image
  255. %     skeletionization_image = bwmorph(smallArea,'skel',Inf);
  256. %     se2 = strel('line',2,2); % 'line',5,8  'best',2,2
  257. %     bw2 = imdilate(skeletionization_image,se2);
  258. %     coloredLabel2 = label2rgb(bw2, 'hsv', 'k', 'shuffle');
  259. %     % figure, imshow(coloredLabel1);
  260. %     % ps     = dpsimplify(skeletionization_image,1);
  261. %
  262. %     % SHOW ---------------- Skeleton the image ---------------------
  263. %     figure, imshow(nm), hold on
  264. %     himage = imshow(coloredLabel2);
  265. %     set(himage, 'AlphaData', 0.7);
  266. %    
  267.     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
  268.    
  269.  
  270.     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  271.    
  272. %  - - - - - - - - - - - - - Find brach I - - - - - - - - - - - - - - - -
  273.     skelImg   = bwmorph(smallArea, 'thin', 'inf');
  274.     branchImg = bwmorph(skelImg, 'branchpoints');
  275.     endImg    = bwmorph(skelImg, 'endpoints');
  276.  
  277.     [row, column] = find(endImg);
  278.     endPts        = [row column];
  279.     [row, column] = find(branchImg);
  280.     branchPts     = [row column];
  281.    
  282.     figure; imshow(skelImg); hold on;
  283.     plot(branchPts(:,2),branchPts(:,1),'r*'); hold on;
  284.     plot(endPts(:,2),endPts(:,1),'*');
  285.    
  286.   % - - - - - - - - - - - - Find branch II - deleting some node - - - - - -
  287.     skel2= bwmorph(smallArea,'skel',Inf);
  288.  
  289.     B = bwmorph(skel2, 'branchpoints');
  290.     E = bwmorph(skel2, 'endpoints');
  291.  
  292.     [y,x] = find(E);
  293.     B_loc = find(B);
  294.    
  295.    
  296.     Dmask = false(size(skel2));
  297.     for k = 1:numel(x)
  298.         D = bwdistgeodesic(skel2,x(k),y(k));
  299.         distanceToBranchPt = min(D(B_loc));
  300.         Dmask(D < distanceToBranchPt) =true;
  301.     end
  302.     skelD = skel2 - Dmask;
  303.     coloredLabel2 = label2rgb(skelD, 'hsv', 'k', 'shuffle');
  304.     figure, imshow(skelD);
  305.     hold all;
  306.     [y,x] = find(B); plot(x,y,'ro')
  307.    
  308.     %figure, imshow(nm), hold on
  309.     figure, imshow( smallArea ); colormap( summer ), hold on % my output
  310.     himage = imshow(coloredLabel2);
  311.     set(himage, 'AlphaData', 0.7);
  312.    
  313.    
  314. %     figure, imshow( smallArea ), hold on % my output
  315. %     himage = imshow(coloredLabel2);
  316. %     set(himage, 'AlphaData', 0.7);
  317.     % - - - - - - - - - - - - Find HOUGH lines - - - - - - - - - - - - - -
  318. %    % rotI = imrotate(smallArea,1,'crop');
  319. %     BW = edge(smallArea,'canny');
  320. %     [H,T,R] = hough(BW);
  321. %     figure, imshow(~BW);
  322. % %     figure, imshow(H,[],'XData',T,'YData',R,...
  323. % %                 'InitialMagnification','fit');
  324. %     xlabel('\theta'), ylabel('\rho');
  325. %     axis on, axis normal, hold on;
  326. %     P  = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
  327. %     x = T(P(:,2)); y = R(P(:,1));
  328. %     plot(x,y,'s','color','white');
  329. %     % Find lines and plot them
  330. %     lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
  331. %     figure, imshow(smallArea), hold on
  332. %     max_len = 0;
  333. %     for k = 1:length(lines)
  334. %        xy = [lines(k).point1; lines(k).point2];
  335. %        plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
  336. %
  337. %        % Plot beginnings and ends of lines
  338. %        plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
  339. %        plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
  340. %
  341. %        % Determine the endpoints of the longest line segment
  342. %        len = norm(lines(k).point1 - lines(k).point2);
  343. %        if ( len > max_len)
  344. %           max_len = len;
  345. %           xy_long = xy;
  346. %        end
  347. %     end
  348. %
  349. %     %  - - - - - - - -  Simplipfy with DP  - - - - - - - - - - - - - - - -
  350. % %     B2 = num2cell(B);
  351. % %     tol    = 0.5;
  352. % %     ps     = dpsimplify(B2,tol);
  353. % %     hold on
  354. % %     figure, plot(ps(:,1),ps(:,2),'r','LineWidth',2);
  355. % %     legend('original polyline','simplified')
  356.  
  357.  
  358.  
  359.  
  360.  
  361. % Comment %%
  362. end
  363.  
  364.  
  365. % % --------------- Plot R-G-B Histrogram -----------------------------
  366. % % Read in standard MATLAB color demo image.
  367. % rgbImage = imread('test_demo.png');
  368. % workspace; % Make sure the workspace panel is showing.
  369. % fontSize = 20;
  370. % [rows columns numberOfColorBands] = size(rgbImage);
  371. % subplot(2, 2, 1);
  372. % imshow(rgbImage, []);
  373. % title('Original Color Image', 'Fontsize', fontSize);
  374. % set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
  375. %
  376. % redPlane = rgbImage(:, :, 1);
  377. % greenPlane = rgbImage(:, :, 2);
  378. % bluePlane = rgbImage(:, :, 3);
  379. %
  380. % % Let's get its histograms.
  381. % [pixelCountR grayLevelsR] = imhist(redPlane);
  382. % subplot(2, 2, 2);
  383. % plot(pixelCountR, 'r');
  384. % title('Histogram of red plane', 'Fontsize', fontSize);
  385. % xlim([0 grayLevelsR(end)]); % Scale x axis manually.
  386. %
  387. % [pixelCountG grayLevelsG] = imhist(greenPlane);
  388. % subplot(2, 2, 3);
  389. % plot(pixelCountG, 'g');
  390. % title('Histogram of green plane', 'Fontsize', fontSize);
  391. % xlim([0 grayLevelsG(end)]); % Scale x axis manually.
  392. %
  393. % [pixelCountB grayLevelsB] = imhist(bluePlane);
  394. % subplot(2, 2, 4);
  395. % plot(pixelCountB, 'b');
  396. % title('Histogram of blue plane', 'Fontsize', fontSize);
  397. % xlim([0 grayLevelsB(end)]); % Scale x axis manually.
  398. % %------------------ End to plot R-G-B Histrogram ------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement