How to create auto polyline in the image
조회 수: 4 (최근 30일)
이전 댓글 표시
Hello,
I am so new to MATLAB coding and for my original code I need to draw auto polyline for my figure. I have written a code but it is not following the original area, so i am wondering if any one can help me to fix it. I have attached the image below (gray image) and I want to learn the x and y coordinate of the area below red line but my code gives something else like in results of bwperim. Can someone help me to solve this? problem? I have also added the original image and my script.
Thank you
clear
close all
clc
fontSize = 16;
skip = 10;
%directory for matlab.
% Load Image
I = imread('Band Contrast 1-1.jpg');
size(I);
% Workaround to get back to a binary image
% I(:,[1:86 403:end],:) = 0;
% I([1:31 500:end],:,:) = 0;
level=graythresh(I)
%%
B = imbinarize(I,0.5);
figure()
imshowpair(I,B,'montage')
%% remove features.
clc
Bfilled=imfill(B,'holes');
Bcc=bwconncomp(Bfilled);
voids=regionprops(Bcc,'centroid','Area','Circularity','MajorAxisLength','MinorAxisLength','Eccentricity','Orientation','EquivDiameter','Extent','MaxFeretProperties','MinFeretProperties');
%
CCC=voids.Centroid;
CENTROID=zeros(size(voids,1),2);
icc=0;
for ii=1:2:size(CCC,2)
icc=icc+1;
CENTROID(icc,:)=CCC(ii,ii+1);
end
%%
clc
Bvoids=ismember(labelmatrix(Bcc),find([voids.Area]>1));
% condition=CENTROID(:,1)<=560 & CENTROID(:,2)<=40 & CENTROID(:,1)>=500 & CENTROID(:,2)>=0;
condition=[voids.Area]>1000 & [voids.Area]<2000;
BVOID=ismember(labelmatrix(Bcc),find(~condition));
sum(condition)
figure()
imshowpair(Bvoids,BVOID,'montage')
%%
% Show Initial Image
hf = figure('Units','Normalized','OuterPosition',[0 0 1 1]);
subplot(1,2,1), imshow(BVOID)
title('Initial Binary Image','fontSize',fontSize)
% This step is not required, but speeds up the use of boundary later.
Bperimeter = bwperim(BVOID,26);
% Bperimeter = imdilate(BVOID,offsetstrel('ball',100,90));
subplot(1,2,2), imshow(Bperimeter)
title('Result of bwperim','fontSize',fontSize)
% Get x,y coordinates of perimeter (column index and row index,
% respectively)
[y,x] = find(Bperimeter);
k = boundary(x,y,1); %use boundary with shrink factor of 1 to find vertices
% Back to the Initial Binary image, add the polyline using recently
% obtained vertices
idx = [k(1:skip:end);k(1)];
drawpolygon('Position',[x(idx) y(idx)])
%%
clc
figure()
plot(x(idx),-y(idx)+(max(y(idx))),'k.-','MarkerSize',20)
댓글 수: 0
채택된 답변
Image Analyst
2022년 10월 16일
Try this:
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 25;
% Create initial image.
grayImage = imread('Band Contrast 1-1.jpg');
grayImage = imnoise(grayImage, "gaussian", 0, .01);
[rows, columns, numberOfColorChannels] = size(grayImage)
imshow(grayImage, []);
title('Band Contrast 1-1.jpg')
impixelinfo
% Let user threshold the image.
% Interactively and visually set a threshold on a gray scale image.
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
lowThreshold = 93
% [lowThreshold, highThreshold, lastThresholdedBand] = threshold(lowThreshold, 255, grayImage)
% Threshold the image according to the user's choice.
binaryImage = grayImage >= lowThreshold;
% Take the largest blob only
binaryImage = bwareafilt(binaryImage, 1);
% Scan across finding top row
topRows = nan(1, columns);
for col = 1 : columns
t = find(binaryImage(:, col), 1, 'first');
if ~isempty(t)
topRows(col) = t;
end
end
% Plot over image.
hold on;
plot(topRows, 'r-', 'LineWidth', 4);
댓글 수: 9
Image Analyst
2022년 10월 17일
This tiff image still has a 2-pixel wide white frame around it. Plus it's 4 channels. I'm throwing away the 4th channel and erasing the white frame. For the bottom part use a threshold of 153. See attached m-file and adapt as needed.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Import, Export, and Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!