Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Learning image processing in Matlab 7

Status
Not open for further replies.

Faizan Jawaid

Full Member level 3
Full Member level 3
Joined
Jul 17, 2008
Messages
189
Helped
23
Reputation
46
Reaction score
5
Trophy points
1,298
Location
Karachi, Pakistan
Activity points
2,336
Hi all...I want to learn Image processing...But i dont know basic even...I use matlab 7.0....If somebody could help me in processing an Image which simply has a Circle in centre...and another image with a triangle..How can we distinguish between them using matlab...

Regards
 

Re: I need basic help!

Reading Image Segmentation chapter of any Digital Image Processing would help you find solution yourself

Cat Le
 

Re: I need basic help!

An open exchange for the MATLAB and Simulink user community Hosted by The MathWorks
Search: MATLAB Central File Exchange MATLAB Newsgroup Link Exchange Blogs Programming Contest MathWorks.com All of MathWorks.com
File Exchange MATLAB Newsgroup Link Exchange Blogs Contest MathWorks.com
Steve on Image Processing
February 10th, 2006
All about pixel colors: Scaled indexed images
In my last post on pixel colors, I described the truecolor and indexed image display models in MATLAB, and I promised to discuss an important variation on the indexed image model. That variation is the scaled indexed image, and the relevant MATLAB image display function is imagesc.

Suppose, for example, we use a small magic square:

A = magic(5)A =

17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9

Now let's display A using image and a 256-color grayscale colormap:

map = gray(256);
image(A)
colormap(map)
title('Displayed using image')
The displayed image is very dark. That's because the element values of A vary between 1 and 25, so only the first 25 entries of the colormap are being used. All these values are dark.

Compare that with using imagesc:

imagesc(A)
colormap(map)
title('Displayed using imagesc')
Here's what's going on. The lowest value of A, which is 1, is displayed using the first colormap color, which is black. The highest value of A, which is 25, is displayed using the last colormap color, which is white. All the other values between 1 and 25 are mapped linearly onto the colormap. For example, the value 12 is displayed using the 118th colormap color, which is an intermediate shade of gray.

You can switch colormaps on the fly, and the values of A will be mapped onto the new colormap.

colormap(jet)
title('Scaled image using jet colormap')
Let's dig into the low-level Handle Graphics properties that are controlling these behaviors. Image objects have a property called CDataMapping.

close all
h = image(A);
get(h, 'CDataMapping')
closeans =

direct

You can see that its default value is 'direct'. This means that values of A are used directly as indices into the colormap. Compare that with using imagesc:

h = imagesc(A);
get(h, 'CDataMapping')
closeans =

scaled

The imagesc function creates an image whose CDataMapping property is 'scaled'. Values of A are scaled to form indices into the colormap. The specific formula is:



C is a value in A, and c_min and c_max come from the CLim property of the axes object.

h = imagesc(A);
get(gca, 'CLim')
closeans =

1 25

It's not a coincidence that the CLim (color limits) vector contains the minimum and maximum values of A. The imagesc function does that by default. But you can also specify your own color limits using an optional second argument to imagesc:

