How to remove frame appear as a result of thresholding

1 回表示 (過去 30 日間)
yasmin ismail
yasmin ismail 2023 年 7 月 10 日
コメント済み: Image Analyst 2023 年 8 月 3 日
it is working well but when I take the result as input to to get threshold the result appear with outerframe as shown in attachment
how can I remove this frame or not allowed it to appear?

回答 (2 件)

Walter Roberson
Walter Roberson 2023 年 7 月 10 日
Your image has a white border that you need to trim out.
bb1 = imread('bb1.jpg');
imshow(bb1); title('original');
set(gca, 'visible', 'on')
is_all_white = all(bb1 == 255, 3);
vert_profile = ~all(is_all_white, 1);
horz_profile = ~all(is_all_white, 2);
first_col = find(vert_profile, 1);
last_col = find(vert_profile, 1, 'last');
first_row = find(horz_profile, 1);
last_row = find(horz_profile, 1, 'last');
bb1_cropped = bb1(first_row:last_row, first_col:last_col, :);
imshow(bb1_cropped); title('cropped');
set(gca, 'visible', 'on');
  4 件のコメント
yasmin ismail
yasmin ismail 2023 年 7 月 30 日
My image is png but have same problem as shown in attach how to fix it please?
Note, i want to use this image as input to another script
DGM
DGM 2023 年 7 月 30 日
Note that there are two separate issues. The primary problem is that the image is at some point being padded by capturing a screenshot. The padding will create false edges no matter what image format is used. The use of JPG simply degrades the image further, making it slightly more problematic to crop the result.
Given the inconsistent and nonstandard geometry of both the padded and base images in these samples, that's by far the most likely reason they've appear this way.
If you save an image by displaying it and then taking a screenshot with saveas(), print(), etc, the result will not reliably be the same size as the original, and it will contain an unpredictable amount of white padding. In other words, a screenshot is not a faithful copy of the image stored in memory. Save the image directly using imwrite().

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


Image Analyst
Image Analyst 2023 年 7 月 27 日
Avoid the white frame altogether by using imwrite to save the image, rather than saveas or print or whatever other function you're using.
  17 件のコメント
yasmin ismail
yasmin ismail 2023 年 8 月 2 日
@Image Analyst I did it but still error
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 = 18;
filename = 'c1-1fsr.png';
rgbImage = imread(filename);
subplot(1, 2, 1);
imshow(rgbImage);
% Now it's gray scale with range of 0 to 255.
impixelinfo; % Let user mouse around and see values in the status line at the lower right.
title('Original Input Image', 'FontSize', fontSize);
% Now segment the image.
% Strange though -- it seems to be binary (i.e. segmented) already
% or the image is way over exposed.
outputImage = AnalyzeSingleImage(rgbImage);
% Display output image.
subplot(1, 2, 2);
imshow(outputImage); % Result of process 1
title('Output Mask Image', 'FontSize', fontSize);
msgbox('done');
% Now your segmentation app - simple global thresholding.
function mask = AnalyzeSingleImage(grayImage)
% 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(grayImage)
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(outputImage);
% 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 = grayImage(:, :, 3); % Take Blue channel.
else
grayImage = grayImage; % It's already gray scale.
end
% Binarize the image.
% mask = imbinarize(grayImage);
lowThreshold = 0;
highThreshold = 220;
% Interactively and visually set a threshold on a gray scale image.
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
% [lowThreshold, highThreshold] = threshold(lowThreshold, highThreshold, grayImage);
mask = grayImage >= lowThreshold & grayImage <= highThreshold;
% Fill holes.
mask = imfill(mask, 'holes');
subplot(2, 3, 2);
imshow(mask)
impixelinfo; % Let user mouse around and see values in the status line at the lower right.
title('Mask', 'FontSize', fontSize)
% Take largest blob only.
% mask = bwareafilt(mask, 1);
% Compute the skeleton
skelImage = bwskel(mask);
% Find the areas of all the skeletons.
props = regionprops(skelImage, 'Area');
allAreas = sort([props.Area])
% Extract only skeletons longer than 60 pixels.
skelImage = bwareaopen(skelImage, 60);
subplot(2, 3, 3);
imshow(skelImage)
impixelinfo; % Let user mouse around and see values in the status line at the lower right.
title('Thinned', 'FontSize', fontSize)
% Enlarge figure to full screen.
g = gcf;
g.WindowState = 'maximized';
% Compute the Euclidean distance image.
edtImage = bwdist(~mask);
subplot(2, 3, 4);
imshow(edtImage, [])
title('Distance Transform Image', 'FontSize', fontSize);
impixelinfo; % Let user mouse around and see values in the status line at the lower right.
% Multiply them to get an image where the only pixels in the image
% are along the skeleton and their value is the radius.
% Multiply radius image by 2 to get diameter image.
diameterImage = 2 * edtImage .* single(skelImage);
subplot(2, 3, 5);
imshow(diameterImage, [])
title('Diameter Image', 'FontSize', fontSize);
impixelinfo; % Let user mouse around and see values in the status line at the lower right.
% Get the widths. These will be where the image is not zero.
widths = diameterImage(diameterImage > 0);
% Show histogram of widths.
subplot(2, 3, 6);
histogram(widths);
grid on;
xlabel('Width in Pixels', 'FontSize', fontSize);
ylabel('Count', 'FontSize', fontSize);
% Compute the mean width
meanWidth = mean(widths)
% Put a line on the histogram at the mean width
xline(meanWidth, 'LineWidth', 2, 'Color', 'r');
caption = sprintf('Histogram of Widths. Mean Width = %.1f Pixels', meanWidth);
title(caption, 'FontSize', fontSize);
message = sprintf('Mean Width = %.1f Pixels', meanWidth);
msgbox(message);
end
Unrecognized function or variable 'fontSize'.
Error in Untitled2>AnalyzeSingleImage (line 52)
title('Mask', 'FontSize', fontSize)
Error in Untitled2 (line 18)
outputImage = AnalyzeSingleImage(rgbImage);
Image Analyst
Image Analyst 2023 年 8 月 3 日
You need to also define fontSize inside AnalyzeSingleImage(). Since it was not passed in via the argument list, the function has no idea what that is, so you need to assign it to some value, like you did in the main program.

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

Community Treasure Hunt

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

Start Hunting!

Translated by