しきい値ごとの面積を計算 グラフ作成

16 views (last 30 days)
kou
kou on 17 Jan 2020
Commented: kou on 20 Jan 2020
ある一枚の画像を対象に0から255のしきい値で二値化しそれぞれのしきい値の時のピクセル値を求めて
横軸を0から255 縦軸をそれぞれのしきい値の時に求めたピクセル値をプロットしたグラフ作成したいのですが、
どのようにすればいいのでしょうか。どなたかご教授お願いいたします。
また、それぞれのしきい値での二値化された画像を一つのフィギュアにまとめて載せたいのですが、上書きされて一枚しか表示されません。合わせてお願いいたします。
clear all;
close all;
% 各種定義
fig = 0;
for i=1
%Image Read
Imgfilename = strcat('./',num2str(i),'.jpg');
img=imread(Imgfilename);
gimg=rgb2gray(img);
for j=0:255
BW = gimg>j;
BW2 = medfilt2(BW);
BW3 = imfill(BW2,'holes');
BW4 = im2uint8(BW3);
BW_out = BW;
% Remove portions of the image that touch an outside edge.
BW_out = imclearborder(BW_out);
% Fill holes in regions.
BW_out = imfill(BW_out, 'holes');
BW_out = bwpropfilt(BW_out, 'Area', [20, 526]);
% Get properties.
properties = regionprops(BW_out, {'Area'});
B=bwarea(BW);
end
end

Accepted Answer

Kenta
Kenta on 18 Jan 2020
こんにちは、以下のようにすればできます。
「縦軸をそれぞれのしきい値の時に求めたピクセル値」とは、変数Bのことで正しいでしょうか。
上は閾値ごとに2値化したもの、下が、Bをプロットした図です。
example.jpg
plots.jpg
clear;clc;close all;
for i=1
%Image Read
% Imgfilename = strcat('./',num2str(i),'.jpg');
% img=imread(Imgfilename);
img=imread('onion.png');
gimg=rgb2gray(img);
range=[0:5:255];
% define the cell variable for montage display
C=cell(numel(range)+1,1);
numPixel=zeros(numel(range),1);
C{1}=img;
count=1;
for j=range
BW = gimg>j;
BW2 = medfilt2(BW);
BW3 = imfill(BW2,'holes');
BW4 = im2uint8(BW3);
BW_out = BW;
% Remove portions of the image that touch an outside edge.
BW_out = imclearborder(BW_out);
% Fill holes in regions.
BW_out = imfill(BW_out, 'holes');
BW_out = bwpropfilt(BW_out, 'Area', [1, 1000]);
% insert a title for the threshold determination
BW_out = insertText(double(BW_out),[round(size(BW,2)/3),1], ...
strcat('j:',num2str(j)),'FontSize',18,'BoxOpacity',0.4,'TextColor','white');
C{count+1}=BW_out;
% Get properties.
properties = regionprops(BW_out, {'Area'});
B=bwarea(BW);
numPixel(count)=B;
count=count+1;
end
end
figure;montage(C)
figure;plot(numPixel)
  1 Comment
kou
kou on 20 Jan 2020
無事できました。ありがとうございます!

Sign in to comment.

More Answers (0)

Categories

Find more on イメージ in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!