Posts Tagged ‘Code’

Playing with Matlab

06/21/2013

Image registration using ga (genetic algorithm)

Given two images, A and B, try to find affine transformation to maximize correlation between the to images.

This simple code works on simple images with simple transformations.

function res=reg1()
 clear all;
 close all;

 global A B
 A=imread('/home/komap/Desktop/SuperResolutionMatlab/Data/IMAG0446.jpg');
 B=imread('/home/komap/Desktop/SuperResolutionMatlab/Data/IMAG0447.jpg');

 A=rgb2gray(A);
 B=rgb2gray(B)

 options = gaoptimset('Generations',50);
 options = gaoptimset(options,'MutationFcn',@mutationadaptfeasible);
 options = gaoptimset(options,'PlotFcns',{@gaplotbestf,@gaplotmaxconstr}, 'Display','iter');
 options = gaoptimset(options,'InitialPopulation',[1 0 0 0 1 0]);
 [a,fval] = ga(ObjectiveFunction,6, options);
end;

function a = cor(alfa)
 global A B
 alfa = reshape(alfa, 3, 2);
 t_n = maketform('affine', alfa);
 D = imtransform(A,t_n);
 D = imresize(D, size(B));

 a = 1-corr2(B, D);
end
Advertisements

999…

02/08/2010

Question

Let N = any number not divisible by 2 and 5.
Does there exist a k (for each such N), such that 10^k – 1 is divisible by N?
Or: Is there 99..9 for any N, such as 99..9 is divisible by N, if N is coprime with 10?

Answer

Yes.  It is multiplicative order of 10 modulo N. The sequence is can be found at The On-Line Encyclopedia of Integer Sequences.

Proof

Trivial.

Code

-- all numbers than cannot be devided by 2 or 5
seq1 :: [Integer]
seq1 = filter (\a->(a `mod` 10) `elem` [1,3,7,9]) [1..]

-- find 99..9 that can be devided by n
findNum::Integer->Integer
findNum n = head $ [x | x<-[1..], (10^x-1) `mod` n == 0]

--prints the sequene
take 100 $ map findNum3 seq2