How to detect the areas within the curves and remove them from the image?

1 次查看(过去 30 天)
I would like to detect the the curves in the attached images and remove the enclosed area from the curve, so that, only the tiny white spots remain in the images. I took an attempt to do the job and come up with the first attachment (detected the curve). I could not remove the marked from the image. Your suggestions is highly appreciated.

采纳的回答

Shubh
Shubh 2023-12-28
编辑:Shubh 2023-12-28
Hi,
I understand that you want to remove the marked spots from the image.
You can follow these steps:
  1. Read the images using "imread".
  2. If the images are not already in grayscale, convert them using "rgb2gray".
  3. Apply a binary threshold using "imbinarize" with Otsu's method.
  4. Remove noise by filtering out small objects with "bwareaopen".
  5. Label the connected components with bwlabel or "bwconncomp".
  6. Identify the largest connected component which is likely to be the curve.
  7. Remove the largest connected component from the binary image.
  8. Display the results using "imshow".
Here's the MATLAB code for processing the images:
% Read the images
image1 = imread('path_to_your_first_image.jpg');
image2 = imread('path_to_your_second_image.jpg');
% Convert images to grayscale if they are in RGB
if size(image1, 3) == 3
image1 = rgb2gray(image1);
end
if size(image2, 3) == 3
image2 = rgb2gray(image2);
end
% Binarize images using Otsu's threshold
bw1 = imbinarize(image1);
bw2 = imbinarize(image2);
% Remove small objects (noise)
cleaned1 = bwareaopen(bw1, 50);
cleaned2 = bwareaopen(bw2, 50);
% Label connected components
[label1, num1] = bwlabel(cleaned1);
[label2, num2] = bwlabel(cleaned2);
% Find the largest component for each image
stats1 = regionprops(label1, 'Area');
stats2 = regionprops(label2, 'Area');
[~, largestIdx1] = max([stats1.Area]);
[~, largestIdx2] = max([stats2.Area]);
% Remove the largest component from the images
final1 = cleaned1;
final2 = cleaned2;
final1(label1 == largestIdx1) = 0;
final2(label2 == largestIdx2) = 0;
% Display the results
figure;
subplot(2,2,1), imshow(final1), title('Final Image 1');
subplot(2,2,2), imshow(final2), title('Final Image 2');
Hope this helps!
  1 个评论
Debasish Sarker
Debasish Sarker 2024-1-11
Sorry for late response. Yes, it really helps. Thank you very much for the algorithm. I was actually trying to adopt the algorithm for other frames. It successfully keeps the white spots in the images. However, I also need to know the area within the marked spots or area enclosed by the curved line. I have attached a couple of images, if you can help me with them.

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by