0

Attached is an image of cameraman.tif that comes with matlab but after applying [fuzzy c-means] 1

enter image description here

Also, as an output from the algorithm is u2 (degree of membership of a pixel). See matlab.mat for this data.

What I want to do is select the pixel(s) from the image attached whose u2 value is equal to 1.

Any ideas on how this can be done in matlab?

Thanks.

Shai
  • 111,146
  • 38
  • 238
  • 371
Simplicity
  • 47,404
  • 98
  • 256
  • 385

2 Answers2

3

Get the indices of the appropriate pixels:

ind = find( u2 == 1 ); % return indices of all pixels with u2 eq to 1

Get the pixels themselfs

pixels = img( ind );
Shai
  • 111,146
  • 38
  • 238
  • 371
  • When I do this for `u2`, I get: `ind = Empty matrix: 0-by-1` although there are values equal to `1`. Why is that? Thanks – Simplicity Feb 20 '13 at 13:32
  • 1
    This might be due to numerical issues. How about `find( abs(u2-1) < 1e-6 )`? – Shai Feb 20 '13 at 13:37
  • 1
    When I checked `matlab.mat`, when you look at the cell value it says `1.0000`. But, when I double-click the cell I amazingly get: `0.999999999259113`! That seems why I get an empty matrix as a return value... – Simplicity Feb 20 '13 at 13:44
  • Your solution gives the results of from `u2`. But, I want to map that to the pixels in the original image. How can I get the pixels from the original image whose `u2 -- 1`? Thanks – Simplicity Feb 20 '13 at 13:47
  • 1
    @Med-SWEng what is the shape (`size`) of `u2`? is it the same as that of the image? if so - the indices are the same. You may get row/column locations using `[rows cols] = find( abs(u2-1)<1e-6 );` – Shai Feb 20 '13 at 13:49
  • @shai.Yes, `u2` has the same size as image – Simplicity Feb 20 '13 at 13:57
  • Sorry, what do you mean by `abs(u2-1)<1e-6 `? Thanks – Simplicity Feb 20 '13 at 13:58
  • Si, when I use `[rows cols] = find(u2 == 1)` for instance, and I get the locations of those pixels from `u2` equal to `1`. In my output, how would I return those locations from the original image? – Simplicity Feb 20 '13 at 14:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24832/discussion-between-shai-and-med-sweng) – Shai Feb 20 '13 at 14:01
2

Alternatively, you can avoid using find and go straight into logical indexing.

Given an image as:

>> image = [1 3 5; 2 3 1; 3 2 2]

image =

     1     3     5
     2     3     1
     3     2     2

you can find the pixels with value 2 using the condition iamge == 2:

K>> image == 2

ans =

     0     0     0
     1     0     0
     0     1     1

If for instance you want to manipulate these pixels by setting them to 7 you can easily do that with a single line:

image(image == 2) = 7

image =

     1     3     5
     7     3     1
     3     7     7
fuyas
  • 129
  • 1
  • 9