Color in white only pixels defined by a matrix [rx2]

2 views (last 30 days)
Hi! I would like to keep white pixels only at the desired coordinates ("contour") while transforming other pixels with black color. Is there an easy way to be able to do this?
BW = imread('blobs.png');
imshow(BW)
r1 = 163;
c1 = 37;
contour = bwtraceboundary(BW,[r1 c1],'W');
hold on
plot(contour(:,2),contour(:,1),'g','LineWidth',2)

Accepted Answer

Voss
Voss on 31 Jan 2023
BW = imread('blobs.png');
imshow(BW)
r1 = 163;
c1 = 37;
contour = bwtraceboundary(BW,[r1 c1],'W');
hold on
plot(contour(:,2),contour(:,1),'g','LineWidth',2)
figure
BW_new = BW; % make a copy of BW
BW_new(:) = 0; % set all pixels to black
BW_new(sub2ind(size(BW),contour(:,1),contour(:,2))) = 1; % set "contour" pixels to white
imshow(BW_new)

More Answers (0)

Categories

Find more on Contour Plots in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!