imagesc(A, [10 15])
colormap(gray)
title('imagesc(A, [10 15])')
10 (and values below 10) were displayed as black. 15 (and values above 15 were displayed as white. Values between 10 and 15 were displayed as shades of gray.

Scaled image display is very important to engineering and scientific applications of image processing, because often what we are looking at it isn't a "picture" in the ordinary sense. Instead, it's an array containing measurements in some sort of physical unit that's not related to light intensity. For example, I showed this image in my first "All about pixel colors" post:



This is a scaled-image display of a matrix containing terrain elevations in meters.


Get the MATLAB code

Published with MATLAB® 7.1




7:00 am | Posted in Pixel colors | Permalink |

You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.

44 Responses to “All about pixel colors: Scaled indexed images”
Vitor replied on February 13th, 2006 at 4:49 pm :

Hi Steve. This scaled-indexed image is not technically a model, but a “subset” of a scaled one, isn’t it?
Anyway, your blog is a very good reading. Thanks.

Steve replied on February 13th, 2006 at 4:58 pm :

Vitor - Did you mean that “direct indexed” is a subset of “scaled indexed”? If so, then yes, that’s a valid way of looking at it. But I think most people think of them and use them quite differently, as do I. Also, the HG image object properties encourage thinking of them as two different models.

Suvrat Bafna replied on October 30th, 2006 at 5:09 pm :

Hey Steve, can you tell me how to measure the length of a ling using Image Processing in Matlab?

Steve replied on October 30th, 2006 at 5:16 pm :

Suvrat - I assume that “calculate the Euclidean distance between the end-points of the line segment” isn’t the answer you are looking for. However, the way you phrased your question is very vague, so that’s about all I can say. If you can be a lot more specific about your scenario, then maybe I can be more helpful.

Suvrat replied on November 6th, 2006 at 2:21 am :

Sorry wasn’t specific the last time. I actually had difficulty finding the start and end points of a crooked line from in a hazy image. Then I had to find the length of the curving line.

Steve replied on November 6th, 2006 at 8:57 am :

Suvrat - What procedure did you follow to find the start and end points of the line, and what problems did you have? For computing the length, look at the code for the ComputePerimeter function inside regionprops.m.

Suvrat replied on November 7th, 2006 at 2:33 pm :

i used the colorgrad function with a code for determining the threshold, then erosion to extract lines from the pic of the palm of a person. The main problem that i faced was that lines are not clearly demarkable. Also, there are several lines, and have to choose from amongst them though this might be the easy part.

Steve replied on November 8th, 2006 at 9:55 am :

Suvrat - I understand your question better now, but I don’t have a good answer for you. You are in the territory of serious algorithm development now. Successful methods for such problems are almost always heavily tailored to the characteristics of a specific data set, so any general advice I could give is probably not very useful.

Suvrat replied on November 10th, 2006 at 3:13 am :

I guess you’re right steve. thanks a lot. wll try to figure it out.

Charlie vrachi replied on December 29th, 2006 at 2:54 pm :

Hi Steve,

i am Charlie, I started using MATLAB recently .. i am working on image processing .. please help me to pixel information from an image .. then i want to divide all the pixels with the center pixel ..
how can I do this..
ur help is appreciated

thanks in advance

charlie

Steve replied on December 30th, 2006 at 2:02 pm :

Charlie - I’m sorry, but I do not understand your question.

Jon replied on January 11th, 2007 at 5:57 pm :

Steve, thanks for the help with image processing. The listing of the various ways to view an image is useful, but I’m hoping to find one more method. Is there a clean way to view an image WITH the pixel’s value? What I’m looking for is the jet-colormap’ed image with the raw values (17, 24, 1, etc) superimposed on each pixel. I realize that I could probably do something like this by using multiple axes, but I want to be able to zoom in and have the pixel & numeric value remain together. Is there a good way to handle this?

Thanks,
Jon

Steve replied on January 12th, 2007 at 9:35 am :

Jon - Try the Pixel Region Tool in the Image Processing Toolbox.

Marcus replied on July 15th, 2007 at 1:41 pm :

Hi Steve,

I have a simple question that I would like to ask you.

I import a RGB JPEG image, convert it to a greyscale image with 256 shades of grey, and then extract the colormap to be used for comparing with other images (generated as simulations within Matlab). Here is the code segment:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Import JPEG image as an 8-bit RGB image
myimage = imread('image.jpg');
% Adjust the image to grayscale. This is now an Jx-by-Jy array where
% II(i,j) gives a numeric value for a shade of gray between 1 and 256.
I = 0.2989*myimage:),:,1)+0.5870*myimage:),:,2)+0.1140*myimage:),:,3);
% Display the grayscale image:
figure;
colormap(gray(256));
image([0 2],[0 2],I); % notice how the 1st 2 arguments sets the axis data
title('Original image')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
So my question is how can I do the equivalent thing, but in color. Essentially what I am asking is how do I extract the colormap of the original image. If I replace ‘ colormap(gray(256))’ with e.g., colormap(jet); then the image coloring is all wrong (i.e., different from the original image).

