سؤال

I was reading the documentation of regionprops and found out that if the image being labeled is grayscale, regionprops will find the PixelValues as well which is defined as:

'PixelValues' — p-by-1 vector, where p is the number of pixels in the region. Each element in the vector contains the value of a pixel in the region.

Is the pixel value the grayscale value of the pixel? Like between 0 and 255? also is it safe to assume that the region mentioned here being the labeled connected component obtained using bwlabel.

So I wanted to see the pixel value of each pixel (I knew it would be a mesh and not clear but with the help of zoom I would be able to read the values) But the code I used produced a weird result. As in the results are in it did not show clear boxes around each area showing the pixel value (like it did with stats.Area). This is what I got:

enter image description here

Why are the text boxes so long? and why does it contain decimals? When I type stats(1).PixelValues I get a [2159x1] vector like so

ans =

  125
  128
  130
  126
  137
  101
  131
   28

Here is my code

img = rgb2gray(imread('W1\Writer1_01_02.jpg'));
bw_normal = im2bw(img, graythresh(img));
bw = imcomplement(bw_normal);
[label,n] = bwlabel(bw);
stats = regionprops(label, img, {'Area', 'Centroid', 'BoundingBox', 'PixelValues'});

imshow(img);

%To display text box on CC

for j = 1:numel(stats)
         text(stats(j).Centroid(1),stats(j).Centroid(2), ...
        sprintf('%2.1f', stats(j).PixelValues), ...
        'EdgeColor','b','Color','r');
     end
هل كانت مفيدة؟

المحلول

You ask several questions here. I'll try and answer.

  1. When asking for 'PixelValues' you get a vector with the gray-scale values of the pixels in input img corresponding to the labeled region in label. That is, stats(ii).PixelValues is a vector with number of elements equal to the number of pixels in the ii-th region. The values of this vector is the gray-scale values taken from the corresponding pixels in img.

  2. The range of values in 'PixelValues' depends on the input img. If the values of img are between 0..255 so will the values be. If you converted img to double (e.g., using im2double) the values will rang ein 0..1

  3. Your text boxes are long because they show you ALL pixel values of each region (values of MANY pixels). They contain decimal because this is the way you printed them with '%2.1f'. Use '%d' to print integer values. You might also want to add a space there.

  4. If all you want is to inspect the pixel values, you migt want to try tool like impixelinfo.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top