MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: importing image and getting numbers from the gray intensity

  • To: mathgroup at smc.vnet.net
  • Subject: [mg49470] Re: [mg49445] Re: importing image and getting numbers from the gray intensity
  • From: Yasvir Tesiram <yat at omrf.ouhsc.edu>
  • Date: Wed, 21 Jul 2004 06:39:44 -0400 (EDT)
  • References: <cd5okp$b2c$1@smc.vnet.net> <200407201153.HAA23720@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,
Some additional notes on imported images. I've used the same image as 
in your original post and saved a copy as ".png" format. This is a 
format that Mathematica knows how to Import. Everything in (* *) is a 
comment and everything else you can copy and paste into a Notebook and 
evaluate as usual.

In brief, if you Import a known format picture into Mathematica the 
resulting expression is a Raster. If you copy and paste a picture into 
the Mathematica Front End and then change the cell to InputForm from 
the commands in the menu bar as Paul Abbot had suggested then you will 
end up with a RasterArray object. This is not a version thing. Here is 
the imported image.

g1=Import["~/Desktop/SDS-PAGE.jpg"];
g2=Import["~/Desktop/SDS-PAGE.png"];

(* Mathematica Imports files of various formats and stores them as a \
Mathematica expression.*)
(* There are 4 items in the expression that Mathematica has stored. 
Actually there are plenty but lets just stick to this first. *)
Length[g1]
Length[g2]

(* What are these four items*)
(* Note that 0 is the uppermost level of any Mathemtica expression and 
I've \
included it here to show that g1 and g2 are ultimately graphics 
objects*)

