How to extract region of Interest of gray Image?

5 views (last 30 days)
Hi, I'd like to extract right half and left half of breast and i found borders of left( red line)and right (yellow line)of breast and middle line. Also I seperated image from middle but i couldn't extract both right and left half of image.
Second Image (c) is what i want.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
grayImage = rgb2gray(imread('DINAMIC-FRONTAL1.jpg'));
en = imsharpen(grayImage,'Radius',2,'Amount',1);
B = double(imgaussfilt(en,1.4));
% filtered_image = zeros(size(B));
% Mx = [-1 0 1; -1 0 1; -1 0 1];
% My = [-1 -1 -1; 0 0 0; 1 1 1];
% for i = 1:size(B, 1) - 2
% for j = 1:size(B, 2) - 2
%
% % Gradient approximations
% Gx = sum(sum(Mx.*B(i:i+2, j:j+2)));
% Gy = sum(sum(My.*B(i:i+2, j:j+2)));
%
% % Calculate magnitude of vector
% filtered_image(i+1, j+1) = sqrt(Gx.^2 + Gy.^2);
%
% end
% end
% filtered_image = uint8(filtered_image);
% figure(); imshow(filtered_image); title('Filtered Image');
% thresholdValue = 100; % varies between [0 255]
% output_image = max(filtered_image, thresholdValue);
% output_image(output_image == round(thresholdValue)) = 0;
ed=edge(B,'canny',0.3,0.5);
figure();
imshow(ed);
title('Initial Canny Edge', 'FontSize', fontSize);
bw1=bwareaopen(ed,10);
se = strel('disk',4);
bw=imdilate(bw1,se);
subplot(2, 2, 2);
imshow(bw);
title('Next Mask', 'FontSize', fontSize);
% Take largest 2 blobs.
bw = bwareafilt(bw, 2);
% Erase top half
[rows, columns] = size(bw);
bw(1:round(rows/2), :) = false;
% Skeletonize
bw = bwskel(bw);
% Find branchpoints.
[bpRows, bpColumns] = find(bwmorph(bw, 'branchpoints'))
% Erase branchpoints.
hold on;
for k = 1 : length(bpRows)
bw(bpRows(k), bpColumns(k)) = false;
plot(bpColumns(k), bpRows(k), 'r.', 'MarkerSize', 50);
end
% Erase any blobs with a centroid below 400.
props = regionprops(bw, 'Centroid');
xy = vertcat(props.Centroid)
% Don't keep the two with the highest centroid.
indexesToKeep = find(xy(:, 2) < 405);
% Extract all but the lowest two.
labeledImage = bwlabel(bw);
bw = ismember(labeledImage, indexesToKeep);
subplot(2, 2, 3);
imshow(bw);
axis('on', 'image');
title('Final Mask', 'FontSize', fontSize);
% Put the branchpoints back in to make each breast just one curve.
for k = 1 : length(bpRows)
bw(bpRows(k), bpColumns(k)) = true;
end
labeledImage = bwlabel(bw);
% Find the left breast curve
[yr, xr] = find(labeledImage == 1);
% Find the right breast curve
[yl, xl] = find(labeledImage == 2);
subplot(2, 2, 4);
imshow(grayImage, 'border', 'tight' );
hold all
plot(xr, yr, 'r.', 'MarkerSize', 14);
plot(xl, yl, 'y.', 'MarkerSize', 14);
% Find the midline
xMiddle = mean([max(xl), min(xr)]);
xline(xMiddle, 'Color', 'm', 'LineWidth', 4);
title('Breast Outlines Overlaid on Original Image', 'FontSize', fontSize);
leftHalf = grayImage(:, 1:xMiddle, :);
rightHalf=grayImage(:, xMiddle+1:end, :);
figure(); imshow(leftHalf);
figure(); imshow(rightHalf);

Accepted Answer

Image Analyst
Image Analyst on 8 Oct 2021
  12 Comments
Beren Ac
Beren Ac on 9 Oct 2021
Can we use circle fitting by using xr-yr and middle line for right half of breast and xl-yl and middle line for left half. If yes, could you please help me for it? There is as picture(section b)red circles below;

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!