How to extract particular pixel values from an image?
Show older comments
Hello,
I'm doing project on image processing, and in one of my module i need to extract the particular bunch of the pixels.I know the values of that pixel but i'm not getting how exactly to extract that values. so please help me to get out of this!
2 Comments
Walter Roberson
on 24 Dec 2012
What do you mean by "extract" in this case?
Are the pixels RGB or grayscale?
Nikhil
on 26 Dec 2012
Answers (4)
Walter Roberson
on 24 Dec 2012
[PixelRows, PixelColumns] = find( YourImage(:,:,1) == RedValue & YourImage(:,:,2) == GreenValue & YourImage(:,:,3) == BlueVaue );
Nikhil
on 24 Dec 2012
0 votes
7 Comments
Walter Roberson
on 24 Dec 2012
Then what do you mean when you say that you need to extract a bunch of pixels? If you know the locations then just
YourImage(Row, Column)
and if you have a list of row and column pairs then
YourImage(sub2idx(size(YourImage), rows, columns)
Nikhil
on 26 Dec 2012
Nikhil
on 26 Dec 2012
Image Analyst
on 26 Dec 2012
Like I said, post your image.
Nikhil
on 26 Dec 2012
Kris
on 2 Jan 2013
Dear Nikhil, Are you sure those are the images or they are just for example??...if so, please share the original image you are working on through flickr.com or picasa, tiny pic or any other photo sharing site...
Also, I feel that the labeling of image will be helpful for you..try image labeling once you label the regions of your image, it will be relatively easy to extract them...try once this might be helpful...
Nikhil
on 2 Jan 2013
Image Analyst
on 24 Dec 2012
0 votes
You say
- You have a binary image (which means value of 0 and 1)
- You know the values in the particular bunch (either 0, 1, or both of course)
- You need to extract their values (which will again be either 0 or 1 or both)
So the question remains,
- Do you know the locations of the "particular bunch" of pixels?
- If you already know their values then what exactly does it mean that you need to extract their values? You mean you want a vector that is the length of the "particular bunch" and has the 0 or 1 values in the vector? Like your image is 1 megapixel but you want a vector of pixel values from a small 1000 pixel chunk in the image?
41 Comments
Nikhil
on 26 Dec 2012
Image Analyst
on 26 Dec 2012
This doesn't answer my questions. You haven't really said what "extract" means to you. I can tell you now that the value of your binary object is 1 at every pixel, but I don't think that's what you want. The position of every "1" pixel is available from regionprops but that is not really a final answer and again, not what I think you really want or need. It's time for you to post your image so we can discuss it further. http://www.mathworks.com/matlabcentral/answers/7924-where-can-i-upload-images-and-files-for-use-on-matlab-answers
Nikhil
on 26 Dec 2012
Walter Roberson
on 26 Dec 2012
If you need to know the positions of the 1, then
find(TheBinaryImage)
Nikhil
on 26 Dec 2012
Walter Roberson
on 26 Dec 2012
The result of find() will tell you where each individual pixel of the object is. If you prefer you can use
[r, c] = find(TheBinaryImage);
and then pixel #K is at r(K), c(K)
Might I suggest you examine the documentation for regionprops() ?
Nikhil
on 26 Dec 2012
Walter Roberson
on 26 Dec 2012
regionprops() will not do any cropping. regionprops() is for returning information about the image.
Image Analyst
on 26 Dec 2012
Edited: Image Analyst
on 26 Dec 2012
Nikhil, you are asking the wrong questions. You don't need to find out the values of where the image is 1 (white) or their values. LIke we've been telling you, the values will all be 1, and the image itself tells you where those "1" pixels are, or if you want them in (row, column) format ( which you definitely DON'T), then you can use the find() function. But you're asking the wrong questions - questions that will get you useless stuff you don't need, because you already have the information or know it.
What you should be asking is "How can I measure X, Y, Z, etc." and the answer to that is to use regionprops(). Walter and I can't stress this enough. For example, how can I measure area, solidity, Euler number, perimeter length, etc. And another useless question is "how can I crop it?" - that doesn't matter because you don't need to. There is no need to crop those images on your web page. It's just not necessary to make the measurements of area, etc. You can make those measurements perfectly well without cropping, without knowing that the pixel values (which we've discussed are all 1 anyway), and without knowing the rows and column of every white pixel. Please believe me. I do this all day long, every day for a living. Please review my demo: http://www.mathworks.com/matlabcentral/fileexchange/25157-image-segmentation-tutorial-blobsdemo. It should explain everything.
Walter Roberson
on 26 Dec 2012
Suppose, Nikhil, that you had the 3 x 3 image
000
010
111
then what output would you be hoping to get for the "location" of the 1's in this image?
Nikhil
on 27 Dec 2012
Nikhil
on 27 Dec 2012
Walter Roberson
on 27 Dec 2012
It would help us if you were to respond to my earlier question. Repeating it,
Suppose, Nikhil, that you had the 3 x 3 image
000
010
111
then what output would you be hoping to get for the "location" (or shape) of the 1's in this image?
Nikhil
on 27 Dec 2012
Edited: Walter Roberson
on 27 Dec 2012
Walter Roberson
on 27 Dec 2012
Show us what you would want the output to be. A specific output. For example are you looking for the output "triangle", or are you looking for the output [3, 5, 6, 9] which is the linear indices, or are you looking for the output [3 2 3 3] [1 2 3 3] which is row values and column values, or are you looking for a shape descriptor such as (-1,1),(1,0),(0,1) which represents the (r,c) offsets needed to trace a path through from the lower-left corner, or ... ?
Nikhil
on 27 Dec 2012
Image Analyst
on 27 Dec 2012
Edited: Image Analyst
on 27 Dec 2012
Wow, this is like pulling teeth. You can't have a matrix with nothing, like a dash that you showed. A matrix is a 2D array of numbers. SOMETHING has to be there, such as zeros, in which case the original matrix is what you want. If you don't want the zeros, then do you want this: [1 1 1 1] instead? That is a linear array, because, remember, you can't have a 2D numerical array with dashes instead of a number.
Walter Roberson
on 27 Dec 2012
So what you are asking for is that the shape somehow be recognized and an identification of the shape be returned? If so then that is a very different task than "extracting pixel values from the image", and is a lot more work. You would need to do "feature extraction" and match those extracted features against the features of the known database items, finding the closest match in the database.
Nikhil
on 28 Dec 2012
Nikhil
on 28 Dec 2012
Walter Roberson
on 28 Dec 2012
Go back to the 3 x 3 example I gave above. What is the exact values you would want to output for that example? It was
000
010
111
Show the exact output you would want from that.
Nikhil
on 29 Dec 2012
Image Analyst
on 29 Dec 2012
Edited: Image Analyst
on 29 Dec 2012
Aaaaaaand, we come full circle.
I think I'll bail out now, unless Nikhil can answer the question properly, like he wants [1 1 1 1] or [0 0 0;0 1 0; 1 1 1], and how either of those could possibly be useful in any way at all to him.
Walter Roberson
on 29 Dec 2012
Do you mean you need the character string 'triangle' as the output ?
Nikhil
on 30 Dec 2012
Walter Roberson
on 30 Dec 2012
Are you asking how to crop the image to its bounding box ? I am referring back here to "one of the feature is to extract the part where object is present in the image". That probably does not mean "just the 1's", that probably means that to trim down to the bounding box. For example in the example
000
010
111
the top row is just background, so the "interesting" part of the figure is
010
111
and that is probably what is being asked for.
Nikhil
on 31 Dec 2012
Walter Roberson
on 31 Dec 2012
So you want the "1" and the "1 1 1" together, right? And what data representation do you want for that? An array like
[1]
[1 1 1]
with the [1] "over" the center "1" because it came from the center? Or do you want
[1]
[1 1 1]
with it not being relevant that the [1] came from the center?
Or do you want
[1 1 1 1]
Or do you want the coordinates of the 1's, such as [2 2] [3 1] [3 2] [3 3] ?
Nikhil
on 31 Dec 2012
Walter Roberson
on 31 Dec 2012
Edited: Walter Roberson
on 31 Dec 2012
You want the triangle drawn with no background? Or you want it to return
[- - -]
[- 1 -]
[1 1 1]
except with nothing at all where the "-" are?
Or you want it to return the string 'triangle' ?
Or you want it to return a reference to the triangle shape in the database, having looked in the database and figured out that the triangle shape there was the best match? Perhaps you are wanting to return the name of the file in the database that most closely matches the input of
[0 0 0]
[0 1 0]
[1 1 1]
??
Nikhil
on 31 Dec 2012
Walter Roberson
on 31 Dec 2012
Okay, we can draw it, but what then? Are you going to work on the drawing of the triangle somehow?
Here is the drawing:
imagesc(YourImageData, 'Alphadata', YourImageData)
colormap(gray)
Image Analyst
on 31 Dec 2012
This is becoming very entertaining. You're a real trooper Walter. A persistent one too. By the way, you should probably start your own answer so you get the credit, since I've bailed out long ago and am merely chuckling on the sidelines now.
Walter Roberson
on 1 Jan 2013
Feature extraction from a drawing of the shape is considerably more tedious than feature extraction from the binary matrix.
Nikhil
on 2 Jan 2013
Walter Roberson
on 2 Jan 2013
What you are seeing as background is the axes background. You can set the axes 'color' property to 'none'
set(gca, 'color', 'none')
Nikhil
on 2 Jan 2013
Walter Roberson
on 2 Jan 2013
What is the name of your black-and-white image array?
Nikhil
on 3 Jan 2013
Anuradhi Umayangani
on 14 Sep 2016
0 votes
Hi, I have a matrix with only 0's & 1 's .I actually it is a square devided in to 9 equal squares some are coloured in black and some quares are white.this master square is printed in a white background.it has a black outline.I want to crop the square from the white background.do u know a way to do that. i need to write the program to identify the margine by itself
3 Comments
Image Analyst
on 14 Sep 2016
Use imcrop(). Or else indexing. Post your image in a new question if you want more help, since Nikhil probably doesn't care about your problem like the rest of us do.
snehal jaipurkar
on 24 Oct 2016
If I am having a color image and I want to find the total no of pixels in a specific range of pixel values then what should I do????and how to find out the range of pixel values in a certain region of an image???
Image Analyst
on 24 Oct 2016
You can take the histogram of each channel:
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
[countsR, edgesR] = imhist(redChannel);
[countsG, edgesG] = imhist(greenChannel);
[countsB, edgesB] = imhist(blueChannel);
To do it with a masked image, put the binary image mask as an index to the images.
% Extract the individual red, green, and blue
% color channels but only in the masked region(s);
[countsR, edgesR] = imhist(redChannel(mask));
[countsG, edgesG] = imhist(greenChannel(mask));
[countsB, edgesB] = imhist(blueChannel(mask));
Categories
Find more on Axis Labels in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!