Head/@g1[[#]]&/@{0,1,2,3,4}
Head/@g1[[#]]&/@{0,1,2,3,4}

(* Despite the fact our files
   are two different formats and have completely different 
FileByteCounts (on disk), \
they use up the same number of bytes when stored as a Mathematica 
expression \
*)

FileByteCount/@{"~/Desktop/SDS-PAGE.jpg","~/Desktop/SDS-PAGE.png"}
ByteCount/@{g1,g2}

Out[292]=
4

Out[293]=
4

Out[294]=
{Graphics,Raster[List,List,List,Rule],Symbol\[Rule]List,Symbol\[Rule]
     List,Symbol\[Rule]Symbol}

Out[295]=
{Graphics,Raster[List,List,List,Rule],Symbol\[Rule]List,Symbol\[Rule]
     List,Symbol\[Rule]Symbol}

Out[296]=
{9787,30640}

Out[297]=
{289716,289716}



(* Since g1 and g2 are already graphics objects we can just show it etc 
*)
Show[GraphicsArray[{g1, g2}]]


(* But we are interested in the data so lets get it *)

d1 = g1[[1, 1]];
d2 = g2[[1, 1]];

(* Here are the dimensions of the data *)

Dimensions[d1]
Dimensions[d2]

(* And of course we could have got them from the Imported Mathematica \
expression *)

g1[[1,2]]
g2[[1,2]]

Out[318]=
{289,250}

Out[319]=
{289,250}

Out[320]=
{{0,0},{250,289}}

Out[321]=
{{0,0},{250,289}}

Out[322]=
{0,255}

(* Have a look at the notes for Raster in the Help files *)
(* The first argument to Raster is an array (data) and the second
   are the bounds of the image*)
(* And the third and fourth arguments are for the color range and \
ColorFunction *)


(* We have the data so here are some ways to look at it*)
<< Graphics`Colors`

(* A colored contour plot *)
ListContourPlot[d1, ColorFunctionScaling -> True, ColorFunction -> Hue, 
\
Contours -> 20, PlotRange -> All]

(* A gel that jumps out at you *)
ListPlot3D[(255 - d1), Mesh -> False, PlotRange -> All]

(* If you were wondering about the 255, here is a negative of your gel 
*)
ListDensityPlot[(255 - d1), Mesh -> False]

(* Here is a chromatogram the right way up *)
lane1 = ListPlot[Transpose[(255 - d1)][[26]],
PlotStyle -> Black, Frame -> True, PlotJoined -> True, ImageSize -> 
600];

Cheers
Yas


On Jul 20, 2004, at 6:53 AM, sean kim wrote:

> Hi everyone,
>
> follow up on Paul's, George's Jerry's and Yas' suggestions.
>
> Yes, it must be version dependent. I'm getting exactly what George is
> getting as well.
>
> In[4]:= $Version
> Out[4]= 5.0 for Microsoft Windows (June 12, 2003)
>
> And I named the input cell pixels, not pixel.
>
> And I'm getting Raster instead of RasterArray object
>
> I though I would be clever and change the Input to RasterArray, then
> it brings back...
>
> RasterArray::rscol: Expression 0.160784` at position {1, 1} in first
> argument \
> of RasterArray is not a valid color or gray-level directive
>
> the numbers are not the same because I took the negative image from
> Irfanview image viewer.
>
> Also I noticed that if I take the negative image from irfanview and
> save it as jpg, non of the suggestions work, but that's not related to
> Mathematica, i don't think. it must be more of how irfanview encode 
> the jpg
> images...
>
> IF I use Raster in your codes as George said and with the original
> image,
>
> In[16]:=
> pixels = First[Cases[pixels, Raster[x__] :> x, Infinity]] /.
> RGBColor[r_, g_, b_] :> (r + g + b)/3
>
> ListDensityPlot[pixels,Mesh\[Rule]False];
>
> then i get,
>
>
> {{0.839216, 0.839216,0.807843,
> ...
> long list of numbers
> ...
> 0.905882, 0.905882, 0.905882}}
>
> and the the original plot.
>
> here is something that is kinda weird for me,
>
> if I repeat evaluate that cell,
>
> In[18]:=
> pixels = First[Cases[pixels, Raster[x__] :> x, Infinity]] /.
>     RGBColor[r_, g_, b_] :> (r + g + b)/3
> ListDensityPlot[pixels,Mesh\[Rule]False];
>
> From In[18]:=
> First::first: {} has a length of zero and no first element
>
> Out[18]=
> First[{}]
>
> From In[18]:=
> DensityGraphics::gmat: First[{}] is not a rectangular array larger
> than 2 x 2.
>
>
>
> And both Jerry and Yas used the columns to find the lanes.  After
> trying the hint, I totally understand where you guys got the lane
> numbers. It tells you right on the picture. Mathematica must have 
> taken the
> image and defined it in an array. so that the dimentions tells you how
> many Rows and Columns are in the array (i guess?)
>
> If I use a negative image, then the chromatograms you suggested looks
> much better, as you get peaks instead of dips.
>
> ListPlot3D[d, Mesh -> False, PlotRange -> All, AspectRatio ->
> Automatic, ImageSize -> 400]
>
> Above will really demonstrate the limitation of the gray levels human
> eye can perceive. if you look at the center of the bands, they seem to
> be of one intensity but Mathematica wil actually show you differing 
> levels in
> the form of peaks!
>
> Anyway, thank you all very much.
>
> I'm starting to appreciate the power of Mathematica.  it's pretty 
> amazing.
> Even though I'm still not very good at it. (but I'm getting better
> thanks to this group)
>
>
> sean
>
>
> Hi Paul (Jean and Sean),
>
> Thanks for the clarification on how the function works.  Also, from
> your
> post to Sean, I realize now that my issue was that my Mathematica
> version:
>
> In[8]:=  (9:08:18 on 7/19/04)
> $Version
>
> Out[8]=  (9:08:18 on 7/19/04)
> 5.0 for Microsoft Windows (June 10, 2003)
>
> returns a "Raster" object.  So simply using Raster[x__] in place of
> RasterArray[x__] makes it work.
>
> Thanks greatly,
>
> George
>
> -----Original Message-----
> From: Paul Abbott [mailto:paul at physics.uwa.edu.au]
To: mathgroup at smc.vnet.net
> To: mathgroup at smc.vnet.net
> Subject: [mg49470] [mg49445] RE: [mg49371] Re: importing image and getting 
> numbers from
> the
> gray intensity
>
> Hi George:
>
>> I suppose this is terrible etiquette.  I do not use post groups like
>> this often.  But, since I discovered this (through your "In and Out",
> I
>
>> have become rather addicted to the group.
>
> I'm glad that you like MathGroup
>
>> I am very interested in your response to this post.  However, I do
> not
>> understand it.  I am okay up to getting the following into my cell:
>>
>> pixels=Show[Graphics[{
>> AbsoluteThickness[1],
>> Raster[{
>> {0.827451,.... 0.882353}},
>> {{4, 4}, {254, 293}}]
>> }], AspectRatio->1.156,
>> PlotRange->{{4, 254}, {4, 293}},
>> ImageSize->{250, 289}]
>
> That looks fine to me.
>
>> But, then I do not understand what part of this expression I assign
> to
>> "pixels".
>
> As you have done above (typing "pixels=" into the InputForm cell).
>
>> Perhaps my confusion is due to another issue, which is that I have
>> never named a Cell, and therefore, I assume I should name part of the
>> above expression "pixels".  I made a couple of attempts, such as:
>>
>> pixels={{0.827451,.... 0.882353}};
>>
>> Or
>>
>> Pixels=Raster[{{0.827451,.... 0.882353}},{{4, 4}, {254, 293}}];
>
> You could do either of these things. The idea I was presenting here is
> to assign pixels to be the full graphic object (the result of the
> Show[]
> command) and then use Mathematica commands to extract from this
> graphic
> the information giving the colors by _overwriting_ the value of pixels
> using
>
>     pixels = First[Cases[pixels, RasterArray[x__] :> x, Infinity]] /.
>        RGBColor[r_, g_, b_] :> (r + g + b)/3
>
>> However, I am having no success with your function.  Also, you can
> see
>> that part of my problem is that I do not understand in totality, the
>> syntax of your command.
>
> You can de-construct this command as follows. The inner part
>
>    Cases[pixels, RasterArray[x__] :> x, Infinity]
>
> locates the RasterArray in pixels and saves its argument, x. There are
> two parts to the RasterArray but only the first part is relevant,
> extracted using First. Then, for each color primitive RGBColor[r, g,
> b]
> we average the r,g,b values using the replacement rule
>
>    RGBColor[r_, g_, b_] :> (r + g + b)/3
>
> Then we call the result of this pixels (overwriting its earlier
> value).
> As a check we re-draw the picture
>
>    ListDensityPlot[pixels, Mesh -> False];
>
> Cheers,
> Paul
>
>        This message may contain privileged and/or confidential
> information.  If you have received this e-mail in error or are not the
> intended
> recipient, you may not use, copy, disseminate or distribute it; do not
> open any attachments, delete it immediately from your system and
> notify
> the sender promptly by e-mail that you have done so.  Thank you.


  • Prev by Date: Re: Question about shape of histogram of minimal prime partition p's
  • Next by Date: LogScale ListPlot
  • Previous by thread: Re: importing image and getting numbers from the gray intensity
  • Next by thread: Help -- Weird integration behavior