Advertisement
pongfactory

My Project CU 2016 [2-May-2016]

May 1st, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 13.98 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;]';
  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.     j = 1;
  155.     k = 1;
  156.     for i=1:size(s)
  157.         if s_area(i) > threshold_use & shape_index(i) > 2.5;
  158.            q1 = s_num(i);
  159.            q2 = s_area(i);
  160.            q3 = shape_index(i);
  161.            num(j) = q1;
  162.            area(j) = q2;
  163.            shape(j) = q3;
  164.            figure, imshow(bw4);
  165.            text(s(i).Centroid(1),s(i).Centroid(2),num2str(score(i)),'FontSize',15,'color','red');
  166.            j = j+1;
  167.            %region_state1 = [num' area' shape'];
  168.         end
  169.     end
  170.     for i=1:size(s)
  171.         if shape_index(i) > 2.5;
  172.            q1 = s_num(i);
  173.            q2 = s_area(i);
  174.            q3 = shape_index(i);
  175.            q4 = cen1(i);
  176.            q5 = cen2(i);
  177.            num2(k) = q1;
  178.            area2(k) = q2;
  179.            shape2(k) = q3;
  180.            cent1(k) = q4;
  181.            cent2(k) = q5;
  182. %            figure, imshow(bw4);
  183. %            text(s(i).Centroid(1),s(i).Centroid(2),num2str(score(i)),'FontSize',15,'color','red');
  184.            k = k+1;
  185.          
  186.         end
  187.     end
  188.     region_state1 = [num' area' shape'];
  189.     region_state2 = [num2' area2' shape2' cent1' cent2'];
  190.     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  191.    
  192.     %%%%%%%%%%%%%%%%%%%%%%%%%% Show value on objects %%%%%%%%%%%%%%%%%%%%%%
  193.     % score = (min(sqrt([s.Area]),[s.Perimeter]/4)./(max(sqrt([s.Area]),[s.Perimeter]/4))).^2;    
  194.     % score = shape_index;
  195. %     figure, imshow(bw4);
  196. %     for cnt = 1:length(s)
  197. %         text(s(cnt).Centroid(1),s(cnt).Centroid(2),num2str(score(cnt)),'FontSize',15,'color','red');
  198. %     end
  199.     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  200.  
  201. threshold_max = max(region_matrix_used(:,2))/2
  202. %shape_index = 2.5;
  203.  
  204. small_select = ((s_area >= threshold_max) & (shape_index > 2.5));
  205. smallArea( vertcat( s(~small_select).PixelIdxList ) ) = 0; %// set all other regions to zero
  206.  
  207.  
  208. small_select2 = ((s_area < threshold_max) & (shape_index > 2.49));
  209. smallArea2( vertcat( s(~small_select2).PixelIdxList ) ) = 0; %// set all other regions to zero
  210.  
  211. small_select3 = ((shape_index > 2.5));
  212. smallArea3( vertcat( s(~small_select3).PixelIdxList ) ) = 0; %// set all other regions to zero
  213.  
  214. figure, imshow( smallArea ); colormap( summer );
  215. figure, imshow( smallArea2 ); colormap( summer );
  216. figure, imshow( smallArea3 ); colormap( summer );
  217.  
  218.  
  219. %     if (s_area >= threshold_max) & (shape_index > 2.5)
  220. %         smallArea( vertcat( s(~small_select).PixelIdxList ) ) = 0;
  221. %         coloredLabelA = label2rgb(smallArea, 'hsv', 'g');    
  222. %     elseif (s_area < threshold_max) & (shape_index > 2.5)
  223. %         smallArea( vertcat( s(~small_select).PixelIdxList ) ) = 0;
  224. %         coloredLabelA = label2rgb(smallArea, 'hsv', 'b');
  225. %     end
  226. % figure, imshow( coloredLabelA );
  227.  
  228.    
  229. figure, imshow(nm), hold on, himage = imshow(smallArea3), set(himage, 'AlphaData', 0.6);
  230.  
  231.  
  232. %%% Comment %%%
  233.    
  234.     % Thining the image
  235.     skeletionization_image = bwmorph(smallArea,'thin',Inf);
  236.     se2 = strel('line',2,2); % 'line',5,8  'best',2,2
  237.     bw2 = imdilate(skeletionization_image,se2);
  238.     coloredLabel1 = label2rgb(bw2, 'hsv', 'k', 'shuffle');
  239.     % figure, imshow(coloredLabel1);
  240.     % ps     = dpsimplify(skeletionization_image,1);
  241.    
  242.     % SHOW ---------------- Thining the image ---------------------
  243.    
  244. %     figure, imshow( smallArea ); colormap( summer ), hold on
  245. %     himage = imshow(coloredLabel1);
  246. %     set(himage, 'AlphaData', 0.7);
  247. %    
  248. %     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
  249. %    
  250. %
  251. %     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  252. %    
  253. %     % Skeleton the image
  254. %     skeletionization_image = bwmorph(smallArea,'skel',Inf);
  255. %     se2 = strel('line',2,2); % 'line',5,8  'best',2,2
  256. %     bw2 = imdilate(skeletionization_image,se2);
  257. %     coloredLabel2 = label2rgb(bw2, 'hsv', 'k', 'shuffle');
  258. %     % figure, imshow(coloredLabel1);
  259. %     % ps     = dpsimplify(skeletionization_image,1);
  260. %
  261. %     % SHOW ---------------- Skeleton the image ---------------------
  262. %     figure, imshow(nm), hold on
  263. %     himage = imshow(coloredLabel2);
  264. %     set(himage, 'AlphaData', 0.7);
  265. %    
  266.     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
  267.    
  268.  
  269.     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  270.    
  271. %  - - - - - - - - - - - - - Find brach I - - - - - - - - - - - - - - - -
  272.     skelImg   = bwmorph(smallArea, 'thin', 'inf');
  273.     branchImg = bwmorph(skelImg, 'branchpoints');
  274.     endImg    = bwmorph(skelImg, 'endpoints');
  275.  
  276.     [row, column] = find(endImg);
  277.     endPts        = [row column];
  278.     [row, column] = find(branchImg);
  279.     branchPts     = [row column];
  280.    
  281.     figure; imshow(skelImg); hold on;
  282.     plot(branchPts(:,2),branchPts(:,1),'r*'); hold on;
  283.     plot(endPts(:,2),endPts(:,1),'*');
  284.    
  285.   % - - - - - - - - - - - - Find branch II - deleting some node - - - - - -
  286.     skel2= bwmorph(smallArea,'skel',Inf);
  287.  
  288.     B = bwmorph(skel2, 'branchpoints');
  289.     E = bwmorph(skel2, 'endpoints');
  290.  
  291.     [y,x] = find(E);
  292.     B_loc = find(B);
  293.    
  294.    
  295.     Dmask = false(size(skel2));
  296.     for k = 1:numel(x)
  297.         D = bwdistgeodesic(skel2,x(k),y(k));
  298.         distanceToBranchPt = min(D(B_loc));
  299.         Dmask(D < distanceToBranchPt) =true;
  300.     end
  301.     skelD = skel2 - Dmask;
  302.     coloredLabel2 = label2rgb(skelD, 'hsv', 'k', 'shuffle');
  303.     figure, imshow(skelD);
  304.     hold all;
  305.     [y,x] = find(B); plot(x,y,'ro')
  306.    
  307.     %figure, imshow(nm), hold on
  308.     figure, imshow( smallArea ); colormap( summer ), hold on % my output
  309.     himage = imshow(coloredLabel2);
  310.     set(himage, 'AlphaData', 0.7);
  311.    
  312.    
  313. %     figure, imshow( smallArea ), hold on % my output
  314. %     himage = imshow(coloredLabel2);
  315. %     set(himage, 'AlphaData', 0.7);
  316.     % - - - - - - - - - - - - Find HOUGH lines - - - - - - - - - - - - - -
  317. %    % rotI = imrotate(smallArea,1,'crop');
  318. %     BW = edge(smallArea,'canny');
  319. %     [H,T,R] = hough(BW);
  320. %     figure, imshow(~BW);
  321. % %     figure, imshow(H,[],'XData',T,'YData',R,...
  322. % %                 'InitialMagnification','fit');
  323. %     xlabel('\theta'), ylabel('\rho');
  324. %     axis on, axis normal, hold on;
  325. %     P  = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
  326. %     x = T(P(:,2)); y = R(P(:,1));
  327. %     plot(x,y,'s','color','white');
  328. %     % Find lines and plot them
  329. %     lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
  330. %     figure, imshow(smallArea), hold on
  331. %     max_len = 0;
  332. %     for k = 1:length(lines)
  333. %        xy = [lines(k).point1; lines(k).point2];
  334. %        plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
  335. %
  336. %        % Plot beginnings and ends of lines
  337. %        plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
  338. %        plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
  339. %
  340. %        % Determine the endpoints of the longest line segment
  341. %        len = norm(lines(k).point1 - lines(k).point2);
  342. %        if ( len > max_len)
  343. %           max_len = len;
  344. %           xy_long = xy;
  345. %        end
  346. %     end
  347. %
  348. %     %  - - - - - - - -  Simplipfy with DP  - - - - - - - - - - - - - - - -
  349. % %     B2 = num2cell(B);
  350. % %     tol    = 0.5;
  351. % %     ps     = dpsimplify(B2,tol);
  352. % %     hold on
  353. % %     figure, plot(ps(:,1),ps(:,2),'r','LineWidth',2);
  354. % %     legend('original polyline','simplified')
  355.  
  356.  
  357.  
  358.  
  359.  
  360. % Comment %%
  361. end
  362.  
  363.  
  364. % % --------------- Plot R-G-B Histrogram -----------------------------
  365. % % Read in standard MATLAB color demo image.
  366. % rgbImage = imread('test_demo.png');
  367. % workspace; % Make sure the workspace panel is showing.
  368. % fontSize = 20;
  369. % [rows columns numberOfColorBands] = size(rgbImage);
  370. % subplot(2, 2, 1);
  371. % imshow(rgbImage, []);
  372. % title('Original Color Image', 'Fontsize', fontSize);
  373. % set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
  374. %
  375. % redPlane = rgbImage(:, :, 1);
  376. % greenPlane = rgbImage(:, :, 2);
  377. % bluePlane = rgbImage(:, :, 3);
  378. %
  379. % % Let's get its histograms.
  380. % [pixelCountR grayLevelsR] = imhist(redPlane);
  381. % subplot(2, 2, 2);
  382. % plot(pixelCountR, 'r');
  383. % title('Histogram of red plane', 'Fontsize', fontSize);
  384. % xlim([0 grayLevelsR(end)]); % Scale x axis manually.
  385. %
  386. % [pixelCountG grayLevelsG] = imhist(greenPlane);
  387. % subplot(2, 2, 3);
  388. % plot(pixelCountG, 'g');
  389. % title('Histogram of green plane', 'Fontsize', fontSize);
  390. % xlim([0 grayLevelsG(end)]); % Scale x axis manually.
  391. %
  392. % [pixelCountB grayLevelsB] = imhist(bluePlane);
  393. % subplot(2, 2, 4);
  394. % plot(pixelCountB, 'b');
  395. % title('Histogram of blue plane', 'Fontsize', fontSize);
  396. % xlim([0 grayLevelsB(end)]); % Scale x axis manually.
  397. % %------------------ End to plot R-G-B Histrogram ------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement