Find the diameter of fruit image in matlab
7 ビュー (過去 30 日間)
古いコメントを表示
I want to find the diameter of an binary image. Here i my code, but it makes some error:
img= imread('apple.png');
im1=rgb2gray(img);
%level=graythresh(img);
%BW=im2bw(img,level);
level=graythresh(im1);
BW=im2bw(im1,level);
%obtain the edge of the image
e=edge(im1,'canny',0.52);
%get the size of the image
[m n]=size(e);
i1=0;
j1=0;
%i & j varies from 1 to size of the image
for i=1:m
for j=1:n
if(e(i,j)==1)
%0 for black pixel and 1 for white
%only white pixel enters the if loop position of the white
%pixel are stored in a variable i1 & j1
k2=e(i,j);
i1=i;
j1=j;
break
end
end
break
end
k=1;
k1=1;
for i=1:m
for j=1:n
if(e(i,j)==1)
disi(k)=abs(i-i1);
disj(k1)=abs(j-j1);
k=k+1;
k1=k1+1;
end
end
end
maxi=max(disi);
imagesc(img(:,:,1));
r1=img(:,:,1);
g1=img(:,:,2);
b1=img(:,:,3);
m1=mean2(g1);
m2=mean2(r1);
m3=mean2(b1);
r2=m2/(m1+m2+m3);
if(r2>0.4)
c=1;
end
subplot(331), imshow(img);
subplot(332), imshow(im1);
subplot(333), imshow(e);
subplot(334),imshow(r2);
Can anyone help me out, how can i find the diameter from an binary image.
2 件のコメント
回答 (2 件)
Gopichandh Danala
2018 年 4 月 9 日
編集済み: Gopichandh Danala
2018 年 4 月 9 日
You can use regionprops to find many properties of BW.
props = regionprops(BW,'EquivDiameter');
% equiv diameter of a circle of same area
equiv_diameter1 = [props.EquivDiameter];
In case if you want to do this yourselves use find, pdist to compute max cord_length (diameter)
% raw way to find max chord length for irregular object
[row, col, ~] = find(BW);
all_cords = [row, col];
max_chordLength = max(pdist(all_cords));
4 件のコメント
Gopichandh Danala
2018 年 4 月 10 日
編集済み: Gopichandh Danala
2018 年 4 月 10 日
I don't recommend the way you wrote the code and your code doesn't have the formula to compute Euclidean distance.
From my understanding and in the way you are expecting this is what you are looking for:
%%I think this is what you are planning to do (not recommended)
% Get all cordinates where the pixel value is 1
[nrow, ncol] = size(img);
cords = double.empty();
for row = 1:nrow
for col = 1:ncol
if BW(row, col) == 1
cords = [cords;[row,col]];
end
end
end
% Then compute all the distances between all points in cords manually
num_cords = length(cords);
all_dist = zeros(num_cords^2,1);
counter = 1;
for idx = 1:num_cords
for vals = 1:num_cords
all_dist(counter) = sqrt(abs((cords(idx,1)-cords(vals,1))^2 + (cords(idx,2)-cords(vals,2))^2));
counter = counter + 1;
end
end
max_D = max(all_dist);
But I recommend to use inbuilt functions matlab provides:
%%This can be simply done in many other efficient ways
% Image Analyst answer..
% BW2 = bwdist(BW);
% max_D2 = max(BW2(:));
% or..
% [row, col, ~] = find(BW);
% cords3 = [row, col];
% max_D3 = max(pdist(cords3));
Image Analyst
2018 年 4 月 10 日
Not sure what you're after with your confusing code. The best I can figure is that you're trying to identify the boundary coordinates of the apple. If so, just threshold and call bwboundaries. It's like 2 lines of code. Here is a full demo with fancy displays and lots of comments. See the upper left image with the yellow outline around the apple.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
fullFileName = 'D:\Matlab\work\Tests\apple.png';
[folder, baseFileName, ext] = fileparts(fullFileName);
%=======================================================================================
% Read in demo image.
rgbImage = imread(fullFileName);
% Shrink it to speed it up
% rgbImage = imresize(rgbImage, 0.75);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the original image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Convert ot a grayscale image
grayImage = max(rgbImage, [], 3);
% Display the gray scale image.
subplot(2, 2, 2);
imshow(grayImage, []);
axis on;
caption = sprintf('Gray Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Histogram it
subplot(2, 2, 3);
histogram(grayImage);
grid on;
title('Histogram of Gray Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Determine binary image mask.
mask = grayImage > 128; % Adjust number as necessary.
% Extract largest blob only.
mask = bwareafilt(mask, 1);
%=======================================================================================
% Display the image.
subplot(2, 2, 4);
imshow(mask, []);
axis on;
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;
% bwboundaries() returns a cell array, where each cell contains the row/column coordinates for an object in the image.
% Plot the borders of all the blobs found in the mask image, on the original grayscale image using the coordinates returned by bwboundaries.
subplot(2, 2, 1); % Switch back to the original image in the upper left.
hold on;
boundaries = bwboundaries(mask);
numberOfBoundaries = size(boundaries, 1);
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k};
plot(thisBoundary(:,2), thisBoundary(:,1), 'y', 'LineWidth', 3);
end
hold off;
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Image Processing and Computer Vision についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!