Why my image cannot dynamically crop?

4 次查看(过去 30 天)
Hello. I want to ask. I want to crop my image data with specific area with a specific part of an image. And I want to use auto dynamic cropping method in matlab. I'm using ultrasound data. Why does my cropped image not appear on the final figure? Here's my codes and I attach with my image:
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;
mypath = '/home/motmot/Documents/test/';
dinfo = dir(fullfile(mypath,'*.jpg'));
filenames = fullfile({dinfo.folder}, {dinfo.name});
nfiles = length(filenames);
for K = 1 : nfiles
thisfile = filenames{K};
[folder, basename, ext] = fileparts(thisfile);
outfile = fullfile(folder, basename + "_crop" + ext);
I = imread(thisfile);
% Get the dimensions of the image. numberOfColorChannels should be = 3.
[rows, columns, numberOfColorChannels] = size(I);
% Display the original color image.
subplot(2, 3, 1);
imshow(I);
axis on;
hp = impixelinfo(); % Show x,y and RGB as you mouse around.
title('Original Color Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Take the red channel.
grayImage = I(:, :, 1);
% Erase the first 110 columns to get rid of the holes.
verticalProfile = mean(grayImage, 2);
for col = 1 : 110
grayImage(:, col) = verticalProfile;
end
% Run a texture filter on it.
stdImage = stdfilt(grayImage);
% Display the image.
subplot(2, 3, 2);
imshow(stdImage, []);
axis on;
title('Filtered Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Enlarge figure
set(gcf, 'Units', 'Normalized', 'Outerposition', [0.05, 0.1, .9, .80]);
% Let's compute and display the histogram.
subplot(2, 3, 3);
histogram(stdImage, 500); % 500 bins.
grid on;
title('Histogram of Filtered image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
% Threshold the image.
binaryImage = stdImage > 5;
% Display the image.
subplot(2, 3, 4);
imshow(binaryImage);
axis on;
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% % Get areas of blobs
% labeledImage = bwlabel(binaryImage);
% props = regionprops(labeledImage, 'Area');
% allAreas = sort([props.Area])
% subplot(2, 3, 5);
% histogram(allAreas); % Show histogram of blob areas.
% title('Blob Areas', 'FontSize', fontSize, 'Interpreter', 'None');
% Get rid of small blobs.
binaryImage = bwareafilt(binaryImage, [70, inf]); % Keep only if 70 pixels or larger.
% Display the image.
subplot(2, 3, 5);
imshow(binaryImage);
axis on;
title('Cleaned Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Find the bounding box.
[blobRows, blobColumns] = find(binaryImage);
row1 = min(blobRows);
row2 = max(blobRows);
col1 = min(blobColumns);
col2 = max(blobColumns);
% Crop it.
croppedImage = rgbImage(row1:row2, col1:col2, :);
% Display the cropped, enhanced color image.
subplot(2, 3, 6);
imshow(croppedImage);
axis on;
title('Cropped Color Image', 'FontSize', fontSize, 'Interpreter', 'None');
imwrite(croppedImage, outfile)
end
And lastly, I want to crop the lesion automatically by 512x512px only include the lesion. I cannot crop the lesion in proper way.
  2 个评论
Image Analyst
Image Analyst 2024-5-8
The axes for subplot(2, 3, 6) is not even showing up in the figure, meaning it never got to that part of your code. Are you sure it did? Set a breakpoint there and you'll see it didn't because you were sloppy in your naming and usage of image variable names. Here is a correction, however your segmentation algorithm of taking the local standard deviation and thresholding it at 5 totally doesn't work for this image -- just look at your binary image!
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;
mypath = '/home/motmot/Documents/test/';
dinfo = dir(fullfile(mypath,'*.jp*'));
filenames = fullfile({dinfo.folder}, {dinfo.name});
nfiles = length(filenames)
for K = 1 : nfiles
thisfile = filenames{K};
[folder, basename, ext] = fileparts(thisfile);
outfile = fullfile(folder, basename + "_crop" + ext);
rgbImage = imread(thisfile);
% Get the dimensions of the image. numberOfColorChannels should be = 3.
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the original color image.
subplot(2, 3, 1);
imshow(rgbImage);
axis on;
hp = impixelinfo(); % Show x,y and RGB as you mouse around.
title('Original Color Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
drawnow;
% Take the red channel.
grayImage = rgbImage(:, :, 1);
% Erase the first 110 columns to get rid of the holes.
verticalProfile = mean(grayImage, 2);
for col = 1 : 110
grayImage(:, col) = verticalProfile;
end
% Run a texture filter on it.
stdImage = stdfilt(grayImage);
% Display the image.
subplot(2, 3, 2);
imshow(stdImage, []);
impixelinfo;
axis on;
title('StDev-Filtered Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Enlarge figure
set(gcf, 'Units', 'Normalized', 'Outerposition', [0.05, 0.1, .9, .80]);
drawnow;
% Let's compute and display the histogram.
subplot(2, 3, 3);
histogram(stdImage, 500); % 500 bins.
grid on;
title('Histogram of StDev-Filtered image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
drawnow;
% Threshold the image.
binaryImage = stdImage > 5;
% Display the image.
subplot(2, 3, 4);
imshow(binaryImage);
axis on;
title('Binary Image where SD > 5', 'FontSize', fontSize, 'Interpreter', 'None');
% % Get areas of blobs
% labeledImage = bwlabel(binaryImage);
% props = regionprops(labeledImage, 'Area');
% allAreas = sort([props.Area])
% subplot(2, 3, 5);
% histogram(allAreas); % Show histogram of blob areas.
% title('Blob Areas', 'FontSize', fontSize, 'Interpreter', 'None');
% Get rid of small blobs.
binaryImage = bwareafilt(binaryImage, [70, inf]); % Keep only if 70 pixels or larger.
% Display the image.
subplot(2, 3, 5);
imshow(binaryImage);
axis on;
title('Cleaned Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Find the bounding box.
[blobRows, blobColumns] = find(binaryImage);
row1 = min(blobRows)
row2 = max(blobRows)
col1 = min(blobColumns)
col2 = max(blobColumns)
% Crop it.
croppedImage = rgbImage(row1:row2, col1:col2, :);
% Display the cropped, enhanced color image.
subplot(2, 3, 6);
imshow(croppedImage);
axis on;
title('Cropped Color Image', 'FontSize', fontSize, 'Interpreter', 'None');
imwrite(croppedImage, outfile)
end
Dayangku Nur Faizah Pengiran Mohamad
Thank you Sir. I have separately the codes to solve this.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by