Razões e proporções

1. Calcule as razões abaixo, simplificando o resultado, quando possível:
a) de 2 horas para 45 minutos;
b) de 300 m para 2 km;
c) de 2 m² para 400 cm²;
d) de 5 meses para 2 anos;
e) de 5 minutos e 20 segundos para 2 horas e meia.

Resolução da letra a – vamos escolher minuto como a grandeza comum e então transformar as medidas para minutos: 1 hora = 60 minutos.

\frac{2 \times 60}{45} = \frac{120}{45} = \frac{8}{3} \times \frac{15}{15} = \frac{8}{3}

Resolução da letra b – vamos escolher metro como a grandeza comum e então transformar as medidas para metro: 1km = 1.000m.

\frac{300}{2 \times 1000} = \frac{300}{2000} = \frac{3}{20}

Resolução da letra c – vamos escolher o cm² como grandeza comum e então transformar as medidas para cm²: 1m² = 10000 cm²

\frac{2 \times 10000}{400} = \frac{20000}{400} = \frac{50}{1} = 50

Resolução da letra d – vamos escolher o mês como grandeza comum e então transformar as medidas de anos para meses: 1 ano = 12 meses

\frac{5}{2 \times 12} = \frac{5}{24}

Resolução da letra e – vamos escolher o segundo como grandeza comum e então transformar as medidas de horas e minutos para segundos: 1 hora = 60 minutos e 1 minuto = 60 segundos

\frac{5 \times 60 + 20}{2 \times 60 \times 60 + 30 \times 60} = \frac{320}{9000} = \frac{8}{225} \times \frac{40}{40} = \frac{8}{225}

2. Numa data t o preço de um produto é o tripo do que era na data 0.
a) Qual a razão entre o preço na data t e o preço na data 0?
b) Qual a razão entre o aumento de preço ocorrido entre as duas datas e o preço na data 0?

A letra a pede a razão entre o preço na data t (3 \times x) e o preço na data 0 (x)

\frac{3 \times x}{x} = 3

A letra b pede a razão entre o aumento de preço ocorrido (o preço era x e passou para 3x então teve um aumento de 2x) e o preço na data 0 (x)
\frac{2 \times x}{x} = 2

Posted in Matemática Básica | Leave a comment

JPEG Compression and Quality rate

Octave provides a way to manually set the quality rate, using the command imwrite it is possible to set the Quality as a parameter.

The Quality parameter assume values between 0 and 100, and as the documentation states, higher values means higher visual quality and less compression.

Quality File Size (Kb)
100 725
95 370
90 301
80 221
50 158
7 14
Numeric relation between Quality and File Size
Graph relation between Quality and File Size

To generate this graph I generated 100 images varying the Quality factor, I used the following script:

y = jpg2gray("c:\\mona-lisa.jpg");
for i = 1:1:100
   imwrite(y, ["c:\\temp\\", num2str(i), ".jpg"], "jpg", "Quality", i);
endfor

# Generating the graph
hold("on");
grid("on");
plot(x, y, "LineWidth", 3);
set(gca(), "fontname", "Courier New");
set(gca(), "fontsize", 10);
set (ax, "position", [0.2, 0.2, 0.7, 0.7]);
xlabel("Quality (Percentage)", "FontName","Courier New","FontSize",10);
ylabel("File Size (Bytes)", "FontName","Courier New","FontSize",10, "rotation", 90);
hold("off");
Sample with Quality 1 Sample with Quality 10 Sample with Quality 75
Posted in Uncategorized | Leave a comment

Configuring SLF4J with LOG4J

The purpose of this post is to show how to use Simple Logging Facade for Java (SLF4J) having Log4j as the logging framework.

In this post I used SLF4J version 1.6.1 and Log4j version 1.2.16.

In the Eclipse IDE, create an ordinary Java Project, create a lib directory and copy the following libraries to it:

  • slf4j-api-1.6.1.jar
  • slf4j-log4j12-1.6.1.jar
  • log4j-1.2.16.jar

Add these libraries (jar-files) to the build path.

To configure the log4j we created the log4j.properties below, this file configure the rootCategory to append the log content to the console.

log4j.rootCategory=INFO, S

log4j.appender.S=org.apache.log4j.ConsoleAppender
log4j.appender.S.layout=org.apache.log4j.PatternLayout
log4j.appender.S.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n

