How to remove image margins
조회 수: 2 (최근 30일)
이전 댓글 표시
After extracting the required parts from a CT scan, I also intend to remove the extra margins of the image (black parts around the lungs)
I understand that I can add the contents of rows or columns one by one with command for and delete the first non-zero row or column, but it seems that using command for has a high computational load. Do you have any other suggestion to solve this problem?
댓글 수: 0
채택된 답변
Esen Ozbay
2021년 3월 30일
편집: Esen Ozbay
2021년 3월 30일
Let your array be called CTscan, and be a 1024x1024 matrix.
If you know how many columns you want to delete (let's say 21), you can do this to delete the first 21 columns:
CTscan(:, 1:21) = [];
Then, you will have a 1024x1003 matrix.
You can also do the same for rows. To delete the first 23 rows of CTscan:
CTscan(1:23, :) = [];
Then, you will have a 1001x1003 matrix.
To delete the last 21 columns:
CTscan(:, end-20:end) = [];
댓글 수: 3
Esen Ozbay
2021년 3월 31일
편집: Esen Ozbay
2021년 3월 31일
To detect the location of the margins automatically, I can suggest the following:
% Delete zero columns (leave a margin of 10 elements)
temp = sum(CTScan>0, 1); % Find the number of nonzero elements in each column
nonZeroColumnIndices = find(temp>0); % Find columns that have at least 1 nonzero element
CTScan = CTScan(:, nonZeroColumnIndices(1)-10:nonZeroColumnIndices(end)+10);
% Delete zero rows (leave a margin of 10 elements)
temp = sum(CTScan>0, 2);
nonZeroRowIndices = find(temp>0);
CTScan = CTScan(nonZeroRowIndices(1)-10:nonZeroRowIndices(end)+10,:);
Hope this helps!
추가 답변 (1개)
KSSV
2021년 3월 30일
Let I be your image and say you want to extract image from row0 to row1 and col0 to col1..
I_extract = I(row0:row1,col0:col1,:) ;
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!