How to remove image margins

12 visualizaciones (últimos 30 días)
lech king
lech king el 30 de Mzo. de 2021
Editada: Esen Ozbay el 31 de Mzo. de 2021
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?

Respuesta aceptada

Esen Ozbay
Esen Ozbay el 30 de Mzo. de 2021
Editada: Esen Ozbay el 30 de Mzo. de 2021
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 comentarios
Esen Ozbay
Esen Ozbay el 31 de Mzo. de 2021
Editada: Esen Ozbay el 31 de Mzo. de 2021
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!
lech king
lech king el 31 de Mzo. de 2021
It was really great
Thank you very much for your kindness
This is exactly what I was looking for

Iniciar sesión para comentar.

Más respuestas (1)

KSSV
KSSV el 30 de Mzo. de 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 comentarios
lech king
lech king el 30 de Mzo. de 2021
Editada: lech king el 30 de Mzo. de 2021
thank you
But if it was just a number of photos, it would be possible, but the problem is that I intend to do this for 20,000 photos, in which case the location of the margins is different.
Basically the problem is that first we have to identify the location of the extra margins (it is different in each photo) and second we have to remove them
KSSV
KSSV el 30 de Mzo. de 2021
Editada: KSSV el 30 de Mzo. de 2021
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,:) ;

Iniciar sesión para comentar.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by