why the following code do this to make binary a image

1 visualización (últimos 30 días)
I have a code to binaryce a image, but I dont undertand why rest the red chanel and blue chanel and then say if the rest are less than 1 is equal to 0.... if its greater than 1 is equal to 1. Why the code make the rest between RM and BM? what benefits have this? why simply binarice a image
for i = 1:length(Filelist)
CFileName = Filelist(i).name;
filename = CFileName;
IM = imread(CFileName);
IM = imgaussfilt(IM,1.5); % The use of the filter depends on the value standard deviation, i.e. 0.9
IM = double(IM);
RM = IM(:,:,1);
GM = IM(:,:,2);
BM = IM(:,:,3);
mask = IM(:,:,1);
RBD = RM-BM;
GBD = GM-BM;
RGD = RM-GM;
mask(RBD<1) = 0;
% mask(GBD<60) = 0;
% mask(RGD<0) = 0;
mask(mask>1) = 1;
% cd ..
cd Binary_images
% h=figure(1);
% imshow(mask);
imwrite(mask,sprintf(filename));
cd ..

Respuestas (1)

KALYAN ACHARJYA
KALYAN ACHARJYA el 20 de Nov. de 2020
Editada: KALYAN ACHARJYA el 20 de Nov. de 2020
There are several methods to obtain the resulting image as a logical or binary image. That does not mean, why does it not follow direct imbinirize? The main issue is in what context the code has been written?
What is binary image?
Any image with logical data (0 or 1), consider as binary image, supoose you have an image with the following-
mat =
0.2785 0.9706 0.4218 0.0357 0.7431
0.5469 0.9572 0.9157 0.8491 0.3922
0.9575 0.4854 0.7922 0.9340 0.6555
0.9649 0.8003 0.9595 0.6787 0.1712
0.1576 0.1419 0.6557 0.7577 0.7060
Now, in this partcular case, you may get binary image in various way-
bw1=imbinarize(mat);
or
bw2=mat>0.4
Why they do in such way?
Before proceeding to the logical operation in the attached code, it performs channel extraction, then applies a threshold mask to get the result value 0 or 1. The resulting mask here is not a binary image (please verify the data type in the workspace). Just having 0 or 1 in the element does not mean that the image is binary, if the image is binary, you can see the logical data type mentioned in the workspace.
"Why the code is like this"?
I have no answer, in which context the code is written, I have no clue, but it seems that it just implements the basic operation.
Is tehre any sense of doing it this way?
It can only be confimred, after test the with their original image. I have no clue as to which code is written, it is better to ask the person who wrote the code.
Still you did not get the issue, please call me, I will give you my 2 minutes to explain it, as I have seen since yesterday, you are continnusly editing the question several times to get attention from its members.
Good Wishes!
  1 comentario
Cristian camilo García Castaño
Cristian camilo García Castaño el 24 de Nov. de 2020
Hello Kalyan!
Thanls for your answer, i supose the rest in the code is to obtain a umbral, but, dont understandt the sense of do that.
The person who write the code I cant comunicate whit him/her my advisor tell me take that code and try to understand we use thar several time ago to make the operation.... I can share the code most of them.
clc
clear
tic
% Naming of variables
% IM: image
% Segmentation of RGB images using the three channels: red, green, and blue.
% The code starts with seperating the RGB channels into separate matrices
% Oil phase has a big difference between the red and blue chanel, i.e. 50
% Oil phase has a big difference between the green and blue chanel, i.e. 50
% Oil phase has a slight difference between the red and blue chanel, i.e. 20
% Glass and brine phase has no such differences
%% Creating folders for saving the binary and labled images.
% It delete exsiting folders with the same name.
currentdirectory = pwd;
% cd(pwd)
% if 7==exist('Binary_images', 'dir')
% rmdir Binary_images s
% end
% if 7==exist('Labled_images', 'dir')
% rmdir Labled_images s
% end
% mkdir Binary_images;
% mkdir Labled_images;
% Filelist = dir(fullfile(currentdirectory, '*.png'));
%% image processing
%input
con = (248.4)^2; % resolution is 215.6 pix per 1 mm
PVROCK = 88.7138; % pore volume in mm2
for p = 1 : 10
xx= [currentdirectory '\Experiment_' num2str(p)];
if ~exist(xx, 'dir')
break
end
cd (xx)
if 7==exist('Binary_images', 'dir')
rmdir Binary_images s
end
if 7==exist('Labled_images', 'dir')
rmdir Labled_images s
end
mkdir Binary_images;
mkdir Labled_images;
% Get all text files in the current folder
%% to fix the names of the files
files = dir('*.png');
% Loop through each file
for id = 1:length(files)
% Get the file name
[~, f,ext] = fileparts(files(id).name);
L = strlength(f);
if L == 9
rename = strcat( cellstr(f(:,1:7)), cellstr('00'), cellstr(f(:, 8:end)));
movefile(files(id).name, char(rename)+".png");
end
end
%%
Filelist = dir(fullfile(pwd, '*.png'));
data = zeros(length(Filelist),6);
for i = 1:length(Filelist)
CFileName = Filelist(i).name;
filename = CFileName;
IM = imread(CFileName);
IM = imgaussfilt(IM,1.5); % The use of the filter depends on the value standard deviation, i.e. 0.9
IM = double(IM);
RM = IM(:,:,1);
GM = IM(:,:,2);
BM = IM(:,:,3);
mask = IM(:,:,1);
RBD = RM-BM;
GBD = GM-BM;
RGD = RM-GM;
mask(RBD<1) = 0;
% mask(GBD<60) = 0;
% mask(RGD<0) = 0;
mask(mask>1) = 1;
% cd ..
cd Binary_images
% h=figure(1);
% imshow(mask);
imwrite(mask,sprintf(filename));
cd ..
%% image analysis
Label_cc = bwlabel(mask);
cc = bwconncomp(mask);
This code make the following image binary I atach a example.... I have a basics sense to underand the code, and how works, but still understand why rest the channels and why take that way to binarice.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by