フィルターのクリア

Why the codes detects the binary image wrong?

1 回表示 (過去 30 日間)
Dayangku Nur Faizah Pengiran Mohamad
Dayangku Nur Faizah Pengiran Mohamad 2024 年 5 月 13 日
コメント済み: Image Analyst 2024 年 5 月 15 日
Hello all. Why the codes detects the binary image the wrong one? Below I attach the codes, and sample of my data.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%===============================================================================
% Read in gray scale demo image.
rgbImage = imread('1_245.jpg');
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(rgbImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = rgbImage(:, :, 2); % Take green channel.
else
grayImage = rgbImage; % It's already gray scale.
end
% Now it's gray scale with range of 0 to 255.
% Display the image.
subplot(2, 3, 1);
imshow(grayImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% Show the histogram
subplot(2, 3, 2);
[counts, grayLevels] = histcounts(grayImage(:));
counts(counts == max(counts)) = 0; % Suppress spike
bar(grayLevels(1:end-1), counts);
grid on;
title('Histogram of Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Median Filter to get rid of seams in wall.
windowSize = 11;
mfImage = medfilt2(grayImage, [windowSize, windowSize]);
% Display the image.
subplot(2, 3, 3);
imshow(mfImage, []);
title('Median Filtered Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
windowSize = 13;
sdImage = stdfilt(mfImage, ones(windowSize));
% Display the image.
subplot(2, 3, 4);
imshow(sdImage, []);
title('Standard Deviation Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
% Show the histogram
subplot(2, 3, 5);
[counts, grayLevels] = histcounts(sdImage(:));
counts(counts == max(counts)) = 0; % Suppress spike
bar(grayLevels(1:end-1), counts);
grid on;
title('Histogram of Standard Deviation Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Binarize the image
binaryImage = sdImage >= 25;
% Remove blobs touching border.
binaryImage = imclearborder(binaryImage);
% Take largest blob
binaryImage = bwareafilt(binaryImage, 1);
% Display the image.
subplot(2, 3, 6);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
% Get widths as a function of row
widths = zeros(rows, 1);
for row = 1 : rows
col1 = find(binaryImage(row, :), 1, 'first');
col2 = find(binaryImage(row, :), 1, 'last');
if ~isempty(col1)
widths(row) = col2 - col1 + 1;
end
end
figure
subplot(2, 1, 1);
plot(widths, 'b-', 'LineWidth', 2);
xlabel('Row', 'FontSize', fontSize);
ylabel('Width', 'FontSize', fontSize);
% Find valleys byinverting and finding peaks
[peakValues, indexes] = findpeaks(-widths);
peakValues = -peakValues; % Negate to flip over.
grid on;
hold on;
plot(indexes, peakValues, 'r+', 'MarkerSize', 12, 'LineWidth', 2);
% The neck is the first peak.
neckRow = indexes(1);
% Display the original image again.
subplot(2, 1, 2);
imshow(grayImage, []);
axis('on', 'image');
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
% Put a line up over the neck.
hold on;
line(xlim, [neckRow, neckRow], 'Color', 'r', 'LineWidth', 2);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);

採用された回答

Image Analyst
Image Analyst 2024 年 5 月 13 日
That's a demo of mine that was probably not developed for your particular image. You'll have to modify it for your image. I don't know what you're trying to find in your original image so I don't know what to suggest. What are you trying to find and measure exactly?
  4 件のコメント
Dayangku Nur Faizah Pengiran Mohamad
Dayangku Nur Faizah Pengiran Mohamad 2024 年 5 月 15 日
Hello Sir. Here's the original one and with the outline region. If I want crop it automatically, how Sir? Can you show how to crop using Matlab code?
Image Analyst
Image Analyst 2024 年 5 月 15 日
Try the attached. Adapt as needed.

サインインしてコメントする。

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by