Get the matrix after imagesc? [duplicate] - matlab

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
imagesc plot to matrix in matlab
Scale Matrix to a new range
I have:
I = imread('image.tif');
At this point I can easly print the pixel with cord 100,100 by doing I(100,100)
Now I scale to image to fit the range 0.5...0.9
imagesc(I,[0.5 0.9]);
colormap('gray');
Is there any way to get the new matrix I ? (with pixel values from 0.5 to 0.9)
If i do
I = imagesc(I,[0.5 0.9]);
I only get the handler to image object

You can get the image data from an image plot with:
A = rand(100,100);
I = imagesc(A, [.5 .9]);
B = get(I, 'CData');
Predicting from your previous question: Scale Matrix to a new range I expect that B won't be you want. In fact B will be identical to A. This can be verified with:
all(all(A==B))
The second argument to imagesc doesn't scale the values in the provided matrix rather it scales the colormap.

Try the getimage command:
A = rand(100,100);
I = imagesc(A, [.5 .9]);
B = getimage(gca);

Related

How to find a brightest point an image and mark it in Matlab? [duplicate]

This question already has an answer here:
How to find and highlight the brightest region an image in Matlab?
(1 answer)
Closed 1 year ago.
Dears,
I would like to ask you for help with the code. My goal is to find the brightest point an image and mark it.
I used the following code to get the Image in Grey Values -> How to find and highlight the brightest region an image in Matlab? and tried to extend it based on my intention.
Then I found the max value of the matrix and go with the for loop function to highlight the coordinates of the max points. Unfortunately, here I have started struggle.
rgbImage = imread( 'Zoom1_WhiteImage.png' );
%imshow(rgbImage);
[rows, columns, numberOfColorChannels] = size(rgbImage)
if numberOfColorChannels == 1
brightness = rgbImage; % For a 1 channel image, brightness is the same as the original pixel value.
else
% For an RGB image, the brightness can be estimated in various ways, here's one standard formula:
brightness = (0.2126*rgbImage(:, :, 1) + 0.7152*rgbImage(:, :, 2) + 0.0722*rgbImage(:, :, 3));
end
% Establish a brightness threshold - pixels brighter than this value will
% be highlighted
threshold = 105;
% Create a zero-filled mask of equal size to the image to hold the
% information of which pixels met the brightness criterion:
mask = zeros(rows, columns, 'logical');
% Assign "1" to any mask pixels whose corresponding image pixel met the
% brightness criterion:
mask(brightness > threshold) = 1;
[a,b] = size(brightness)
maxgrey = max(max(brightness));
aux=0;
for i=1:1:a;
for j=1:1:b;
if brightness(a,b)>brightness(a,b+1)
aux=brightness(a,b+1)
else aux
end
end
end
Could you help me to finish the code, please?
You can achieve your goal with the function find() or with the function ind2sub():
% Random 2D matrix
I = rand(10);
% First option with find
[x,y] = find(I == max(I(:)))
% Second option using ind2sub, a bit more efficient since we only read I once.
[~,ind] = max(I(:))
[x,y] = ind2sub(size(I),ind)

How to draw a line between two coordinates of an image permanently in Matlab? [duplicate]

