0

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.

ADI
  • 91
  • 1
  • 4
  • 13
  • 1
    What is a sparse image accroding to you ? Please be more specific. – Ratbert Feb 18 '15 at 10:22
  • i have added an example of the sparse image. A image containing pixels on the foreground image. But, not all the pixels are plotted. – ADI Feb 18 '15 at 10:52
  • Ok thanks for the image. But it is still unclear to me what a sparse image is. Without a proper definition, there is not much we can do. – Ratbert Feb 18 '15 at 11:14
  • 1
    Do you just want to plot the boundary + some internal points? Or just a random subset (like every tenth point), which would be easy. Somebody told you to make a "sparse image", I presume ,maybe ask them what the requirements are. – nkjt Feb 18 '15 at 12:02
  • @nkjt: Yes, i want to plot a random subset which would resemble the original image. Any suggestions? – ADI Feb 18 '15 at 12:08

1 Answers1

1

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;
nkjt
  • 7,825
  • 9
  • 22
  • 28
  • Thank for the information. But, how do I get the the random subset pixels to be displayed? I know this is a silly question but i'm new to MATLAB. – ADI Feb 18 '15 at 13:08
  • I figured it out how to display the images. Thanks again – ADI Feb 18 '15 at 14:05