Thank you,
Marcus.

Steve replied on July 16th, 2007 at 10:41 am :

Marcus—Your original image doesn’t have a colormap associated with it, so I really don’t know what you mean by “extract the colormap of the original image.” Can you clarify what you are trying to do?

Marcus Garvie replied on July 24th, 2007 at 11:46 am :

Hi Steve,

This question refers to your reply no. 15 above, asking for more information about what I’m trying to do. I know very little about image manipulation, so I apologize beforehand if my description is not very good.

Let me first explain what the snipet of code above (reply no. 14) does. It takes a RGB JPEG image and converts it to a FIG file, which has a grayscale color map with 256 shades of gray. Using a branch of mathematics called optimal control theory I then generate images (FIG files) that are close to this original ‘target’ image, using the SAME colormap (with the PCOLOR command). In theory an image generated using the optimal control algorithm may exactly match the original image that I loaded (i.e., look the same). Here is my problem. I want to do the same thing but in color. I want to take my original color JPEG image and convert it to a Matlab FIG file that looks the same. Thus it will have a colormap associated with it that can be used in the generation of the images from the optimal control algorithm.

I hope this is enough information to explain what I am doing!.

Thank you,
Marcus.

Steve L replied on July 25th, 2007 at 9:05 pm :

Marcus,

It sounds sort of like you want RGB2IND, which will convert your RGB image into an indexed image and a colormap.

https://www.mathworks.com/access/he...ess/helpdesk/help/toolbox/images/rgb2ind.html

https://www.mathworks.com/access/he...ss/helpdesk/help/toolbox/images/f8-15484.html

the other Steve

Steve replied on July 26th, 2007 at 11:28 am :

Marcus—I don’t really understand your emphasis on FIG files. A FIG file simply contains the data necessary to reconstruct and display a MATLAB figure window. It isn’t an image. If you have the original color image data from the JPEG file, why not compare that image directly? Converting to an indexed image and saving as a FIG file seems like a very roundabout way to compare outcomes. However, if that’s really what you want to do, then I agree with Steve Lord—you might find RGB2IND useful.

Marcus Garvie replied on July 26th, 2007 at 9:49 pm :

Thanks, both Steves. I’m getting there, but there is still a problem.

OK. So I use my numerical algorithm to generate MATLAB figures, and I wish to compare these figures with my original loaded index image (from RGBIND). And so that I’m not ‘comparing apples with oranges’ I generate these images using the colormap associated with the original indexed image. So Steve E. you are right. I don’t need to convert to a FIG file, but I do need to convert to an indexed file in order to extract a colormap.

Now here is my problem, which is a little more complicated than I first described. After converting my original RGB image to an indexed file I then interpolate this indexed image onto a triangular mesh. (I need to do this because the numerical algorithm for generating the images is a Finite Element Method, where all unknowns lie on the vertices of the triangles. ) But the interpolated image looks nothing like the original image (it looks random). Any ideas why I cannot interpolate the values of the indexed image onto a triangular mesh? The relevent code is given below.

Thank you again for any help you can provide.

Marcus.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Import JPEG image as an 8-bit RGB image
image = imread(’image.jpg’);
% Convert image to an indexed image with 256 colors with colormap ‘map’
[indexed_image,map] = rgb2ind(image,256);
% Display indexed image
figure(1);
imshow(indexed_image);
title(’Original indexed image’)
% Extract the no. of rows and columns
[Jx,Jy] = size(indexed_image);
% Construct ‘meshgrid’ on [0,2]-by-[0,2] to be used for interpolating
% onto triangular mesh
IndexIx = 1:Jx;
IndexIy = 1:Jy;
hx = 2/(Jx-1);
hy = 2/(Jy-1);
x = (IndexIx-1)*hx;
y = (IndexIy-1)*hy;
[X,Y] = meshgrid(x,y);
% Interpolate indexed image onto the vertices of the triangular mesh
interpolated_image = interp2(X’,Y’,indexed_image,xx,yy,’*bicubic’);
% Plot interpolated image onto triangular mesh
figure(2)
trisurf(t’,xx,yy,interpolated_image,’FaceColor’,'interp’,'EdgeColor’,…
‘interp’);
title(’Image interpolated onto triangular mesh’)
view ( 2 )
colormap(map)
% Note: xx and yy give the coordinates of the vertices on the triangular
% mesh, while t gives a list of nodes for the triangles.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Steve replied on July 27th, 2007 at 7:25 am :

