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 = {'test_demo.png'}; % theshold = 23000
- nlist2 = {'test_paper_1.png'}; % theshold =
- nlist3 = {'test_per_1.png'}; % theshold =
- nlist4 = {'testroad3.png'}; % theshold = 7500
- nlist5 = {'test_demo1.png'}; % theshold = 29000
- nlist6 = {'test_paper_3.png'};
- 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 = nlist5{cta};
- rgb = imread(nm);
- normImage = im2double(rgb);
- nm_input = rgb2gray(normImage);
- %figure, imshow(rgb);
- % 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')
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- smallArea = La;
- smallArea2 = La;
- smallArea3 = 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);
- region_matrix = [s.Area; s.Perimeter; num_boject; s.Centroid;]';
- 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)];
- cen1 = s_centroid(:,1);
- cen2 = s_centroid(:,2);
- threshold_use = max(s_area)/2;
- s_area > threshold_use & shape_index > 2.5;
- score = shape_index;
- score2 = s_area;
- 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);
- num(j) = q1;
- area(j) = q2;
- shape(j) = q3;
- 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'];
- end
- end
- for i=1:size(s)
- if 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;
- end
- end
- region_state1 = [num' area' shape'];
- region_state2 = [num2' area2' shape2' cent1' cent2'];
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%% 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_max = max(region_matrix_used(:,2))/2
- %shape_index = 2.5;
- small_select = ((s_area >= threshold_max) & (shape_index > 2.5));
- smallArea( vertcat( s(~small_select).PixelIdxList ) ) = 0; %// set all other regions to zero
- 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));
- smallArea3( vertcat( s(~small_select3).PixelIdxList ) ) = 0; %// set all other regions to zero
- figure, imshow( smallArea ); colormap( summer );
- figure, imshow( smallArea2 ); colormap( summer );
- figure, imshow( smallArea3 ); colormap( summer );
- % 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(smallArea3), set(himage, 'AlphaData', 0.6);
- %%% Comment %%%
- % Thining the image
- skeletionization_image = bwmorph(smallArea,'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(smallArea, '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(smallArea,'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( smallArea ); 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
- % % --------------- Plot R-G-B Histrogram -----------------------------
- % % Read in standard MATLAB color demo image.
- % rgbImage = imread('test_demo.png');
- % workspace; % Make sure the workspace panel is showing.
- % fontSize = 20;
- % [rows columns numberOfColorBands] = size(rgbImage);
- % subplot(2, 2, 1);
- % imshow(rgbImage, []);
- % title('Original Color Image', 'Fontsize', fontSize);
- % set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
- %
- % redPlane = rgbImage(:, :, 1);
- % greenPlane = rgbImage(:, :, 2);
- % bluePlane = rgbImage(:, :, 3);
- %
- % % Let's get its histograms.
- % [pixelCountR grayLevelsR] = imhist(redPlane);
- % subplot(2, 2, 2);
- % plot(pixelCountR, 'r');
- % title('Histogram of red plane', 'Fontsize', fontSize);
- % xlim([0 grayLevelsR(end)]); % Scale x axis manually.
- %
- % [pixelCountG grayLevelsG] = imhist(greenPlane);
- % subplot(2, 2, 3);
- % plot(pixelCountG, 'g');
- % title('Histogram of green plane', 'Fontsize', fontSize);
- % xlim([0 grayLevelsG(end)]); % Scale x axis manually.
- %
- % [pixelCountB grayLevelsB] = imhist(bluePlane);
- % subplot(2, 2, 4);
- % plot(pixelCountB, 'b');
- % title('Histogram of blue plane', 'Fontsize', fontSize);
- % xlim([0 grayLevelsB(end)]); % Scale x axis manually.
- % %------------------ End to plot R-G-B Histrogram ------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement