How to crop an ultrasonography to remove annotations and the black portions?

조회 수: 2 (최근 30일)
Ostache Andrei
Ostache Andrei 2024년 3월 14일
댓글: DGM 2024년 3월 18일
I have a dataset of ~1350 images with the size 646x618x3.
I dont't know how to crop the image as good as possible in an automatic manner.
I thaught of removing each line untill i have ~0 pixel value but it does not work.
I tried to crop by trying differet velues but some images are shifted/ translated so it does not work a single cropping value for all.
I just need an ideea to implement the code, or some lines if possible!
Thank you!
  댓글 수: 1
DGM
DGM 2024년 3월 18일
Unless you post images, nobody knows what you're actually trying to do. All we know is the size of the image and that you want to crop or fill parts of it. We know there are inconsistencies in the images, but we don't know what they are.
Attach representative images -- enough to describe the portions you want to keep, the portions you want to remove, and how the positions vary between images.
Here is perhaps one tangentially related answer:
... though that's probably not super helpful.

댓글을 달려면 로그인하십시오.

답변 (1개)

Udit06
Udit06 2024년 3월 18일
편집: Udit06 2024년 3월 18일
Hi Ostache,
You can use edge detection or thresholding to identify the content boundaries and then crop accordingly. This method can adapt to variations in content positioning and size across image. Here is the code implementation for the same.
A = imread("saturn.png");
imshow(A)
grayImg = rgb2gray(A);
edges = edge(grayImg, 'Canny');
% For edge-detected image, you might need to fill in gaps first
filledImg = imfill(edges, 'holes');
% Find connected components
props = regionprops(filledImg, 'BoundingBox');
% Assuming the largest connected component is your content
[~, indexOfMax] = max(arrayfun(@(x) prod(x.BoundingBox(3:4)), props));
boundingBox = round(props(indexOfMax).BoundingBox);
croppedImg = imcrop(A, boundingBox);
imshow(croppedImg)
You can similarly use Otsu's thresholding method, instead of edge detection to find the main region of interest and crop accordingly.
Refer to the following MathWorks documentation to understand more about the functions used in the above code snippet.
I hope this helps!
If this approach does not works with the images that you have, it would be better if you could upload a sample image with this question. It would help others to respond to your query in a better way.

Community Treasure Hunt

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

Start Hunting!

Translated by