Marcus—In general, doing any kind of math (interpolation, filtering, etc.) directly on the index values of indexed image is a bad idea. The index values have no meaning other than as lookup indices into the colormap. 5 could be the color red, and 7 could be the color blue. If you interpolate midway between them and get 6, what does that mean? There could literally be any color in the 6th slot in the colormap, totally unrelated to red or blue. This is why I remain skeptical of the notion of doing some kind of quantitative analysis based on comparing indexed images.

But I do have one possibly constructive suggestion: Use a different RGB2IND syntax. Specifically, use the one where you specify the colormap. In your code you are using the syntax where you specify the desired number of colormap colors. With that syntax, RGB2IND produces a colormap optimized for that particular image. So you are probably comparing two indexed images with different colormaps.

Marcus Garvie replied on July 27th, 2007 at 11:40 am :

Steve - but now we have come full circle! If I do as you suggest and choose a colormap beforehand, i.e. use the syntax X = rgb2ind(RGB, map), then the image will be colored differently from the original. Which explains why I was saying at the start that I wanted to extract a colormap from the original image. I already did as you suggest using map = gray(256). The interpolation works very nicely, but it’s not in color!

Thanks,
Marcus.

Steve replied on July 27th, 2007 at 11:42 am :

Marcus—So extract the colormap from the original, but then use the extracted colormap when you make the 2nd indexed image for comparison.

Marcus Garvie replied on July 27th, 2007 at 12:40 pm :

Steve - I would like to say that you have shown incredible patience with my questions. Thank you! We are almost there.

So how do I extract a good colormap from the original image. If I do, e.g.

A = imread(’image.jpg’);
image(A);
map = colormap;
B = rgb2ind(A,map);
image(B);

the image B looks nothing like image A?

Thanks again,
Marcus.

Steve replied on July 27th, 2007 at 12:49 pm :

Marcus—Aha! Maybe we are indeed almost there. (You’ve shown even more patience with my answers.) I see a critical point of confusion in your last snippet of code. If image.jpg contains an RGB image, then image(A) displays the pixels colors directly, meaning that the figure colormap is not used. When you type map = colormap you are just getting the default figure colormap, which unfortunately has nothing whatever to do with the image being displayed!

So use rgb2ind to compute a colormap for the original image, like this:

A = imread('image.jpg');
imshow(A)
[X_A,map] = rgb2ind(A, 256);
imshow(X_A,map); % or image(X_A), colormap(map)
Then when you get your second image, convert it to indexed using the colormap previously computed by rgb2ind.

B = imread('second_image.jpg');
X_B = rgb2ind(B, map);
imshow(X_B, map)
Marcus Garvie replied on July 27th, 2007 at 1:42 pm :

Sorry Steve, but as before the interpolated image is still a mess (a random bunch of dots) - see code in reply no. 19.

Also, I’m not trying to compare one JPEG image with another JPEG image, but a single JPEG ‘target’ image (interpolated onto a triangular mesh after conversion with RGB2IND) with MATLAB figures resulting from numerical solutions on the same mesh.

Thanks,
Marcus.

Steve replied on July 27th, 2007 at 1:46 pm :

Marcus—As I mentioned before, you can’t interpolate indexed images by interpolating the index array. I’m afraid I’m going to have to give up; I still suspect the whole idea is flawed.

Marcus Garvie replied on July 27th, 2007 at 3:20 pm :

Thanks for trying Steve. Just one last comment. If we do

A = imread(’image.jpg’)
map = bone(256); % for example
B = rgb2ind(A,map);

and then apply the interpolation process to image B it WORKS, but the color doesn’t necessarily match the original image. So you CAN apply interpolation to indexed images provided the colormap is in some ordered state.

Stella replied on August 18th, 2007 at 7:43 am :

Hey Steve .I have to use matlab in order to manipulate my research results.My problem is that I don’t know how to calibrate my images.I have write a small script to convert the pixels into centimeters..I know for spesific distance how many pixels I have.For example for 22 cm is 260 pixels in my image…So I found the conversion from pixels to centimeters…is 11.8 pixels/cm…But after that I don’t know how to write the script and for every image to make the conversion..Can u help me?pls?thanx

Steve replied on August 20th, 2007 at 9:28 pm :

Stella—Can you be specific about the desired inputs and outputs of your script? What form do they take?

Stella replied on August 21st, 2007 at 5:01 am :

Hey Steve
I have a sequence (about 250 frames) and I want each frame to have a real dimensions.I record the movement of dye in water so I need to know how many cm moves every 5 sec. So I know the time in each frame and I know that pix*0.08645=cm (because as I said before 260pixels ==22cm)

Steve replied on August 21st, 2007 at 2:44 pm :

Stella—I’m afraid I’m still not getting it. If you already the know how to convert from pixel units to centimeters, then what else do you need?

Gabriel replied on September 19th, 2007 at 3:24 am :

Hi Steve,

This is my first message to the matlab central. I have random points of X and Y, where X and both Y are “m times 1″ vector each. Then, I generate triangular meshing via TRI = delaunay(X,Y), which gives me “n times 1″ vector.

At the same time, I have a set of surface data of “n times 1″ vector. May I know how to surf the surface data (”n times 1″) on the information of TRI (”n times 1″) ?

From the help file, I know that if using the command: trisurf(tri,X,Y,rand(size(X),1)), I can plot the distributed point data. So, my question is, how to plot the distributed surface data, instead of the point data, based on the information of TRI?

Thank you.

best rgds,
Gab

Steve replied on September 19th, 2007 at 8:34 am :

Gabriel—You have posted a comment to my image processing blog. Since your question doesn’t appear to be related to image processing, perhaps you meant to post a message to the MATLAB newsgroup, comp.soft-sys.matlab? Go to MATLAB Central and click on “MATLAB Newsgroup.”

Gabriel replied on September 19th, 2007 at 9:50 am :

oh, sorry for the misplacement. All because I saw no. 19 and the use of “trisurf” has reminded me of this problem. Thank you for the clue. I think I would post there.

best rgds,
Gab

Gabriel replied on September 19th, 2007 at 9:49 pm :

Hi Steve,

I have an image question, and is hopefully is relevant to this blog.

I have a matlab model (finite element model) and wanted to mount an image [imread(’image.jpg’)] to the generated nodes from my model. Then, I want to displace all the nodes and am expected to see the stretching of the image.

My question is:

Is there any function that can assign every of the image pixel (or gradient level) on every nodes (or data points) or surface between the nodes, so that the image is shown stretched, by displacing nodes?

Thank you for your time.

Steve replied on September 20th, 2007 at 9:33 am :

Gabriel—You can texture-map an image onto a surface object. See the MATLAB Handle Graphics documentation about surfaces and texture mapping.

v.ravi sankar reddy replied on September 22nd, 2007 at 12:34 am :

sir,
i want the source code for rotating an image in digital image processing using matlab

Steve replied on September 22nd, 2007 at 7:19 am :

V.Ravi—See the code in the Image Processing Toolbox.

Sridharan replied on September 27th, 2007 at 9:00 pm :

Hi Steve,

Have a look at the following code. I have an image (apple.bmp) which is grayscale. I want to display an indexed image on top of it with a transparency value associated to it. But i get some weird pink areas on my image. Could you please let me know why this is happening??

label_color(1,:)=[0 0 0];
label_color(2,:)=[1 0 0];
label_color(3,:)=[0 1 0];
label_color(4,:)=[0 0 1];
label_color(5,:)=[1 1 0];
label_color(6,:)=[0 1 1];
label_color(7,:)=[1 0 1];

a1=imread(’apple.bmp’);

