接する円を作製して幅を測定したいです。

3 views (last 30 days)
Shota
Shota on 13 Feb 2023
Commented: Shota on 14 Feb 2023
下記コードを用い、添付の液体画像(Image1.bmp)を読み込んで、二値化してスケルトン処理で中心線をだします。
その後、その中心線状に中心を持ち、元画像の液体の両方に接するような円を作製し、その直径から幅をはかるプログラムが作製したいです。
以下、スケルトン処理までのコードです。
宜しくお願いいたします。
%画像の読み込み
file1 = 'Image1.bmp';
I1 = imread(file1);
%二値化
BW1 = imbinarize(I1,0.528);
%左側除去
BW1(1:256,1:159) = 1;
%右側除去
BW1(1:256,259:512) = 1;
%白黒反転
BW1 = imcomplement(BW1);
%画像調整
BW1 = bwareafilt(BW1,1);
BW1 = imfill(BW1,"holes");
%モルフォロジー処理
BW2 = bwskel(BW1,'MinBranchLength',50);
BW2 = bwareaopen(BW2,4);
BW2(223:256,1:512) = 0;
%元画像に液体中心線を重ねる
BW2 = imcomplement(BW2);
file2 = "Image2.bmp";
imwrite(BW2,file2);
I2 = imread(file2);
masked_image = I1 .* uint8(I2);
file3 = "Image3.bmp";
imwrite(masked_image,file3);

Accepted Answer

Tohru Kikawada
Tohru Kikawada on 13 Feb 2023
液体の流れに内接する円の半径を見つけたいということだと理解しました。
冗長かもしれないですが下記のコードでやられたいに近いことができるかと思います。
このような例を探す場合には英語で「 inscribed circle maximum radius」などで検索されると見つけやすいかもしれません。
%画像の読み込み
file1 = 'Image1.bmp';
I1 = imread(file1);
%二値化
BW1 = imbinarize(I1,0.528);
%左側除去
BW1(1:256,1:159) = 1;
%右側除去
BW1(1:256,259:512) = 1;
%下側除去
BW1(end-50:end,:) = 1;
% オープン処理&面積フィルタでゴミを取り除き、穴埋め
BW1 = imopen(~BW1,strel("disk",2));
BW1 = bwareaopen(BW1,300);
BW1 = imfill(BW1,"holes");
% 画像枠に接触しているオブジェクトを除去
BW1 = imclearborder(BW1);
imshow(BW1)
% 輪郭抽出
[B,L] = bwboundaries(BW1,'noholes');
imshow(label2rgb(L, @jet, [.5 .5 .5]))
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end
% 距離画像を作り、ピーク値が半径、場所が円の中心
% Image Analyst様の回答を参考に作成
% https://www.mathworks.com/matlabcentral/answers/357943-maximum-inscribed-circle-problem#answer_282733
mask = poly2mask(boundary(:,2), boundary(:,1), size(BW1,1), size(BW1,2));
hold off;
imshow(mask)
edtImage = bwdist(~mask);
imagesc(edtImage)
axis image
maxRadius = max(edtImage(:));
[y, x] = find(edtImage == maxRadius);
% 可視化
I2 = insertShape(I1,"circle",[x, y, maxRadius],"Color","red");
I2 = insertText(I2,[x,y]+10,"Radius:"+string(maxRadius)+" pixel",...
"FontSize",20);
imshow(I2);
  1 Comment
Shota
Shota on 14 Feb 2023
迅速にご対応いただきありがとうございます。
無事に測定がおこなえそうです。ありがとうございます。

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!