Hatch pattern inside connected components in a binary image

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.

5 commentaires

Matt J
Matt J le 24 Mar 2022
Modifié(e) : Matt J le 24 Mar 2022
Francesco Pignatelli's comment moved here:
Hello Ankit,
I am facing a similar problem, did you solve it in the end?
I am also struggling on generating a file similar to your 'BinMat.mat' since I did not understand how to extract an edge from my PLIF data. I think we have a similar situation, I guess your data come from a swirling flame.
Could you please share with me your knowledge about how to get the edge binarizing a a matrix?
Best
Francesco
Hi Francesco,
I apologize for the late reply. I was not able to solve it, although primarily the reason might have been that I had to postpone the project, and I have not been able to pick it up until now. It has been quite a time since I have attempted to solve this, maybe some fresh ideas will help me solve it. I will let you know about the results.
You are right, my data comes from a swirl combustor. For edge detection, I initially use 2D-median filtering (medfilt2), followed by a combination of operations using adaptthresh, imfill, imdilate, and bmorph to detect the edges. If you give me some time, I will try to find a working algorithm to include here along with a sample data.
Again, I apologize for not following up on this problem, I should've. I will pick this up again as soon as I have some time (PhD is tougher than I expected!!). Please let me know if you have any more questions.
Hi Francesco, I apologize for the late reply. I was not able to solve it,
@Ankit Sahay Puzzling. I gave you a solution in my answer below which seems to work as requested. You made no comment on it at all.
Hello @Ankit Sahay, no worries at all for the delay, it is totally ok :) Thank you very much! Looking forward to see your algorithm. Also I am still working on it but I realized I have an issue on binarizing so I am still one step behind this issue. Once that is fixed I will keep on finding the solution related to the issue of this post and I will also try the @Matt J solution and I will come back here with a possibile solution (if I can find it). Again, thank you very much :)

Connectez-vous pour commenter.

Réponses (1)

Matt J
Matt J le 11 Mar 2021
Modifié(e) : Matt J le 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)

Catégories

En savoir plus sur Images dans Centre d'aide et File Exchange

Produits

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by