Delete a specific pixels of an image

3 views (last 30 days)
I need to delete a specific pixel of an image, I have its coordinates, x and y, but I do not know how to delete only that one pixel.
My code is this:
B6 = imread('B6.TIF');
imshow(B6);
[x, y] = ginput(1);

Accepted Answer

Nada Kamona
Nada Kamona on 8 Oct 2017
Hi David,
I'm not sure what you mean by delete just that pixel. So I'm assuming you only want to set that pixel at value equal to zero or NaN, while keeping the image at the same size. If this is what you want, and assuming x and y are the location of the pixel, then you can simply do the following:
B6(x,y) = 0;
% Or ...
B6(x,y) = NaN;
Please note that if x and y are arrays of coordinates, the above code still works. It will set all the points to zeros/NaNs.
Hope this helps!
  2 Comments
Image Analyst
Image Analyst on 8 Oct 2017
Note: for a uint8 or uint16 image, setting to nan will set it to 0, not nan. And you reversed x and y. You'd need to have
B(ceil(y), ceil(x)) = 0; % Round and put y first, NOT x.

Sign in to comment.

More Answers (1)

Community Treasure Hunt

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

Start Hunting!