This question already has answers here:
MATLAB: Drawing a line over a black and white image
(5 answers)
Closed 4 years ago.
I have a set of points that I want to connect sequentially. Suppose the points are (A1,A2,A3,...A9); I want to connect A1 to A2, A2 to A3 and so on and finally connect A9 to A1.
All I need is to know a function that would help me connect A1 to A2, I could do the rest using for loops.
I know connecting two points is a question that has been asked here several times before but I couldn't find the answer I required. Several of the solutions suggest using "plot" and "line" but these functions overlay the results on the image and don't actually make any changes to the original image.
I did try them out and managed to save the resulting figure using the "saveas" and "print" functions but the image doesn't get saved in the proper format and there are a lot of problems using the parameters for these functions. Besides, I don't really want to save the image, it's just an unnecessary overhead I was willing to add if I could get the desired image with the lines.
I've also tried "imline" to draw lines but it seems to be interactive.
This particular question reflects my problem perfectly but when I ran the code snippets given as solutions, all of them gave a set of dots in the resulting image.
I tried the above mentioned codes in the link with this image that I found here.
A dotted line was an output for all three code snippets in the link above.
For example, I ran the first code like this:
I = imread('berries_copy.png');
grayImage=rgb2gray(I);
img =false(size(grayImage,1), size(grayImage,2));
I wrote the above piece of code just to get a black image for the following operations:
x = [500 470]; % x coordinates
y = [300 310]; % y coordinates
nPoints = max(abs(diff(x)), abs(diff(y)))+1; % Number of points in line
rIndex = round(linspace(y(1), y(2), nPoints)); % Row indices
cIndex = round(linspace(x(1), x(2), nPoints)); % Column indices
index = sub2ind(size(img), rIndex, cIndex); % Linear indices
img(index) = 255; % Set the line points to white
imshow(img); % Display the image
This is the resulting image for the above code as well as the other two, which as you can see, is just a few points on a black background which isn't the desired output.
I changed the code and used the "plot" function for the same to get this output which is what I want. Is there anyway I can change the dotted output into a solid line?
Or if could anyone suggest a simple function or a method that would draw a line from A1 to A2 and would actually make a change in the input image, I'd be grateful. (I really hope this is just me being a novice rather than Matlab not having a simple function to draw a line in an image.)
P.S. I don't have the Computer Vision toolbox and if possible, I'd like to find a solution that doesn't involve it.
Your first problem is that you are creating a blank image the same size as the image you load with this line:
img =false(size(grayImage,1), size(grayImage,2));
When you add the line to it, you get a black image with a white line on it, as expected.
Your second problem is that you are trying to apply a solution for grayscale intensity images to an RGB (Truecolor) image, which requires you to modify the data at the given indices for all three color planes (red, green, and blue). Here's how you can modify the grayscale solution from my other answer:
img = imread('berries_copy.png'); % Load image
[R, C, D] = size(img); % Get dimension sizes, D should be 3
x = [500 470]; % x coordinates
y = [300 310]; % y coordinates
nPoints = max(abs(diff(x)), abs(diff(y)))+1; % Number of points in line
rIndex = round(linspace(y(1), y(2), nPoints)); % Row indices
cIndex = round(linspace(x(1), x(2), nPoints)); % Column indices
index = sub2ind([R C], rIndex, cIndex); % Linear indices
img(index) = 255; % Modify red plane
img(index+R*C) = 255; % Modify green plane
img(index+2*R*C) = 255; % Modify blue plane
imshow(img); % Display image
imwrite(img, 'berries_line.png'); % Save image, if desired
And the resulting image (note the white line above the berry in the bottom right corner):
You can use the Bresenham Algorithm. Of course it has been implemented and you can find it here: Bresenham optimized for Matlab. This algorithm selects the pixels approximating a line.
A simple example, using your variable name could be:
I = rgb2gray(imread('peppers.png'));
A1 = [1 1];
A2 = [40 40];
[x y] = bresenham(A1(1),A1(2),A2(1),A2(2));
ind = sub2ind(size(I),x,y);
I(ind) = 255;
imshow(I)
You can use imshow to display a image and then use plot to plot lines and save the figure. Check the below code:
I = imread('peppers.png') ;
imshow(I)
hold on
[m,n,p] = size(I) ;
%% Get random points A1, A2,..A10
N = 9 ;
x = (n-1)*rand(1,N)+1 ;
y = (m-1)*rand(1,N)+1 ;
P = [x; y]; % coordinates / points
c = mean(P,2); % mean/ central point
d = P-c ; % vectors connecting the central point and the given points
th = atan2(d(2,:),d(1,:)); % angle above x axis
[th, idx] = sort(th); % sorting the angles
P = P(:,idx); % sorting the given points
P = [P P(:,1)]; % add the first at the end to close the polygon
plot( P(1,:), P(2,:), 'k');
saveas(gcf,'image.png')

MATLAB - How to measure mean intensity of a circular area in an image? [duplicate]

This question already has answers here:
mean value in a sphere
(3 answers)
Closed 5 years ago.
I want the mean intensity of an image, not the whole but only in a certain region of interest. This happens to be circular in shape and I know details of its radius and position.
Just inside the circular ROI (circle not drawn, using the radius and position). I dont want to mask the image and delete the outsides because that'll add unnecessary datapoints to the average.
You can use Boolean indexing
% creating the image:
[xx, yy] = meshgrid(1:100);
r = 23; x0 = 45; y0 = 67;
pixelsInSphere = ((xx-x0).^2 + (yy-y0).^2 < r);
im = pixelsInSphere.*(128+10*randn(size(yy)));
figure; imagesc(im);
% calculating the mean of the pixels using boolean indexing:
m = mean(im(pixelsInSphere));
You can create an Indicator function which given a position of pixel (i, j) it will say whether it is inside the ROI or not.
Loop over all the pixel in the image.
Sum the pixels which are in the ROI according to the indicator.

