Find color patches in a polygon format in matlab

1 回表示 (過去 30 日間)
Muhammad Umar
Muhammad Umar 2018 年 10 月 12 日
回答済み: Himanshu 2024 年 12 月 10 日
I am using following code to get patches of a specific image.
for i=1:100 %this lope executes 100 times if your execution breaks you can resume the execution by cahnging the value of i here.
imshow(Image); hold on; title(i);
[temp_imag,y,x]= roipoly(Image);
Nuclear_Image=Nuclear_Image+temp_imag;
However easier/faster way is to make polygons using photoshop, color them with. let's say red color. Now back in matlab, how can I get these red patches like a polygon as above

回答 (1 件)

Himanshu
Himanshu 2024 年 12 月 10 日
Hey,
To extract red patches from an image in MATLAB, you can employ color thresholding to identify and isolate the red regions. You can try out the following steps to achieve the same:
  1. Load your image into MATLAB (ensure the image is in RGB format)
  2. Threshold the Red Channel: Identify the red regions by setting a threshold on the red channel.
  3. Generate a binary mask where the red regions are identified.
  4. Use the binary mask to extract the red patches.
The code should look something like this:
% Read the image
image = imread('your_image.png'); % Replace with your image file
% Define thresholds for the red channel
redThreshold = 150; % Adjust this value based on your image
% Extract the red channel
redChannel = image(:,:,1);
% Create a binary mask for red regions
redMask = redChannel > redThreshold;
% Display the binary mask
figure;
imshow(redMask);
title('Red Regions Mask');
% Extract red patches as polygons
[B, L] = bwboundaries(redMask, 'noholes');
% Display the boundaries on the original image
figure;
imshow(image);
hold on;
title('Detected Red Patches');
% Loop through each boundary and plot
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'g', 'LineWidth', 2);
end
hold off;
Hope this helps!

Community Treasure Hunt

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

Start Hunting!

Translated by