MATLAB Answers

Hatch pattern inside connected components in a binary image

22 views (last 30 days)
Ankit Sahay
Ankit Sahay on 11 Mar 2021
Edited: Matt J on 11 Mar 2021
Hi everybody!
I have attached a binary matrix (.mat file attached), which looks something like this:
I would like to put hatch pattern inside the two curves, something like this:
I looked at this answer, but couldn't use it to my advantage.
I also have the means of demarcating the different regions, something like this:
As you can say, the regions inside, outside, and on the curve have different cell values. I can demarcate the region, but I am not able to fill the "inside" region with a hatched pattern. The code to obtain the third image from the first image is attached below (it may be an inefficient code, but efficiency isn't my primary concern right now).
%% Separate binary matrix into 2 parts
BinMat = mydata;
BinMat_left = BinMat(:,1:end/2);
BinMat_right = BinMat(:,(end/2)+1:end);
%% Processing for left part
BinImage_right = ~logical(BinMat_left);
% Get a marker image to reconstruct just the connected region and not all the other disconnected regions.
binaryMarkerImage_left = false(size(BinMat_left));
% Specify the starting point - a marker.
row = 1;
column = 3;
binaryMarkerImage_left(row, column) = true;
reconImage_left = imreconstruct(binaryMarkerImage_left, BinImage_right, 4);
filled_win_left = BinMat_left + 2 * reconImage_left;
filled_win_left = imbinarize(filled_win_left);
filled_win_left = imcomplement(filled_win_left);
l_left = bwlabel(filled_win_left,4);
[r_left, c_left] = find(l_left==2);
indices_left = [r_left,c_left];
for len_left = 1:length(r_left)
l_left(indices_left(len_left,1),indices_left(len_left,2)) = 0;
end
dil_ima_2_left = l_left;
edge_corrected_left1 = edge(l_left,'canny');
edge_corrected_left = bwareaopen(edge_corrected_left1,2);
% Demarcating regions on either side of flame edge
I_left = double(edge_corrected_left); % BW2 is the logical matrix with flame edge data
[m_left,n_left] = size(I_left);
[xx_left,yy_left] = find(I_left); % find coordinates of flame edge which will work as nodes
% dil_ima_2 = flip(dil_ima_2,1); % dil_ima_2 is the binarized image obtained PLIF image
for i_left = 1:m_left
for j_left = 1:n_left
if (dil_ima_2_left(i_left,j_left)==0)
I_left(i_left,j_left)=-3;
end
if (dil_ima_2_left(i_left,j_left)==1)
I_left(i_left,j_left)=-2;
end
end
end
II_l = double(edge_corrected_left);
% This loop assigns -3 to region outside the flame edge, 1 to flame edge & -2 to region inside the flame edge in I_l
for i_left = 1:m_left
for j_left = 1:n_left
if(II_l(i_left,j_left)==1)
I_left(i_left,j_left) = II_l(i_left,j_left);
end
end
end
%% Processing for right part
BinImage_right = ~logical(BinMat_right);
% Get a marker image to reconstruct just the connected region and not all the other disconnected regions.
binaryMarkerImage_right = false(size(BinMat_right));
% Specify the starting point - a marker.
row = 1;
column = 3;
binaryMarkerImage_right(row, column) = true;
reconImage_right = imreconstruct(binaryMarkerImage_right, BinImage_right, 4);
filled_win_right = BinMat_right + 2 * reconImage_right;
filled_win_right = imbinarize(filled_win_right);
filled_win_right = imcomplement(filled_win_right);
l_right = bwlabel(filled_win_right,4);
[r_right, c_right] = find(l_right==2);
indices_right = [r_right,c_right];
for len_right = 1:length(r_right)
l_right(indices_right(len_right,1),indices_right(len_right,2)) = 0;
end
dil_ima_2_right = l_right;
edge_corrected_right1 = edge(l_right,'canny');
edge_corrected_right = bwareaopen(edge_corrected_right1,2); % Remove isolated non-connected pixels from all images
% Demarcating regiins inside and outside the flame edge
I_right = double(edge_corrected_right);
[m_right,n_right] = size(I_right);
[xx_right,yy_right] = find(I_right); % find coordinates of flame edge which will work as nodes
% dil_ima_2 = flip(dil_ima_2,1); % dil_ima_2 is the binarized image obtained PLIF image
for i_right = 1:m_right
for j_right = 1:n_right
if (dil_ima_2_right(i_right,j_right)==0)
I_right(i_right,j_right)=-3;
end
if (dil_ima_2_right(i_right,j_right)==1)
I_right(i_right,j_right)=-2;
end
end
end
II_right = double(edge_corrected_right);
% This loop assigns -3 to products, 1 to flame edge & -2 to reactants in I_r
for i_right = 1:m_right
for j_right = 1:n_right
if(II_right(i_right,j_right)==1)
I_right(i_right,j_right) = II_right(i_right,j_right);
end
end
end
%% Full I image
I = [I_left I_right];
imagesc(I)
Can someone please help me with this? Also, if it isn't too much trouble, I would like to overlay this hatched image over some other data, something like this:
In this image, I overlay the binary image on a different image. To accomplish this best, I think the best approach would be to fill the region (which is to be hatched) with 1s in a diagonal fashion (in the binary image), such that the 1s themselves will give a hatched pattern. But I haven't been able to do this.
I request someone to please help. Any way forward also would be extremely useful. Thank you.

Answers (1)

Matt J
Matt J on 11 Mar 2021
Edited: Matt J on 11 Mar 2021
Perhaps as follows,
load Regions
[m,n]=size(BW);
N=7; %controls hatch density
hatch = ( mod((1:m).'+ (1:n),N) == (N-1) );
border=BW & ~imerode(BW,ones(3));
BW=BW & hatch | border;
imshow(BW)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by