Creating a circle with a gradient [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
In a different thread, I found this bit of code to make a circle that has a gradient in it (plot circle with gradient gray scale color in matlab):
N = 200; %// this decides the size of image
[X,Y] = meshgrid(-1:1/N:1, -1:1/N:1) ;
nrm = sqrt(X.^2 + Y.^2);
out = uint8(255*(nrm/min(nrm(:,1)))); %// output image
padsize = 50; %// decides the boundary width
out = padarray(out,[padsize padsize],0);
figure, imshow(out) %// show image
Now I would like to replace that gradient with a fixed vector of decreasing values, so that every radius has it's own value.
Thanks in advance for any help on this
Here is an elegant way to replace elements with values from a vector:
Assume your vector is: V = 283:-1:0.
I used descending values for demonstration.
The value 283 is sqrt(2)*N (supposed to be the maximum radius in the bounding square).
Code differences from your original post:
Divide out by max(out(:)) - set the range of out to [0, 1].
Multiply by length of V (minus 1) - set the range of out to [0, length(V)-1].
Use round instead of uint8 (converting to uint8 clamps value to 255).
Use vector V as a Look Up Table - replace each elements of out with value of V in place of the value.
Here is the code:
N = 200; %// this decides the size of image
%V = round(sqrt(2)*N):-1:0;
%Assume this is your vector.
V = 283:-1:0;
[X,Y] = meshgrid(-1:1/N:1, -1:1/N:1) ;
nrm = sqrt(X.^2 + Y.^2);
%out = uint8(255*(nrm/min(nrm(:,1)))); %// output image
%1. Divide by max(out(:)) - set the range of out to [0, 1].
%2. Multiply by length of V (minus 1) - set the range of out to [0, length(V)-1].
%3. Use round instead of uint8 (converting to uint8 clamps value to 255).
out = nrm/min(nrm(:,1));
out = round(out/max(out(:)) * (length(V)-1));
%4. Use vector V as a Look Up Table - replace each elements of out with value of V in place of the value.
out = V(out+1);
padsize = 50; %// decides the boundary width
out = padarray(out,[padsize padsize],0);
%figure, imshow(out) %// show image
figure, imagesc(out);impixelinfo;colormap hsv %// use imagesc for emphasis values.
As you can see, values are taken from vector V.
Try
R = sqrt(X.^2+Y.^2);
out = uint8(255*R);
padsize = 50; %// decides the bounary width
out = padarray(out,[padsize padsize],0);
figure, imshow(out) %// show image

Matlab - Trying to use vectors with grid coordinates and value at each point for a color plot

I'm trying to make a color plot in matlab using output data from another program. What I have are 3 vectors indicating the x-position, y-yposition (both in milliarcseconds, since this represents an image of the surroundings of a black hole), and value (which will be assigned a color) of every point in the desired image. I apparently can't use pcolor, because the values which indicate the color of each "pixel" are not in a matrix, and I don't know a way other than meshgrid to create a matrix out of the vectors, which didn't work due to the size of the vectors.
Thanks in advance for any help, I may not be able to reply immediately.
If we make no assumptions about the arrangement of the x,y coordinates (i.e. non-monotonic) and the sparsity of the data samples, the best way to get a nice image out of your vectors is to use TriScatteredInterp. Here is an example:
% samplesToGrid.m
function [vi,xi,yi] = samplesToGrid(x,y,v)
F = TriScatteredInterp(x,y,v);
[yi,xi] = ndgrid(min(y(:)):max(y(:)), min(x(:)):max(x(:)));
vi = F(xi,yi);
Here's an example of taking 500 "pixel" samples on a 100x100 grid and building a full image:
% exampleSparsePeakSamples.m
x = randi(100,[500 1]); y = randi(100,[500 1]);
v = exp(-(x-50).^2/50) .* exp(-(y-50).^2/50) + 1e-2*randn(size(x));
vi = samplesToGrid(x,y,v);
imagesc(vi); axis image
Gordon's answer will work if the coordinates are integer-valued, but the image will be spare.
You can assign your values to a matrix based on the x and y coordinates and then use imagesc (or a similar function).
% Assuming the X and Y coords start at 1
max_x = max(Xcoords);
max_y = max(Ycoords);
data = nan(max_y, max_x); % Note the order of y and x
indexes = sub2ind(size(data), max_y, max_x);
data(indexes) = Values;
imagesc(data); % note that NaN values will be colored with the minimum colormap value