How do I write a 3D histogram code without the Matlab built in functions? - matlab

I want to creat a histogram code, knowing that it'll be counting the number of occurence of 3 values of a pixel.
The idea is I have 3 matrices (L1im, L2im, L3im) representing information extracted from an image, size of each of them is 256*226, and I want to compute how many times a combination of let's say (52,6,40) occurs (each number correspends to a matrix/component but they're all of the same pixel).
I have tried this, but it doesn’t produce the right result:
for i = 1 : 256
for j = 1 : 256
for k = 1 : 256
if (L1im == i) & (L2im == j) & (L3im == k)
myhist(i,j,k)= myhist(i,j,k)+1;
end
end
end
end

Colour Triplets Histogram
Keeping in mind doing an entire RGB triplet histogram is a large task since you can have 256 × 256 × 256 = 16,777,216 combinations of possible unique colours. A slightly more manageable task I believe is to compute the histogram for the unique RGB values in the image (since the rest will be zero anyways). This is still a fairly large task but might be reasonable if the image is fairly small. Below I believe a decent alternative to binning is to resize the image to a smaller number of pixels. This can be done by using the imresize function. I believe this will decrease fidelity of the image and almost act as a rounding function which can "kinda" simulate the behaviour of binning. In this example I convert the matrices string arrays an concatenate the channels, L1im, L2im and L3im of the image. Below is a demo where I use the image built into MATLAB named saturn.png. A Resize_Factor
of 1 will result in the highest amount of bins and the number of bins will decrease as the Resize_Factor increases. Keep in mind that the histogram might require scaling if the image is being resized with the Resize_Factor.
Resize_Factor = 200;
RGB_Image = imread("saturn.png");
[Image_Height,Image_Width,Number_Of_Colour_Channels] = size(RGB_Image);
Number_Of_Pixels = Image_Height*Image_Width;
RGB_Image = imresize(RGB_Image,[Image_Height/Resize_Factor Image_Width/Resize_Factor]);
L1im = RGB_Image(:,:,1);
L2im = RGB_Image(:,:,2);
L3im = RGB_Image(:,:,3);
L1im_String = string(L1im);
L2im_String = string(L2im);
L3im_String = string(L3im);
RGB_Triplets = L1im_String + "," + L2im_String + "," + L3im_String;
Unique_RGB_Triplets = unique(RGB_Triplets);
for Colour_Index = 1: length(Unique_RGB_Triplets)
RGB_Colour = Unique_RGB_Triplets(Colour_Index);
Unique_RGB_Triplets(Colour_Index,2) = nnz(RGB_Colour == RGB_Triplets);
end
Counts = str2double(Unique_RGB_Triplets(:,2));
Scaling_Factor = Number_Of_Pixels/sum(Counts);
Counts = Counts.*Scaling_Factor;
if sum(Counts) == Number_Of_Pixels
disp("Sum of histogram is equal to the number of pixels");
end
bar(Counts);
title("RGB Triplet Histogram");
xlabel("RGB Triplets"); ylabel("Counts");
Current_Axis = gca;
Scale = (1:length(Unique_RGB_Triplets));
set(Current_Axis,'xtick',Scale,'xticklabel',Unique_RGB_Triplets);
Angle = 90;
xtickangle(Current_Axis,Angle);

Related

N-dimensional GP Regression

