Matlab data interpolation - matlab

I have a doubt regarding the function interp1 in matlab.
I have an X array of 248 elements with its corresponding Y array. I would like to build a correspondence between the values on the array X and a custom array of gray scale values,e.g. linspace(5,250,248). I don't want to take the value 0 and 255. If i define that max(X) has the value 250 and the min(X) as the value 0, which is the corresponding gray scale value of a generic x_i?
I though to use interp1 but my code is not working. Some suggestion?
Thank you in advance

I = mat2gray(A,[amin amax]) converts the matrix A to an intensity image I that contains values in the range 0 (black) to 1 (white). amin and amax are the values in A that correspond to 0 and 1 in I. Values less than amin become 0, and values greater than amax become 1.

Related

Finding the maximum of a pixel in an RGB image

I have a MxNx3 matrix that represents an RGB image. I am trying to retrieve, for each pixel, the maximum among R, G and B. This would be made easy by using a for loop, which I do not wish to do for performance reasons.
How could I go about doing that? My idea is to use find and max in the following way and get an MxN matrix:
maxRGB = find(max(rgbImage(i, j, :)));
But I am not sure how I could eliminate i and j.
The max function allows to specify along which dimension the maximum value is determined. The standard value is the first dimension. In your case, you'll want to calculate the maximum along the third dimension of the array:
maxValue = max(rgbImage,[],3);
Which returns a matrix of size MxN containing the maximum value of each pixel.
For example, lets take a random 3x3 RGB image. Applying the max function as above yields
rgbImage = rand(3,3,3);
maxValue = max(rgbImage,[],3);
maxValue =
0.6948 0.7094 0.7655
0.6555 0.7547 0.7952
0.9502 0.3816 0.8235
These are the maximal values which were present in rgbImage at each pixel location. But, you don't know if this value was in the R, G or B pixel.
To find out, which color was maximal, you can use the second (optional) argument of max, which is the index of the found maximum:
[~,maxIndex] = max(rgbImage,[],3);
which in this small example was
maxIndex =
2 3 2
1 3 2
2 2 1
where 1 corresponds to R, 2 corresponds to G and 3 corresponds to B.
To find all pixels, in which the red component was the largest, you can use the find function (probably with 2 output arguments: row and column)
[xRed,yRed] = find(maxIndex == 1)
xRed =
2
3
yRed =
1
3
So for the pixels at (2,1) and at (3,3) the red component was the largest. This is exactly what the matrix maxIndex also shows us.

conversion between RGB and HSV space

I am currently trying to convert two objects from RGB space to HSV space using the rgb2hsv function, one being an image, the other being a matrix of RGB values, but my results for the value part of the matrix are not consistent.
The results for value in matrix I are between 1 and 255, but in the second matrix D, they are between 0 and 1. Does anybody know why this is the case?
I = imread('00000001.jpg');
I = rgb2hsv(I);
D = [221 12 26; 30 68 76; 40 47 27; 165 87 25; 37 59 26; 148 125 91];
D = rgb2hsv(D);
When you call rgb2hsv with a N-by-3 matrix, it is interpreted as a colormap, not an image or even image intensities. A colormap has values on [0,1], while a uint8 image has values on [0,255]. This is what rgb2hsv expects.
The colormap syntax explained by the help page for rgb2hsv:
H = rgb2hsv(M) converts an RGB color map to an HSV color map.
Each map is a matrix with any number of rows, exactly three columns,
and elements in the interval 0 to 1.
When you run D = rgb2hsv(D); it is running it with the above syntax, rather than treating the input as an image.
That said, you can just divide the third column of the output D by 255 since the resulting bizarro colormap seems to simply have scaled value elements.
I simply multiplied by 255 on the resulting matrix from running rgb2hsv on an image. I verified that the hue, saturation, and value were then the correct value and it was. This solved my problem. Be sure to verify. Another weird thing was that those values are a percentage, so 1 means 100% and 0.5 means 50%. This also threw me off.

Image matrix operation in Matlab

I have save a image in a matrix like this:
image1=imread('abcn.tif');
nfilas= tamanio(1);
ncols= tamanio(2);
nbandas= tamanio(3);
imagenn = zeros(nfilas, ncols, nbandas);
And my result is that:
Name Size Bytes Class Attributes
imagenn 4x4x3 96 uint16
And now, I want to graph the value of the same pĂ­xel on the three bands. I want to get the value of the first position (1,1), for example, and graph it. How can I indicate the position with a matrix?
Thanks in advance,
The matrix imagenn is square (4x4) and has 3 "layers" (R, G and B?). So, to get a pixel P on each "layer" you have to write P(1,1,1), P(1,1,2) and P(1,1,3). Note that Matlab's indexes start from 1.
You will have to plot vector P(1,1,:)

The use of graycomatrix offset parameter

I came across this example on how to call the function graycomatrix
>I = imread('circuit.tif');
>GLCM2 = graycomatrix(I,'Offset',[2 0;0 2]);
>stats = GLCM_features1(GLCM2,0)
but I do not understand the effect of the second parameter in graycomatrix. I've read Matlab documentation but the explanation is difficult to understand.
The second and third parameters to graycomatrix are a combined name-value pair. The value ([2 0; 0 2]) in this case is a matrix which determines the relative spacing (i.e. offset, the name of the parameter) of pixels which are examined in order to get the co-occurrence count of each value pair.
Each row in this p-by-2 matrix defines a single relative position in [row column] format. The first row of the matrix is [2 0]. This means that each pixel is compared to the pixel 2 rows down, 0 columns over (i.e. in the same column). The second row [0 2] indicates that each pixel is also compared to the pixel 0 rows away (i.e. in the same row) and 2 columns over.
Thus, each pixel is compared against two of its neighbor pixels: the pixel 2 columns to the right, and the pixel 2 rows down. The pairs formed by both of these relationships are used to increment the appropriate pixels in the output image.

Generate Color Plot

There is an object on an area with dimension M*M unit cells. The coverage rate C=1/M * Sum(i=1 to M J(i)) where J(i)=1 when the cell i is covered and 0 otherwise. This is a color scale map representing the visit of the cells vs the times of visiting by the object. So, the legend shows that there are cells which have been visited from 0 to 8 times in N number of iterations. Can anyone tell me how this color representation can be coded? What and how this can be generated?
Use image (or imagesc). You need a matrix of X values and Y values, and the corresponding matrix of Z values.
For example:
% generate some x,y
[x,y]=meshgrid(1:10,1:10);
% generate some z values: random numbers from 0 to 8
z = randi([0 8],size(x));
% plot
imagesc(x,y,z)
How you determine your x, y, z ... well, you'd have to provide more information.
You could use a two dimensional array and read the information in from a file.