Image Processing and Data Evaluation in EMT

There is an example for Euler Math Toolbox (EMT) where I tried to fit curves to a real chain. I then fitted a parabola and a catenary to the points and explained a bit about the catenary.

The points where derived by hanging a real chain in front of the screen. As one user pointed out, this is an outdated method in the ages of digital images. Of course, now you can do that with images in EMT too.

First I downloaded a free image of an egg. If you have an image from a digital camera reduce it in size. We do not want to load a high res image into EMT. You can place the image into the notebook with

>loadimg("egg.jpg");

The image was originally about 944×993 pixels. It will be reduced in size for the notebook. loadimg() accepts a number of lines to be used for the image in height. For the blog, I reduced the image too.

egg1

Now the following lines of code will load the image into EMT as a matrix of RGB (red-green-blue) values.

>M=loadrgb("egg.jpg");
>size(M)
 [944,  993]

We can analyze the image in EMT.  E.g., let us plot the average portion of red color in each column of the image.

>aspect(2); plot2d(sum(getred(M'))'/rows(M)):

red

We could also process the image. Let us extract the RGB values and manipulate them to put a warm tint to the image.

>{R,G,B}=getrgb(M);
>function f(x,a) := x + a*x*(1-x)
>insrgb(rgb(f(R,0.4),G,B));
>savergb(rgb(f(R,0.4),G,B),"egg2.png");

The insrgb() command inserts the RGB image into the notebook. The savergb() command saves the image. It looks like this.

egg2

You can also plot the image as a background image to your plot. The command is plotrgb(). It uses the current plot area as set by window(). In this blog, we make it simple and set a plot are with setplot(). We draw a coordinate system with xplot(). These are older and more basic functions than plot2d(). But plot2d() relies on these functions too.

>savergb(rgb(f(R,0.4),G,B),"egg2.png");
>aspect(1); setplot(0,1,0,1); plotrgb(M); xplot();

plot

You will notice that the plot is not very efficient. That is why you need to reduce your digital image to 1MB or less. This problem might be addressed in further versions of EMT. E.g., we could read the image from a file or a list of loaded images for each plot. Currently, plotrgb(M) for matrices M is the way to go. Of course, you can also use EMT to reduce your image. One way would be to smoothen it with a two matrix fold and then to take every other point. In the example, we reduce the size by a factor of 3.

>E=ones(5,5)/25; 
>M1 = rgb(fold(R,E),fold(G,E),fold(B,E));
>M1=M1[2:3:rows(M1),2:3:cols(M1)]; size(M1)
 [313,  330]
>savergb(M1,"egg2.png");

egg2

Note that the previous images of the egg were reduce for the blog. This is the original result of the reduction in EMT.

Assume we want to get points on the outline of the image. For this, we write a little code in EMT to get mouse clicks from the user.

>function getclicks () ...
$global M;
$setplot(0,1,0,1); plotrgb(M); xplot();
$hold on;
$v=[];
$repeat;
$   m=mouse("Click or press Return!");
$   until length(m)<2;
$   v=v_m;
$   mark(m[1],m[2]);
$end;
$return v;
$endfunction
>v=getclicks()
      0.401925     0.0930601 
      0.249599       0.20031 
      0.131469      0.352635 
      0.066187      0.560917 
      0.112817      0.758318 
       0.28224        0.8951 
      0.569794      0.901318 
      0.768749      0.794068 
       0.88377      0.675938 
      0.956825       0.51895 
      0.959933      0.332429 
      0.900868      0.194092 
      0.762532     0.0868427 
      0.599326     0.0510929 

clicks

 

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.