ADI
ADI

Reputation: 91

Need help regarding plotting the pixel from binary images to a sparse image

I have a binary image and I want to plot only the foreground pixels into a sparse image using MATLAB. I know that the pixel location of the foreground image (which is white) has to be known. For that I following code :

close all
clear all
original = imread('/gt_frame_2.bmp');    
fig1 = figure(1)
[x y]=find([original])

The input used here appears as follows:

Binary image

And the expected sparse image will be something as like this: Sparse image

The above image in generated through GIMP. Just to give an idea of how a sparse image will look like. It contains pixels of the foreground image. But, not all the pixels are plotted.

After finding the X Y coordinates of the pixels I need to plot the same on to a sparse image. This where I am lost, I don't know how to plot a sparse image. Any kind of help is appreciated.

Upvotes: 0

Views: 117

Answers (1)

nkjt
nkjt

Reputation: 7817

You don't need the x-y, if you just want to pick some subset of pixels, linear indexing is easier. The syntax 1:step:end is a very useful way of picking out a subset of a vector or matrix. It works even if the size of the matrix is not an exact multiple of your step, e.g. check out at the command line what this does:

n = 1:5 
n(1:3:end)

So here's a very basic way of doing it:

n = find(I); % list of non-zeros
I2 = zeros(size(I)); % blank image of same size
I2(n(1:10:end))=1; % every tenth pixel

We could get more complicated (think this requires image processing toolbox). If there's only one, solid object, use bwperim to return just the boundary, then fill with a random subset of pixels:

I2 = bwperim(I);
% randperm for sampling without replacement
n2 = randperm(numel(n),floor(numel(n)/10);
I2(n(n2))=1;

Upvotes: 1

Related Questions