How to create auto polyline in the image

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)

 Accepted Answer

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 Comments

Hi,
Thank you so much for your time for writing a code. It is definetely better than mine :) Can I ask some question about the code? First of all, ı am happy that the red line follows the contour of the image but how Can I make it to follow all the contour? I mean I want to cover all the figure except top black area (somethink like below image)?I want to run another anlaysis so I need those bounding box coordinates. Also, can I ask why there is two sudden peak at the beginning and end of the image?
My last question is if there is way to extract the blakish area from the gray area?
Thank you
Look at your original image. The left two columns and the right two columns are very bright - like more than 240 - so they get picked up by the threshold. I don't know why they are bright - it is probably because you're using JPG as your image format and those are compression artifacts. You should use PNG format if at all possible or you will have compression artifacts. If you don't want the outer columns to be bright, then set the outer two columns to zero, or extract the image except for the outer two columns:
grayImage = grayImage(:, 3:end-2); % Extract all but the outer two columns.
To get the full boundary, use bwboundaries:
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);
% Get the boundary
boundary = bwboundaries(binaryImage, 8,"noholes")
xb = boundary{1}(:, 2);
yb = boundary{1}(:, 1);
% Plot over image.
hold on;
plot(xb, yb, 'r-', 'LineWidth', 4);
For the darker stuff in the top of the lower area, just do the same thing but find and apply the threshold:
binaryImage = (grayImage >= lowThreshold) & (grayImage <= highThreshold);
To visually and interactively set the thresholds, see my File Exchange utility:
Thank you so much again. In somehow I have found a way to save image data as tiff in the software that I am using to create those images. I have also tried your code for tiff image but I kept getting error in imshow (grayImage, []), when I saved as png as you suggested, then i got an error in binaryImage = bwareafilt(binaryImage, 1), which is because number of color channles is 3. With my knowledge, i could not solve it.
I will look at your file exchange utility for the darker stuff in the top of the lower area. What I excatly want is to draw bounding box of that area and then extract it so that I could have two different image at the end. Then I can work on those two seperate images. I have looked at extracting images from the original image in matlab but till now i could not find any useful info.
Also please forgive my silly questions since I am still so so new to matlab coding
Please attach the tif image with the paperclip icon.
I couldnt upload my image since it is giving unsupported file error, yet you can see and download hopefully.
Put it in a .zip file.
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.
Thank you so much. Very much appreciated.

Sign in to comment.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Products

Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!