How can i get rid of some part of image ?

3 views (last 30 days)
Beren Ac
Beren Ac on 4 Nov 2021
Edited: Beren Ac on 5 Nov 2021
I'd like to ged rid of some part of image that light pixels of out of breast organ (I drew the region with red line that i don't want). Could you please help me for this problem.
Original Image(p287):
Original Image (p179)
% it's for second image. p179
se = strel('disk',4);
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('p287.jpg'));
en = imsharpen(grayImage,'Radius',2,'Amount',1);
B = double(imgaussfilt(en,1.4));
ed=edge(B ,'canny',0.3,0.5);
subplot(2, 2, 1);
imshow(ed);
title('Initial Canny Edge', 'FontSize', fontSize);
bw1=bwareaopen(ed,10);
se = strel('disk',5);
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);
% Find the left breast circle
% x=xr;y=yr; hullflag=1;
% [centerr,radiusr] = minboundcircle(x, y, hullflag);
% x1=xl;y1=yl; hullflag1=1;
% [centerl,radiusl] = minboundcircle(x1,y1,hullflag1);
% Find the leftmost point and don't include any points with y values above it.
[xMin, index] = min(xl);
keeperIndexes = yl > yl(index)
xll = xl(keeperIndexes);
yll = yl(keeperIndexes);
% Fit a circle to the left coordinates
[xCenter1, yCenter1, radius1, a1] = circlefit(xll, yll);
% Find the rightmost point and don't include any points with y values above it.
[xMax, index] = max(xr);
keeperIndexes = yr > yr(index)
xrr = xr(keeperIndexes);
yrr = yr(keeperIndexes);
% Fit a circle to the right coordinates
[xCenter2, yCenter2, radius2, a2] = circlefit(xrr, yrr);
% View the circles
% center1= [xCenter1, yCenter1];
% center2=[xCenter2, yCenter2];
% radii = [radius1; radius2];
% centerl = [xCenter1, yCenter1];
% centerr=[xCenter2, yCenter2];
theta = 0 : 0.01 : 2*pi;
% Equation of left
x = radius1 * cos(theta) + xCenter1;
y = radius1 * sin(theta) + yCenter1;
% Equation of right
x2 = radius2 * cos(theta) + xCenter2;
y2 = radius2 * sin(theta) + yCenter2;
subplot(2, 2, 4);
imshow(grayImage, 'border', 'tight' );
hold all
roi = drawpolygon(gca,'Position',[x;y]');
plot(xr, yr, 'r.', 'MarkerSize', 4);
plot(xl, yl, 'y.', 'MarkerSize', 4);
plot(x, y, 'b.', 'MarkerSize', 4);
plot(x2, y2, 'b.', 'MarkerSize', 4);
xMiddle = mean([max(xl), min(xr)]);
xline(xMiddle, 'Color', 'm', 'LineWidth', 4);
title('Breast Outlines Overlaid on Original Image', 'FontSize', fontSize);
hold off
grayImage1=grayImage
[nx,ny,d] = size(grayImage1) ;
[X,Y] = meshgrid(1:ny,1:nx) ;
idx = inpolygon(X(:),Y(:),x,y) ;
for i = 1:d
I1 = grayImage1(:,:,i) ;
I1(~idx) = 255 ;
grayImage1(:,:,i) = I1 ;
end
figure();
imshow(grayImage1);
grayImage5=grayImage;
idx3=inpolygon(xr',yr,x2',y2);
for i = 1:d
I1 = grayImage5(:,:,i) ;
I1(~idx) = 255 ;
grayImage5(:,:,i) = I1 ;
end
figure();
imshow(grayImage5);
grayImage6 = min(grayImage5, [], 3);
binaryImage = grayImage6 < 200;
binaryImage = bwareafilt(binaryImage, 1);
[rows, columns] = find(binaryImage);
row1r = min(rows);
row2r = max(rows);
col1r = min(columns);
col2r = max(columns);
% Crop
croppedImage1 = grayImage5(row1r:row2r, col1r:col2r, :);
figure();imshow(croppedImage1);
croppedImage1(croppedImage1 == 255) = 233;
subplot(2, 3, 1);
figure();imshow(croppedImage1);
grayImage2=grayImage;
[nx1,ny1,d1] = size(grayImage2) ;
[X1,Y1] = meshgrid(1:ny1,1:nx1) ;
idx1 = inpolygon(X1(:),Y1(:),x2,y2) ;
for i = 1:d1
I1 = grayImage2(:,:,i) ;
I1(~idx1) = 255 ;
grayImage2(:,:,i) = I1 ;
end
figure();
imshow(grayImage2);
grayImage3=grayImage;
idx2=inpolygon(xl',yl,x',y);
for i = 1:d1
I1 = grayImage3(:,:,i) ;
I1(~idx1) = 255 ;
grayImage3(:,:,i) = I1 ;
end
figure();
imshow(grayImage3);
grayImage4 = min(grayImage3, [], 3);
binaryImage = grayImage4 < 200;
binaryImage = bwareafilt(binaryImage, 1);
[rows, columns] = find(binaryImage);
row1 = min(rows);
row2 = max(rows);
col1 = min(columns);
col2 = max(columns);
% Crop
croppedImage = grayImage3(row1:row2, col1:col2, :);
figure();imshow(croppedImage);
C = maxhessiannorm(croppedImage);
figure();imshow(C);
croppedImage(croppedImage == 255) = 233;
figure();imshow(croppedImage);

Answers (1)

Image Analyst
Image Analyst on 4 Nov 2021
I know from your prior posts that you have the outline of the breasts. So you just need to mask outside of the breast outline before you mask with the circle and crop the circle out of the image.
Actually I don't know why you don't just analyze the image as-is. What is to be gained by circling them and cropping them out. It's not needed.
  4 Comments
Beren Ac
Beren Ac on 5 Nov 2021
Thaks for your help but this mask didn't get properly breast. If it works, there is no need for circle fitting and cropping image. I used circle fitting because i couldn't get breast with xr,xl,yl and yr coordinates. But I have to seperate it for bileteral asymmerty analysis as left and right. Maybe i can do it by using middle line. @Image Analyst

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!