How to extract tumour from bounding box and calculate its area?

3 次查看(过去 30 天)
My project is to segment brain tumour using bounding box method. As a part of post-processing steps, I want to extract tumour inside the bounding box and calculate its area. I don't know how to proceed further. Coding up to forming bounding box and the input image is attached. Please help me with coding to extract tumour inside the box and calculate its area.
function initial I = double(imread('image1.bmp'));
I(:) = (I - min(I(:)))*255/(max(I(:)) - min(I(:)));
[M,I]=skull_detect(I); figure, imagesc(M), colormap(gray), axis image; drawnow;
h = size(I,1); STATS = regionprops(M,'all'); midx = round(STATS.Centroid(1)); M = logical(M);
subplot(2,2,1); imagesc(I),colormap(gray),axis image, title('MR Image'); drawnow; figure(1), subplot(2,2,2); imagesc(I), colormap(gray), axis image; drawnow; hold on, plot([midx midx],[1 h], 'linewidth', 2); drawnow; [b_x,b_y] = find(bwperim(M)== 1); hold on, plot(b_y,b_x, '.c'); drawnow;
Im = I(:,midx:-1:1); ImMask = M(:,midx:-1:1); RefI = I(:,midx:end); RefIMask = M(:,midx:end);
starti=round(STATS.BoundingBox(2)); endi=round(STATS.BoundingBox(2) + STATS.BoundingBox(4));
fact = 16; BC_diff_TD = score(Im,RefI,ImMask,RefIMask,starti,endi,fact);
figure(1), subplot(2,2,3),plot(starti:endi,BC_diff_TD); title('Score plot for vertical direction'); set(gcf, 'color', [1 1 1]);
vert_scale = 30; [topy1, downy1]= find_largest_decreasing_segment(BC_diff_TD,vert_scale);
topy = topy1(1); downy = downy1(1);
subplot(2,2,3), hold on; plot(topy+ starti-1,BC_diff_TD(topy), 'r.',downy+ starti-1,BC_diff_TD(downy),'m.','MarkerSize',10);
topy = topy + starti-1; downy = downy + starti-1; figure(1), hold on,
Im = (Im(topy:downy,:))'; ImMask = (ImMask(topy:downy,:))'; RefI = (RefI(topy:downy,:))'; RefIMask = (RefIMask(topy:downy,:))';
startj=1; endj=floor(min(STATS.BoundingBox(1) + STATS.BoundingBox(3)-midx+1, midx - STATS.BoundingBox(1)+1));
BC_diff_LR = score(Im,RefI,ImMask,RefIMask,startj,endj,fact); horz_scale = 30; [leftx1, rightx1]= find_largest_decreasing_segment(BC_diff_LR,horz_scale);
leftx = leftx1(1); rightx = rightx1(1); leftx2 = leftx1(1); rightx2 = rightx1(1); leftx = leftx + midx + startj-1; rightx = rightx + midx+ startj-1; m_right = mean2(I(topy:downy,leftx:rightx)); m_left = mean2(I(topy:downy,2* midx - rightx:2* midx - leftx)); isleft = 0; if m_left>m_right, leftx1 = 2* midx - rightx; rightx1 = 2* midx - leftx; leftx = leftx1; rightx = rightx1; isleft = 1; end if isleft == 1, figure(1), subplot(2,2,4),plot(midx - endj:midx - startj,-BC_diff_LR(end:-1:1),'r'); subplot(2,2,4), hold on; plot(rightx,-BC_diff_LR(leftx2),'y.',leftx,-BC_diff_LR(rightx2),'c.'); else figure(1), subplot(2,2,4),plot(midx+startj:midx+endj,BC_diff_LR,'r'); subplot(2,2,4), hold on; plot(leftx,BC_diff_LR(leftx2),'c.',rightx,BC_diff_LR(rightx2),'y.'); end title('Score plot for horizontal direction'); set(gcf, 'color', [1 1 1]);
figure(1),subplot(2,2,1), hold on; plot([leftx rightx],[topy, topy],'r'); plot([leftx rightx],[downy, downy],'g'); plot([leftx, leftx],[topy downy],'c'); plot([rightx, rightx],[topy downy],'y');
function [M,I]=skull_detect(I)
I(1:end,1)=0; I(1:end,end)=0; I(1,1:end)=0; I(end,1:end)=0; J = imfill(I,'holes');
K = im2bw(J/max(J(:)), 0.3* graythresh(J/max(J(:))));
[L,N] = bwlabel(K); maxa = 0; maxi=0; for i=1:N, a = sum(sum(L==i)); if a>maxa, maxa=a; maxi=i; end end L = double((L==maxi)); figure,imagesc(L),colormap(gray);axis image; drawnow;
STATS = regionprops(L,'all'); STATS.Centroid; x0 = round(STATS.Centroid(1)); y0 = round(STATS.Centroid(2));
[h,w] = size(I); temp = I(y0-min(y0,h-y0)+1:y0+min(y0,h-y0),x0-min(x0,w-x0)+1:x0+min(x0,w-x0)); clear I; I = temp; clear temp; temp = L(y0-min(y0,h-y0)+1:y0+min(y0,h-y0),x0-min(x0,w-x0)+1:x0+min(x0,w-x0)); L = temp; clear temp;
STATS.Orientation; if STATS.Orientation<0, M = imrotate(L,-90-STATS.Orientation); I = imrotate(I,-90-STATS.Orientation); else M = imrotate(L,90-STATS.Orientation); I = imrotate(I,90-STATS.Orientation); end close all;
function BC_diff_TD = score(Im,RefI,ImMask,RefIMask,starti,endi,fact)
BC_diff_TD = zeros(endi-starti+1,1);
minval = max(min(Im(:)),min(RefI(:))); maxval = min(max(Im(:)),max(RefI(:))); offset=15; xbins = (minval:fact:maxval); for i = starti:endi,
Tmp = Im(1:i,:);
H_leftTop = hist(Tmp(ImMask(1:i,:)),xbins);
clear Tmp;
Tmp = RefI(1:i,:);
H_rightTop = hist(Tmp(RefIMask(1:i,:)),xbins);
clear Tmp;
Tmp = Im(i:end,:);
H_leftBottom = hist(Tmp(ImMask(i:end,:)),xbins);
clear Tmp;
Tmp = RefI(i:end,:);
H_rightBottom = hist(Tmp(RefIMask(i:end,:)),xbins);
clear Tmp;
H_leftTop = H_leftTop / (sum(H_leftTop)+eps);
H_rightTop = H_rightTop / (sum(H_rightTop)+eps);
H_leftBottom = H_leftBottom / (sum(H_leftBottom)+eps);
H_rightBottom = H_rightBottom / (sum(H_rightBottom)+eps);
BC_Top = sum(sqrt(H_leftTop .* H_rightTop));
BC_Bottom = sum(sqrt(H_leftBottom .* H_rightBottom));
% compute difference of BCs
if i<=starti+offset,
BC_diff_TD(i-starti+1) = -BC_Bottom;
if i==starti+offset,
BC_diff_TD(1:i-starti+1) = BC_diff_TD(1:i-starti+1) + BC_Top;
end
elseif i>=endi-offset,
if i==endi-offset,
to_subs = BC_Bottom;
end
BC_diff_TD(i-starti+1) = BC_Top-to_subs;
else
BC_diff_TD(i-starti+1) = BC_Top-BC_Bottom;
end
end
function [from, to]= find_largest_decreasing_segment(score,scale)
hf_scale=round(scale/2);
ext_score = [ones(hf_scale,1)*score(1); score(:); ones(hf_scale,1)*score(end)]; N = length(score); reg_minmax = zeros(N,1);
for n=1:N, if min(ext_score(n:n+2*hf_scale))==score(n), reg_minmax(n)=-1; elseif max(ext_score(n:n+2*hf_scale))==score(n), reg_minmax(n)=1; end end
n=1; count = 0; while n <N-1; while reg_minmax(n)<1 && n<N-1, n = n + 1; end m=n; n = n+1; while reg_minmax(n)==0 && n<N, n=n+1; end if reg_minmax(n)==-1 count = count + 1; thisarea(count) = 0.5*(score(m)-score(n))*(n-m); from(count)=m; to(count)=n; end end [thisarea,ind] = sort(thisarea,'descend'); from(:) = from(ind); to(:) = to(ind);
  2 个评论
John BG
John BG 2016-2-10
1.- do you really want to work with bitmaps, or you don't really mind using RGB .jpg images?
2.- Is is possible to use a reference 'slice' to compare and tell what tissue is damaged from the one that is not?
mounika siripurapu
mounika siripurapu 2016-2-10
I wanted to work with bitmaps only. There is no need to use a reference slice for this method. In the same image we can use one half as reference and other half as test image. To perform post processing functions, I need to save the output image with bounding box. I tried imsave() and also imwrite(). But the image with bounding box was not saved.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2016-2-10
I don't know what "with the bounding box" means in your comment above, but if you want only the bounding box you need to call imcrop() first. If you want the bounding box in the overlay over the image, you have to call plot() to put it there, then call export_fig() to save the image with box in the overlay as an RGB image.
  3 个评论

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by