d_label=zeros(368,400);
d_label(100:200,100:200)=1;
mask=zeros(368,400);
index1=find(d_label~=0);
mask(index1)=.3;
figure,imshow(a1,[]),hold on
h=imshow(uint8(d_label),label_color);
set(h,’Alphadata’,mask)

Steve replied on October 2nd, 2007 at 6:22 pm :

Sridharan—When imshow displays a grayscale image, it installs a gray colormap in the figure. In your second call to imshow, it replaced the figure’s gray colormap with your label_color colormap. That’s why your gray image starts to look pink. A figure can only have one colormap active at a time. I suggest that you convert your images to truecolor in order to get the desired effect.

smita replied on January 9th, 2008 at 2:05 am :

Hi,
steve we are taking a color image converting it into gray image then wavelet transform is going to apply on that gray image.my question to you is can we recover original color image from that wavelet aplplied gray image.
Thank you.

Steve replied on January 9th, 2008 at 11:23 am :

Smita—When you converted your color image to gray, you discarded information. You can’t get it back.

Alex replied on April 2nd, 2008 at 2:27 pm :

Dear Steve,
Why am I having trouble using the imcrop function with a scaled image? Or, rather, why does the image not appear scaled anymore when I try to do imcrop on it? I have 16bit TIFF files with values that go from 0 to like 1500. I can use the imagesc function to display them just fine. But what I want to do then is to create regions of interest on such a scaled image. I was trying to use imcrop, but as soon as I put in [ROI,rect] = imcrop(I,hot); the scaled image that was on the screen is replaced by a solid black rectangle. Why is this, and how do I make it work how I want?
Thank you if you can help!

Steve replied on April 16th, 2008 at 12:42 pm :

Alex—The default colormap length returned by hot has only 64 colors, so it doesn’t make sense to use it to display a 16-bit image with values that go from 0 to 1500. When I try that, I get an image that’s all white, which is what I would expect. You might have better luck with something like this:

% Display using the autoscaling syntax of imshow
imshow(I, [])

% Crop
[ROI,rect] = imcrop;
Leave a Reply

Click the "Preview" button to preview your comment here.
Name (required)
E-mail (required, will not be published)
Website (optional)
Spam protection: Sum of 2 + 7 ?






About
Steve Eddins manages the Image & Geospatial development team at The MathWorks and coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.


Subscribe
Posts (e-mail)
Posts (feed)
Comments (feed)
Image Processing Toolbox
“Image Processing Using MATLAB” Recorded Webinar
Demos
Documentation
Product page
Trial software available
Links
Blog archive
GUI programming resources
Categories
Blog policies
Connected components
Image deblurring
Indexing
Pixel colors
Spatial transforms
Uncategorized
Upslope area
Recent Comments
reji A p: hai I want to upsample the image using cubic spline interpolation. Can I use ‘interp2′ instruction for...
ilyas: I am sorry. I mean how I can exract and save the part of image which I select by mouse.
ilyas: Hi, Steve, I am very thankful for your nice explanation. I would like to ask you a question. when we use...
Swetha: Hello, If you are confused with the two images taken ,i have taken image1- some image ,image2-clipped version...
Swetha: Hello Steve, The posts on connected components is very useful.I have tried relabelling the objects with two...
Sven: Hi Steve and all, Just a little extra in case you want to fill *midsized* holes. The following fits in at Step...
Ratnakar Dash: Hi, I have another question. Can any one name some features that change with blurring ? The features...
reji A p: Sir thank you very much for giving information for my last question.sir now I want to upsample the image...
Steve: Shalin—The Image Processing Toolbox function imclearborder uses the method you describe to remove...
Steve: Roy—I love using the new object-oriented programming features in R2008a, and I hope many of our users...
Blog Admin
Login
Dashboard



These postings are the author's and don't necessarily represent the opinions of The MathWorks.


Related Topics
New Products | Support | Documentation | Training | Webinars | Jobs | Newsletters
Problems? Suggestions? Contact us at files@mathworks.com © 1994-2008 The MathWorks, Inc. Trademarks Privacy Policy
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top