I'm trying to use GPflow for a multidimensional regression. But I'm confused by the shapes of the mean and variance.
For example: A 2-dimensional input space X of shape (20,20) is supposed to be predicted. My training samples are of shape (8,2) which means 8 training samples overall for the two dimensions. The y-values are of shape (8,1) which of course means one value of the ground truth per combination of the 2 input dimensions.
If I now use model.predict_y(X) I would expect to receive a mean of shape (20,20) but obtain a shape of (20,1). Same goes for the variance. I think that this problem comes from the shape of the y-values but I have have no idea how to fix it.
bound = 3
num = 20
X = np.random.uniform(-bound, bound, (num,num))
print(X_sample.shape) # (8,2)
print(Y_sample.shape) # (8,1)
k = gpflow.kernels.RBF(input_dim=2)
m = gpflow.models.GPR(X_sample, Y_sample, kern=k)
m.likelihood.variance = sigma_n
m.compile()
gpflow.train.ScipyOptimizer().minimize(m)
mean, var = m.predict_y(X)
print(mean.shape) # (20, 1)
print(var.shape) # (20, 1)
It sounds like you may be confused between the shape of a grid of input positions and the shape of the numpy arrays: if you want to predict on a 20 x 20 grid in two dimensions, you have 400 points in total, each with 2 values. So X (the one that you pass to m.predict_y()) should have shape (400, 2). (Note that the second dimension needs to have the same shape as X_sample!)
To construct this array of shape (400,2) you can use np.meshgrid (e.g., see What is the purpose of meshgrid in Python / NumPy?).
m.predict_y(X) only predicts the marginal variance at each test point, so the returned mean and var both have shape (400,1) (same length as X). You can of course reshape them to the 20 x 20 values on your grid.
(It is also possible to compute the full covariance, for the latent f this is implemented as m.predict_f_full_cov, which for X of shape (400,2) would return a 400x400 matrix. This is relevant if you want consistent samples from the GP, but I suspect that goes well beyond this question.)
I was indeed making the mistake to not flatten the arrays which in return produced the mistake. Thank you for the fast response STJ!
Here is an example of the working code:
# Generate data
bound = 3.
x1 = np.linspace(-bound, bound, num)
x2 = np.linspace(-bound, bound, num)
x1_mesh,x2_mesh = np.meshgrid(x1, x2)
X = np.dstack([x1_mesh, x2_mesh]).reshape(-1, 2)
z = f(x1_mesh, x2_mesh) # evaluation of the function on the grid
# Draw samples from feature vectors and function by a given index
size = 2
np.random.seed(1991)
index = np.random.choice(range(len(x1)), size=(size,X.ndim), replace=False)
samples = utils.sampleFeature([x1,x2], index)
X1_sample = samples[0]
X2_sample = samples[1]
X_sample = np.column_stack((X1_sample, X2_sample))
Y_sample = utils.samplefromFunc(f=z, ind=index)
# Change noise parameter
sigma_n = 0.0
# Construct models with initial guess
k = gpflow.kernels.RBF(2,active_dims=[0,1], lengthscales=1.0,ARD=True)
m = gpflow.models.GPR(X_sample, Y_sample, kern=k)
m.likelihood.variance = sigma_n
m.compile()
#print(X.shape)
mean, var = m.predict_y(X)
mean_square = mean.reshape(x1_mesh.shape) # Shape: (num,num)
var_square = var.reshape(x1_mesh.shape) # Shape: (num,num)
# Plot mean
fig = plt.figure(figsize=(16, 12))
ax = plt.axes(projection='3d')
ax.plot_surface(x1_mesh, x2_mesh, mean_square, cmap=cm.viridis, linewidth=0.5, antialiased=True, alpha=0.8)
cbar = ax.contourf(x1_mesh, x2_mesh, mean_square, zdir='z', offset=offset, cmap=cm.viridis, antialiased=True)
ax.scatter3D(X1_sample, X2_sample, offset, marker='o',edgecolors='k', color='r', s=150)
fig.colorbar(cbar)
for t in ax.zaxis.get_major_ticks(): t.label.set_fontsize(fontsize_ticks)
ax.set_title("$\mu(x_1,x_2)$", fontsize=fontsize_title)
ax.set_xlabel("\n$x_1$", fontsize=fontsize_label)
ax.set_ylabel("\n$x_2$", fontsize=fontsize_label)
ax.set_zlabel('\n\n$\mu(x_1,x_2)$', fontsize=fontsize_label)
plt.xticks(fontsize=fontsize_ticks)
plt.yticks(fontsize=fontsize_ticks)
plt.xlim(left=-bound, right=bound)
plt.ylim(bottom=-bound, top=bound)
ax.set_zlim3d(offset,np.max(z))
which leads to (red dots are the sample points drawn from the function). Note: Code not refactored what so ever :)

How to count the number of occurrences of pixel intensities in an image without using for loop?

I am to writing a script for histogram equalisation and I need to work on each RGB plane separately. In the first step I count the number of occurrences of each intensity value in the range 0-255. As far as I know, using for loops makes MATLAB code super slow. This is what I came up with :
org_image = imread('image.jpg')
tot_pixel = size(org_image,1) * size(org_image,2)
R = org_image(:,:,1);
G = org_image(:,:,2);
B = org_image(:,:,3);
[R_val_ocurr,R_unique_val] = histcounts(R);
[G_val_ocurr,G_unique_val] = histcounts(G);
[B_val_ocurr,B_unique_val] = histcounts(B);
Now to have an array of size 256,with each index holding number of pixels corresponding to it what should my next step be? I'm trying to write down my logic :
for i = 0 to 255
if i is in R_unique_val
hist[i] = R_val_ocurr[i]
else
hist[i] = 0
How to correctly and efficiently write this in MATLAB?
after you have separete the channel you can use imhist to get the histogram of each channel:
[NumberOfPixelR, intensity] = imhist(R);
[NumberOfPixelG, intensity] = imhist(G);
[NumberOfPixelB, intensity] = imhist(B);

