i'm trying to implement Gabor Wavelet feature as described in this paper:
"Texture Features for Browsing and Retrieval of Image Data"
the feature vector is composed from mean and standard deviation (example of feature vector below has scale=4 and orientation=6)
Implementation code:
void gabor_main(int argc, char **argv)
{
int img_height; // height of input image
int img_width; // width of input image
int side; // side (filter dimension = (2*side+1)*(2*side+1)) = 60
int scale; // number of scale
int orientation; // number of orientation
int flag; // flag (removing the DC term) = 0 (False)
FILE* fp;
unsigned char *tmp_raw_img; // temporary raw image data
double Ul; // Uh (highest spatial frequency)
double Uh; // Ul (lowest spatial frequency)
Matrix* img_mat; // input image
Matrix* F_r; // result, real part
Matrix* F_i; // result, imaginary part
Matrix* F_m; // result, magnitude of real part and imaginary part
scale = 4;
orientation = 6;
Ul = 0.1;
Uh = 0.4;
flag = 0;
side = 60;
...
/* ----------------- Reading raw image ----------------- */
...
/* ----------------- Gabor filtered outputs ----------------- */
CreateMatrix(&F_r, img_height * scale, img_width * orientation); // memory allocation of real part matrix of the output
CreateMatrix(&F_i, img_height * scale, img_width * orientation); // memory allocation of imaginary part matrix of the output
CreateMatrix(&F_m, img_height * scale, img_width * orientation); // // memory allocation of magnitude of the output
GaborFilteredImg(F_r, F_i, img_mat, side, Ul, Uh, scale, orientation, flag);
/* ----------------- Compute Feature Vector ----------------- */
// Magnitude of complex value
for (int h = 0; h < (img_height * scale); h++)
{
for (int w = 0; w < (img_width * orientation); w++)
{
F_m->data[h][w] = sqrt(F_r->data[h][w] * F_r->data[h][w] + F_i->data[h][w] * F_i->data[h][w]);
}
}
for(int i = 0; i < scale; i++)
{
for(int j = 0;j < orientation; j++)
{
double avg = Average(F_m, img_height, img_width, i, j);
double std = StandardDeviation(F_m, img_height, img_width, i, j);
// Print the result
std::cout << avg << " " << std << "\n";
}
}
FreeMatrix(F_r);
FreeMatrix(F_i);
FreeMatrix(F_m);
}
code of mean and standard deviation:
double Average(Matrix* F_m, int img_height, int img_width, int scale, int orientation)
{
double avg = 0.0;
for (int h = (img_height * scale); h < (img_height * (scale + 1)); h++)
{
for (int w = (img_width * orientation); w < (img_width * (orientation + 1)); w++)
{
avg += F_m->data[h][w];
}
}
avg /= (img_height * img_width);
return avg;
}
double StandardDeviation(Matrix* F_m, int img_height, int img_width, int scale, int orientation)
{
double std = 0.0;
double avg = Average(F_m, img_height, img_width, scale, orientation);
for (int h = (img_height * scale); h < (img_height * (scale + 1)); h++)
{
for (int w = (img_width * orientation); w < (img_width * (orientation + 1)); w++)
{
double dif = F_m->data[h][w] - avg;
std += (dif * dif);
}
}
std = sqrt(std / (img_height * img_width));
return std;
}
note:
code of function of GaborFilteredImg i copied from this http://vision.ece.ucsb.edu/texture/software/gabor.c
i would like to ask if the code i wrote (starting from "Compute Texture Feature" section) is correct. I am not sure in getting mean and std given output F_r (real part) and F_i(imaginary part). Basically i calculate the mean and std for every response of gabor filter bank
===UPDATE===
Those F_r and F_i are the result of gabor filtering using scale=4 and orientation=6.
Both F_r and F_i have dimension (img_height * scale) * (img_width * orientation) basically are composed of grids for each response of gabor filter bank.
Then i compute the magnitude F_m(x,y) = sqrt(F_r(x, y) * F_r(x, y) + F_i(x, y) * F_i(x, y))
Finally i calculate the feature vector which is the mean and standard deviation of F_m
===IMAGES===
Image input (real): http://goo.gl/kc5BG
Gabor banks (real) : http://goo.gl/0qM4E
Gabor banks (imaginary) : http://goo.gl/r7Fnk
Output (real) : http://goo.gl/nxVMn
Output (imaginary) : http://goo.gl/SnD7p
Related
How to create a Gaussian kernel by only specifying its width w (3,5,7,9...), and without specifying its variance sigma?
In other word, how to adapt sigma so that the Gaussian distribution 'fits well' w?
I would be interested in a C++ implementation:
void create_gaussian_kernel(int w, std::vector<std::vector<float>>& kernel)
{
kernel = std::vector<std::vector<float>>(w, std::vector<float>(w, 0.f)); // 2D array of size w x w
const Scalar sigma = 1.0; // how to adapt sigma to w ???
const int hw = (w-1)/2; // half width
for(int di = -hw; di <= +hw; ++di)
{
const int i = hw + di;
for(int dj = -hw; dj <= +hw; ++dj)
{
const int j = hw + dj;
kernel[i][j] = gauss2D(di, dj, sigma);
}
}
}
Everything I see on the Internet use a fixed size w and a fixed variance sigma :
geeksforgeeks.org/gaussian-filter-generation-c/
tutorialspoint.com/gaussian-filter-generation-in-cplusplus
stackoverflow.com/a/8204880/5317819
stackoverflow.com/q/42186498/5317819
stackoverflow.com/a/54615770/5317819
I found a simple (arbitrary) relation between sigma and w.
I want the next value outside the kernel (along one axis) below a very small value epsilon:
exp( - (half_width + 1)^2 / (2 * sigma^2) ) < epsilon
with half_width the kernel 'half width'.
The result is
sigma^2 = - (half_width + 1)^2 / (2 * log(epsilon))
I use the following c++ code:
#include <vector>
#include <cmath>
#include <cassert>
using Matrix = std::vector<std::vector<float>>;
// compute sigma^2 that 'fit' the kernel half width
float compute_squared_variance(int half_width, float epsilon = 0.001)
{
assert(0 < epsilon && epsilon < 1); // small value required
return - (half_width + 1.0) * (half_width + 1.0) / 2.0 / std::log(epsilon);
}
float gaussian_exp(float y, float x, float sigma2)
{
assert(0 < sigma2);
return std::exp( - (x*x + y*y) / (2 * sigma2) );
}
// create a Gaussian kernel of size 2*half_width+1 x 2*half_width+1
Matrix make_gaussian_kernel(int half_width)
{
if(half_width <= 0)
{
// kernel of size 1 x 1
Matrix kernel(1, std::vector<float>(1, 1.0));
return kernel;
}
Matrix kernel(2*half_width+1, std::vector<float>(2*half_width+1, 0.0));
const float sigma2 = compute_squared_variance(half_width, 0.1);
float sum = 0;
for(int di = -half_width; di <= +half_width; ++di)
{
const int i = half_width + di;
for(int dj = -half_width; dj <= +half_width; ++dj)
{
const int j = half_width + dj;
kernel[i][j] = gaussian_exp(di, dj, sigma2);
sum += kernel[i][j];
}
}
assert(0 < sum);
// normalize
for(int i=0; i<2*half_width+1; ++i)
{
for(int j=0; j<2*half_width+1; ++j)
{
kernel[i][j] /= sum;
}
}
return kernel;
}
I'm starting with my c++ threads and don't understand some basic stuff. That's Mandelbrot example, it generates fractal image.
It's not my code, I just did some changes (here's original: https://rosettacode.org/wiki/Mandelbrot_set#PPM_non_interactive)
I have this function which generates matrix with colors to save to file:
vector<unsigned char *> drawMandelbrot()
{
/* screen ( integer) coordinate */
int iX, iY;
double Cx, Cy;
const double CxMin = -2.5;
const double CxMax = 1.5;
const double CyMin = -2.0;
const double CyMax = 2.0;
double PixelWidth = (CxMax - CxMin) / iXmax;
double PixelHeight = (CyMax - CyMin) / iYmax;
int Index = 0;
const int IterationMax = 200;
unsigned char color[3];
vector<unsigned char *> rows(MaxIndex);
double Zx, Zy;
double Zx2, Zy2;
int Iteration;
const double EscapeRadius = 2;
double ER2 = EscapeRadius * EscapeRadius;
for (iY = 0; iY < iYmax; iY++)
{
Cy = CyMin + iY * PixelHeight;
if (fabs(Cy) < PixelHeight / 2)
Cy = 0.0; /* Main antenna */
for (iX = 0; iX < iXmax; iX++)
{
Cx = CxMin + iX * PixelWidth;
/* initial value of orbit = critical point Z= 0 */
Zx = 0.0;
Zy = 0.0;
Zx2 = Zx * Zx;
Zy2 = Zy * Zy;
/* */
for (Iteration = 0; Iteration < IterationMax && ((Zx2 + Zy2) < ER2); Iteration++)
{
Zy = 2 * Zx * Zy + Cy;
Zx = Zx2 - Zy2 + Cx;
Zx2 = Zx * Zx;
Zy2 = Zy * Zy;
};
/* compute pixel color (24 bit = 3 bytes) */
if (Iteration == IterationMax)
{ /* interior of Mandelbrot set = black */
color[0] = 0;
color[1] = 0;
color[2] = 0;
}
else
{ /* exterior of Mandelbrot set = white */
color[0] = 255; /* Red*/
color[1] = 255; /* Green */
color[2] = 255; /* Blue */
};
rows[Index] = color;
Index++;
}
}
return rows;
}
Here is function to save it to file:
void saveToFile(vector<unsigned char *> matrix, char *filename)
{
char *comment = (char *)"# "; /* comment should start with # */
FILE *file;
file = fopen(filename, "wb"); /* b - binary mode */
fprintf(file, "P6\n %s\n %d\n %d\n %d\n", comment, iXmax, iYmax, MaxColorComponentValue);
for (int Index = 0; Index < MaxIndex; Index++)
{
fwrite(matrix[Index], 1, 3, file);
}
fclose(file);
}
Some global values and main loop:
const int iXmax = 1000;
const int iYmax = 1000;
const int MaxColorComponentValue = 255;
int const MaxIndex = (iXmax * iYmax) - 1;
int main()
{
clock_t start = clock();
vector<unsigned char *> image = drawMandelbrot();
clock_t stop = clock();
cout << (double(stop - start) / CLOCKS_PER_SEC) << " seconds\n";
char *filename = (char *)"new2.ppm";
saveToFile(image,filename);
return 0;
}
Problem is that generateMandelbrot() returns matrix like this:
image matrix
but it should be vector of elements looks like this which is actually color value:
color char
I know the problems is with color and image values types, but have any idea how it should look like.
Thanks!
This:
rows[Index] = color;
Is assigning the unsigned char * in your vector to the same array every time!
In other words it's like if I sell you ten cars and deliver the keys but they are all identical keys to the same car. Wouldn't you be upset?
Change your variables to use std::array:
using Color = std::array<unsigned char, 3>;
Color color;
vector<Color> rows(MaxIndex);
Now you have a vector of triples (Colors), instead of a vector of pointers that all point at the same triple.
I would like to get average contrast value of an image and I found working code in processing .I tried to apply this in qt-creator as in c++, however when I try to run my code I get this error:
cannot convert ‘cv::Mat’ to ‘uint8_t* {aka unsigned char*}’ in initialization
uint8_t* rowPtr = foo.row(i);<
In these parts:
uint8_t* rowPtr = foo.row(i);>
Here is the code I am trying to run;
bool normalizeRange = true; // normalize results to range of 0-1
float contrast = 0;
float brightness =0;
Mat foo = imread("1.png");
int cn = foo.channels();
Scalar_<uint8_t> bgrPixel;
for(int i = 0; i < foo.rows; i++)
{
uint8_t* rowPtr = foo.row(i);
for(int j = 0; j < foo.cols; j++)
{
bgrPixel.val[0] = rowPtr[j*cn + 0]; // B
bgrPixel.val[1] = rowPtr[j*cn + 1]; // G
bgrPixel.val[2] = rowPtr[j*cn + 2]; // R
// do something with BGR values...
}
}
brightness += (0.2126 * bgrPixel.val[2]) + (0.7152 * bgrPixel.val[1]) + (0.0722 * bgrPixel.val[0]); // scales RGB to perceived brightness
if (normalizeRange) {
brightness /= 255.0; // normalize to 0-1
}
brightness /= sizeof(bgrPixel); // average result
qDebug() << "Average brightness:" << brightness;
// find contrast by comparing average brightness with current value
for(int i = 0; i < foo.rows; i++)
{
uint8_t* rowPtr = foo.row(i);
for(int j = 0; j < foo.cols; j++)
{
bgrPixel.val[0] = rowPtr[j*cn + 0]; // B
bgrPixel.val[1] = rowPtr[j*cn + 1]; // G
bgrPixel.val[2] = rowPtr[j*cn + 2]; // R
// do something with BGR values...
}
}
float pxIntensity = (0.2126 * bgrPixel.val[2]) + (0.7152 * bgrPixel.val[1]) + (0.0722 * bgrPixel.val[2]);
if (normalizeRange) {
pxIntensity /= 255.0; // normalizes to range 0-1
}
contrast += pow((brightness - pxIntensity), 2);
contrast /= sizeof(bgrPixel);
qDebug() << "Average cotrast:" << contrast;
}
Update: I have added the java code which I would like to run in C++
boolean normalizeRange = true; // normalize results to range of 0-1
PImage img;
float brightness = 0;
float contrast = 0;
void setup() {
img = loadImage(filename);
size(img.width, img.height);
image(img, 0,0);
loadPixels(); // load pixels into array, iterate!
// find average brightness across image
for (color c : pixels) {
float r = c >> 16 & 0xFF; // extract RGB values quickly (better than red(), etc)
float g = c >> 8 & 0xFF;
float b = c & 0xFF;
brightness += (0.2126 * r) + (0.7152 * g) + (0.0722 * b); // scales RGB to perceived brightness
if (normalizeRange) {
brightness /= 255.0; // normalize to 0-1
} }
brightness /= pixels.length; // average result
println("Average brightness: " + brightness);
// find contrast by comparing average brightness with current value
for (color c : pixels) {
float r = c >> 16 & 0xFF;
float g = c >> 8 & 0xFF;
float b = c & 0xFF;
float pxIntensity = (0.2126 * r) + (0.7152 * g) + (0.0722 * b);
if (normalizeRange) {
pxIntensity /= 255.0; // normalizes to range 0-1 }
contrast += pow((brightness - pxIntensity), 2);}
contrast /= pixels.length;
println("Average contrast: " + contrast);
}
Use cv::Mat::ptr() to obtain a pointer to the specified matrix row buffer.
cv::Mat::row() constructs a new cv::Mat header for submatrix that consists of that single row instead.
Iterating raw cv::Mat buffer may be more efficient but less readable way to iterate elements. Read about different ways and their comparison here.
I wrote the code in C++ which solves the time-dependent 1D Schrodinger equation for the anharmonic potential V = x^2/2 + lambda*x^4, using Thomas algorithm. My code is working and I animate the results in Mathematica, to check what is going on. I test the code against the known solution for the harmonic potential (I put lambda = 0), but the animation shows that abs(Psi) is changing with time, and I know that is not correct for the harmonic potential. Actually, I see that in one point it time it becomes constant, but before that is oscillating.
So I understand that I need to have constant magnitude of the wave function over the time interval, but I don't know how to do it, or where am I doing mistake.
Here is my code and the animation for 100 time steps and 100 points on the grid.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <complex>
#include <fstream>
using namespace std;
// Mandatory parameters
const int L = 1; //length of domain in x direction
const int tmax = 10; //end time
const int nx = 100, nt = 100; //number of the grid points and time steps respectively
double lambda; //dictates the shape of the potential (we can use lambda = 0.0
// to test the code against the known solution for the harmonic
// oscillator)
complex<double> I(0.0, 1.0); //imaginary unit
// Derived parameters
double delta_x = 1. / (nx - 1);
//spacing between the grid points
double delta_t = 1. / (nt - 1);
//the time step
double r = delta_t / (delta_x * delta_x); //used to simplify expressions for
// the coefficients of the lhs and
// rhs of the matrix eqn
// Algorithm for solving the tridiagonal matrix system
vector<complex<double> > thomas_algorithm(vector<double>& a,
vector<complex<double> >& b,
vector<double>& c,
vector<complex<double> >& d)
{
// Temporary wave function
vector<complex<double> > y(nx + 1, 0.0);
// Modified matrix coefficients
vector<complex<double> > c_prime(nx + 1, 0.0);
vector<complex<double> > d_prime(nx + 1, 0.0);
// This updates the coefficients in the first row
c_prime[0] = c[0] / b[0];
d_prime[0] = d[0] / b[0];
// Create the c_prime and d_prime coefficients in the forward sweep
for (int i = 1; i < nx + 1; i++)
{
complex<double> m = 1.0 / (b[i] - a[i] * c_prime[i - 1]);
c_prime[i] = c[i] * m;
d_prime[i] = (d[i] - a[i] * d_prime[i - 1]) * m;
}
// This gives the value of the last equation in the system
y[nx] = d_prime[nx];
// This is the reverse sweep, used to update the solution vector
for (int i = nx - 1; i > 0; i--)
{
y[i] = d_prime[i] - c_prime[i] * y[i + 1];
}
return y;
}
void calc()
{
// First create the vectors to store the coefficients
vector<double> a(nx + 1, 1.0);
vector<complex<double> > b(nx + 1, 0.0);
vector<double> c(nx + 1, 1.0);
vector<complex<double> > d(nx + 1, 0.0);
vector<complex<double> > psi(nx + 1, 0.0);
vector<complex<double> > phi(nx + 1, 0.0);
vector<double> V(nx + 1, 0.0);
vector<double> x(nx + 1, 0);
vector<vector<complex<double> > > PSI(nt + 1,
vector<complex<double> >(nx + 1,
0.0));
vector<double> prob(nx + 1, 0);
// We don't have the first member of the left diagonal and the last member
// of the right diagonal
a[0] = 0.0;
c[nx] = 0.0;
for (int i = 0; i < nx + 1; i++)
{
x[i] = (-nx / 2) + i; // Values on the x axis
// Eigenfunction of the harmonic oscillator in the ground state
phi[i] = exp(-pow(x[i] * delta_x, 2) / 2) / (pow(M_PI, 0.25));
// Anharmonic potential
V[i] = pow(x[i] * delta_x, 2) / 2 + lambda * pow(x[i] * delta_x, 4);
// The main diagonal coefficients
b[i] = 2.0 * I / r - 2.0 + V[i] * delta_x * delta_x;
}
double sum0 = 0.0;
for (int i = 0; i < nx + 1; i++)
{
PSI[0][i] = phi[i]; // Initial condition for the wave function
sum0 += abs(pow(PSI[0][i], 2)); // Needed for the normalization
}
sum0 = sum0 * delta_x;
for (int i = 0; i < nx + 1; i++)
{
PSI[0][i] = PSI[0][i] / sqrt(sum0); // Normalization of the initial
// wave function
}
for (int j = 0; j < nt; j++)
{
PSI[j][0] = 0.0;
PSI[j][nx] = 0.0; // Boundary conditions for the wave function
d[0] = 0.0;
d[nx] = 0.0; // Boundary conditions for the rhs
// Fill in the current time step vector d representing the rhs
for (int i = 1; i < nx + 1; i++)
{
d[i] = PSI[j][i + 1]
+ (2.0 - 2.0 * I / r - V[i] * delta_x * delta_x) * PSI[j][i]
+ PSI[j][i - 1];
}
// Now solve the tridiagonal system
psi = thomas_algorithm(a, b, c, d);
for (int i = 1; i < nx; i++)
{
PSI[j + 1][i] = psi[i]; // Assign values to the wave function
}
for (int i = 0; i < nx + 1; i++)
{
// Probability density of the wave function in the next time step
prob[i] = abs(PSI[j + 1][i] * conj(PSI[j + 1][i]));
}
double sum = 0.0;
for (int i = 0; i < nx + 1; i++)
{
sum += prob[i] * delta_x;
}
for (int i = 0; i < nx + 1; i++)
{
// Normalization of the wave function in the next time step
PSI[j + 1][i] /= sqrt(sum);
}
}
// Opening files for writing the results
ofstream file_psi_re, file_psi_imag, file_psi_abs, file_potential,
file_phi0;
file_psi_re.open("psi_re.dat");
file_psi_imag.open("psi_imag.dat");
file_psi_abs.open("psi_abs.dat");
for (int i = 0; i < nx + 1; i++)
{
file_psi_re << fixed << x[i] << " ";
file_psi_imag << fixed << x[i] << " ";
file_psi_abs << fixed << x[i] << " ";
for (int j = 0; j < nt + 1; j++)
{
file_psi_re << fixed << setprecision(6) << PSI[j][i].real() << " ";
file_psi_imag << fixed << setprecision(6) << PSI[j][i].imag()
<< " ";
file_psi_abs << fixed << setprecision(6) << abs(PSI[j][i]) << " ";
}
file_psi_re << endl;
file_psi_imag << endl;
file_psi_abs << endl;
}
}
int main(int argc, char **argv)
{
calc();
return 0;
}
The black line is abs(psi), the red one is Im(psi) and the blue one is Re(psi).
(Bear in mind that my computational physics course was ten years ago now)
You say you are solving a time-dependent system, but I don't see any time-dependence (even if lambda != 0). In the Schrodinger Equation, if the potential function does not depend on time then the different equation is called separable because you can solve the time component and spatial component of the differential equation separately.
The general solution in that case is just the solution to the time-independent Schrodinger Equation multiplied by exp(-iE/h_bar). When you plot the magnitude of the probability that term just becomes 1 and so the probability doesn't change over time. In these cases people quite typically just ignore the time component altogether.
All this is to say that since your potential function doesn't depend on time then you aren't solving a time-dependent Schrodinger Equation. The Tridiagonal Matrix Algorithm can only be used to solve ordinary differential equations, whereas if your potential depended on time you would have a partial differential equation and would need a different method to solve it. Also as a result of that plotting the probability density over time is rarely interesting.
As for why your potential is not constant, numerical methods for finding eigenvalues and eigenvectors rarely produce the normalised eigenvectors naturally, so are you manually normalising your eigenvector before computing your probabilities?
I am trying to run an oscillator and storing its fourier spectrum values, with high precision using Quad math in C++. I am able to compute the high precision value but I am not able to save it to a file as a quad precise value.
It gives me an error as:
error: cannot convert âstd::complex<__complex__ __float128>â to â__complex128 {aka __complex__ __float128}â for argument â1â to â__float128 cabsq(__complex128)â
My code is:
//Fourier transform
int size_dft=size_org;
int size_dfty=2e5;
int increment=0;
int initial_size_dft=0;
double pi2 = -2.0 * M_PI;
double angleTerm,cosineA,sineA;
int N_dft= 1e3;
double y_dft_deeper=0;
double invs = 1.0 / N_dft;
std::vector< std::complex< __complex128 > > output_seq(size_dft);
//std::complex<double> output_seq[size_dft];
for( int y = initial_size_dft;y < size_dfty;y++)
{
output_seq[y] = 0;
y_dft_deeper =2.4316321+(0.0000001*y);
if(y_dft_deeper<2.4318321)
{
int first_1 = 0;
increment = first_1;
for(unsigned int x =5786;x <end_dft;x++)
{
angleTerm = pi2 * y_dft_deeper * x * invs;
cosineA = cosq(angleTerm);
sineA = sinq(angleTerm);
std::real(output_seq[y]) += V2[x] * cosineA ;
std::imag(output_seq[y]) += V2[x] * sineA;
}
output_seq[y] *= invs;
cout<<"iteration = "<<y;//<<" DFT = "<< output_seq[y]<<"\n";
y=y+increment;
}
//Writing data to file
ofstream myfile_dft;
myfile_dft.open ("aug_colpits_deep_1e8_first20000_quadmath.txt");
for (int i = initial_size_dft; i < size_dfty; i++)
{
if (i<2000)
{
increment=0;
myfile_dft << cabsq(output_seq[i]) <<"\n";
i=i+increment;
}
}
myfile_dft.close();
`