File log4j.properties.

Below we show three examples of Console Java Applications that use the SLF4J with log4j.

Automatic.java
This is for sure the preferable way to use the SLF4J, because as you are using an abstraction layer for the Log Library, you’ll wish to have the minimum (or nothing if it is possible) of log4j-related code in your project, so in a future change of the log library, the minimum log4j-related code in your project than the minimum you will have to adapt.
In this case, the log4j.properties must lay in the scr directory.
ViaProperties.java
This is a very similar example, but I called the PropertyConfigurator.configure static method in order to explicity configure the log4j, this can be useful if you have ohter property files.
ViaCode.java
In this console application we configure the log4j programatically.
package org;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Automatic {
	
	final static Logger logger = LoggerFactory.getLogger(Automatic.class);
	
	public static void main(String arg[]) {
		
		if (logger.isInfoEnabled()) {
			logger.info("Hello World!");
		}
		
	}

}

File Automatic.java

package org;

import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ViaProperties {
	
	final static Logger logger = LoggerFactory.getLogger(ViaProperties.class);
	
	public static void main(String arg[]) {
		
		PropertyConfigurator.configure("src/log4j.properties");
		
		if (logger.isInfoEnabled()) {
			logger.info("Hello World!");
		}
		
	}

}

File ViaProperties.java

package org;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.PatternLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ViaCode {

	final static Logger logger = LoggerFactory.getLogger(ViaCode.class);

	public static void main(String arg[]) {

		log4jSetUp();

		if (logger.isInfoEnabled()) {
			logger.info("Hello World!");
		}
	}

	public static void log4jSetUp() {
		org.apache.log4j.Logger rootLogger = org.apache.log4j.Logger.getRootLogger();
		if (!rootLogger.getAllAppenders().hasMoreElements()) {
			rootLogger.setLevel(Level.ERROR);
			rootLogger.addAppender(new ConsoleAppender(new PatternLayout("%-5p [%t]: %m%n")));
		}
	}

}

File ViaCode.java

Posted in Uncategorized | Leave a comment

Converting a RGB image to a gray-scale image

In this post we use Octave to read a JPG image file, wich is an RGB file, and convert it to a gray-scale image.

This procedure is performed by the following steps:

  1. Read the JPG image to an RGB matix.
  2. Convert the RGB image to an color-indexed image
  3. Convert the color-indexed image to a gray-intensity matrix
  4. Treat the borders of the gray matrix in order to fit in the uint8 boundaries.
[img, map, alpha] = imread ("c:\\temp\\mona-lisa.jpg");
[x, map] = rgb2ind (img);
y = ind2gray (x, map);
y = uint8((255 * y) / max(max(y)));
imwrite(y, "c:\\temp\\gray.jpg", "jpg", "Quality", 75);

Below there is an example , in the left the original picture and in the right the treated image.


Posted in Uncategorized | Leave a comment

Separating the RGB components of an image

In this post we are going to use Octave to show how the RGB components of an image can be separated and viewed.

To perform this task, we created a custom function wich receives a filename as input parameter and generates three files (R, G, B) in the same directory of the input file.

This version of the separate_rgb function works fine with JPG images and the input filename must have a dot.

function separate_rgb(file_name)

   pos = findstr(file_name, ".");
   [IMG, MAP, ALPHA] = imread(file_name);

   red = IMG;
   red(:,:,2) = 0;
   red(:,:,3) = 0;
   imwrite (red, [file_name(1:pos(1, size(pos,2)) - 1) , "_red", file_name(pos(1, size(pos,2)): size(file_name, 2))], "jpg", "Quality", 75);

   green = IMG;
   green(:,:,1) = 0;
   green(:,:,3) = 0;
   imwrite (green, [file_name(1:pos(1, size(pos,2)) - 1) , "_green", file_name(pos(1, size(pos,2)): size(file_name, 2))], "jpg", "Quality", 75);

   blue = IMG;
   blue(:,:,1) = 0;
   blue(:,:,2) = 0;
   imwrite (blue, [file_name(1:pos(1, size(pos,2)) - 1) , "_blue", file_name(pos(1, size(pos,2)): size(file_name, 2))], "jpg", "Quality", 75);

endfunction

Below are two examples of this scripts in action.

Posted in Uncategorized | Leave a comment