Image separation into different layers to find percentage of contents in different layers
1 次查看(过去 30 天)
显示 更早的评论
Hi, I have an Image of a metal. I have to find the percentage of the 3 micro structures. So,I have separated the Image into 3 layers by Gray values. I have to apply the threshold manually every time for different Images. Is there any other way to do it automatically? If it is not possible, is there any other better code for my program? Kindly someone review the program. I am also trying to learn more ways to solve the problem.
close all;clear all;clc
warning('off', 'Images:initSize:adjustingMag');
[filename, folder, filterindex] = uigetfile( ...
{ '*.jpg;*.tif;*.png;*.gif','All Image Files';'*.*','All Files'}, ...
'Pick a file', ...
'MultiSelect', 'off');
OG = imread(char(fullfile(folder,filename)));
grayOG = rgb2gray(OG);
h=imhist(grayOG,256);
figure;
subplot(1,2,1);imshow(OG);title('Original Picture');
subplot(1,2,2);imshow(grayOG);title('Grayscale image');
k=max(h);
figure;
bar(h,'showbaseLine','off');xlim([-1 length(h)+1]);ylim([-k/50 k+k/50]);title('Histogram');
threshold_layer_1_start = 0;
threshold_layer_1_end = 85;
threshold_layer_2_start = 86;
threshold_layer_2_end = 149;
threshold_layer_3_start = 150;
threshold_layer_3_end = length(h);
layer_inclusions = (abs(sign(sign(threshold_layer_1_start - grayOG) + sign(threshold_layer_1_end - grayOG))));
logical_inclusions = not(layer_inclusions);
logical_inclusions(1845:1907,2365:2532)= 1;
logical_martensite = not(abs(sign(sign(threshold_layer_2_start - grayOG) + sign(threshold_layer_2_end - grayOG))));
logical_martensite = not(imsubtract(logical_martensite,logical_inclusions));
logical_martensite(1845:1907,2365:2532)= 1;
logical_austenite = not(abs(sign(sign(threshold_layer_3_start - grayOG) + sign(threshold_layer_3_end - grayOG))));
logical_austenite = not(imsubtract(logical_austenite,logical_inclusions));
logical_austenite = not(imsubtract(logical_austenite,logical_martensite));
logical_austenite(1845:1907,2365:2532)= 1;
combined = imcomplement(imfuse(imfuse(logical_martensite,logical_inclusions),logical_austenite));
percentage_inclusions=fun(logical_inclusions);
percentage_martensite=fun(logical_martensite);
percentage_austenite=fun(logical_austenite);
figure;
subplot(2,2,1);imshow(logical_inclusions,'Border','tight');title('Layer A - Inclusions');
subplot(2,2,2);imshow(logical_martensite,'Border','tight');title('Layer B - Martensite');
subplot(2,2,3);imshow(logical_austenite,'Border','tight');title('Layer C - Austenite');
subplot(2,2,4);imshow(combined,'Border','tight');title('Combined Picture of A, B & C');
function percentage = fun(x)
zwnew = find(x==1);
zbnew = find(x==0);
percentage = (length(zbnew)/(length(zbnew)+length(zwnew)))*100;
end
0 个评论
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!