How to remove image margins
Show older comments
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?

Accepted Answer
More Answers (1)
KSSV
on 30 Mar 2021
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,:) ;
2 Comments
I = imread('image.jpeg') ;
I1 = rgb2gray(I) ;
[y,x] = find(I1==0) ;
x0 = min(x) ; x1 = max(x) ;
y0 = min(y) ; y1 = max(y) ;
I = I(y0:y1,x0:x1,:) ;
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!