Normalization of an image dataset after processing

Edited With More Clear Explanation
I am trying to normalize images in a dataset after processing them, but min, max, ranges change (for example one image is between the range [0.38,5.26] and another one is [-0.44, 3.65]) after the processing and normalizing them between [0,1] with the common normalization approach but it causes inconsistency between images.
imagesPath = '/home/berkanhoke/Datasets/Freiburg/Org/Night/';
outFolderPath = '/home/berkanhoke/Datasets/Freiburg/Maddern/Night';
imageSet = dir(strcat(imagesPath,'*.jpeg'));
imageCount = length(imageSet);
for i = 1:imageCount
fileName = imageSet(i).name;
filePath = strcat(imagesPath,fileName);
img = double(imread(filePath));
I_old = maddern(img,0.3975);
I_new = (I_old - min(I_old(:)))/(max(I_old(:)) - min(I_old(:)));
writePath = strcat(outFolderPath,fileName);
imwrite(I_new,writePath,'jpeg');
end
The function I use for processing is the following:
function [ ii_image ] = maddern( image, alpha )
ii_image = 0.5 + log(image(:,:,2)+1)...
- alpha * log(image(:,:,3)+1)...
- (1-alpha) * log(image(:,:,1)+1);
which is based on the paper: http://www.robots.ox.ac.uk/~mobile/Papers/2014ICRA_maddern.pdf
I tried normalized with respect to min/max of the whole dataset, but it did not work and I got weird results. How can I normalize the images by keeping the images consistent after processing?
The problem is that when you do min/max function in MATLAB, it only does it in one dimension. So if you have a 256x256 image, when you do min(image), you get a 1x256 vector. And when you divide this you are doing (256x256)/(1x256) = 256x1
To fix this, you'll want to do min(min(image))

How to create nonlinear spaced vector in Matlab?

I'm trying to create a contour plot with focus around a particular finite range from 1 to 1.05. At the same time, I need very high resolution closer to 1. I thought I could use something like the following but the spacing still looks linear
out=exp(linspace(log(1),log(1.05),100))
plot(diff(out))
What is the best way to enhance the nonlinearity of the spacing when the bounds are so tight? Again, I need to maintain high density near 1 with the resolution tapering off in a nonlinear way. I have a few ideas but I thought someone might have a quick 2 liner or something of the sort.
instead of applying the function f(x) = ex, to get a 'steeper' non-linearity, rather apply f(x) = eax
n = 20;
a = 100;
lower = 1;
upper = 1.05;
temp = exp(linspace(log(1)*a,log(1.05)*a,n))
% re-scale to be between 0 and 1
temp_01 = temp/max(temp) - min(temp)/max(temp)
% re-scale to be between your limits (i.e. 1 and 1.05)
out = temp_01*(upper-lower) + lower
now plot(diff(out),diff(out),'o') produces
Note that you can use the exact same scaling scheme above with logspace so just use
temp = logspace(...)
and then the rest is the same
You can generate a logarithmic distribution between, for example, 1 and 1000 and then scale it back to [1, 1.05]:
out = logspace(0, 3, 100);
out = ( (out-min(out(:)))*(1.05-1) ) / ( max(out(:))-min(out(:)) ) + 1;
Result:
plot(diff(out));

Oblique stripes resulting from bilinear interpolation

I'm trying to write a function in GNU Octave that does bilinear interpolation on a PGM image. The result isn't that great: I keep getting oblique stripes of different shades all over the image. Also, the rows and columns that are added during interpolation are darker than they should. Could someone help me by pointing out the problem, please?
function bilinear(img)
data = imread(img);
for n = 1 : 2 : (rows(data) - 1) * 2
average = average_vector(data(n, 1:end), data(n+1:1:end));
data = [data(1:n, 1:end); average; data(n+1:rows(data), 1:end)];
end
for n = 1 : 2 : (columns(data) - 1) * 2
average = average_vector(data(1:rows(data), n), data(1:rows(data), n+1));
data = [data(1:rows(data), 1:n) average data(1:rows(data), n+1:end)];
end
imwrite(data, strcat("out_bilinear_", img));
end
function res = average_vector(a, b)
res = zeros(size(a));
for n = 1 : length(a)
res(n) = (a(n) + b(n)) / 2;
end
end
Here's an image showing the problem:
You're iterating through the input image row-by-row (or column-by-column), but inserting new rows (or columns) as you go. I'm pretty sure this will screw your indexing up.
I would suggest creating a new output matrix, rather than modifying the original. This will be considerably faster, too.
Incidentally, your average_vector function can be written simply as res = (a + b) / 2;.