How I create a bitmap from a matrix with matlab - matlab

I want to create a bitmap with MATLAB. It is juste a white image with a red rectangle in a very specific location as you can see in the code. In fact I get the red rectangle but I didn't find out how to obtain the White color outside the rectangle.
x=0:9;
y=0:17;
matrice=zeros(17,9,'uint8');
for i =1 :length(x)
for j= 1 :length(y)
if (i>=3) && (i<=6)&&(j>=2) &&(j<=16)
matrice(j,i)=56;
else matrice (j,i)=248;
end
end
end

You're code seems to be missing a lot of stuff. Please double check that you copied everything correct.
A very quick way of doing what you want would be like this:
% create matrix
matrice=zeros(17,9);
matrice(2:16,3:6)=1;
% Plot matrix
imagesc(matrice)
% Color matrix
colormap([1 1 1; 1 0 0])
% Fix axis
axis square
The function imagesc will create an image where each index of the matrix is represented by a pixel. The data is scaled to fit into the current colormap, therefore by setting the current colormap to white and red only you get your red rectangle with white background.

Related

Removing part of an image from the 3d plot

In the above image, blue circle at the center and rectangle at the bottom represent defects while light blue area represents normal area.
When I use slice function for 3d representation, how do I get rid of light blue area so that we only see circle and rectangle in 3d plot?
You mean like this?
Here is the code I came up with (note that it depends on a threshold value that you have to compute youself, from your whole dataset, this is why it is a little noisy):
clear all;
close all;
pkg load image
im=double(rgb2gray(imread("5JpXg.jpg")));
im=im(10:end-10,10:end-10);
%you can try to find a better threshold based on your data
threshold=100;
im(im<threshold)=0;%or im(im>threshold)=0 if you want everything to be blank except the circle and the rectangle
[m n]=size(im);
num_non_zero_pixels=size(im(im~=0),1);
x=zeros(3,num_non_zero_pixels);
counter=1;
for i=1:m
for j=1:n
if(0~=im(i,j))
x(1,counter)=i;
x(2,counter)=j;
x(3,counter)=im(i,j);
counter=counter+1;
end
end
end
plot3(x(1,:)',x(2,:)',x(3,:)',"*");

How do I crop a 2-d surface plot in MATLAB, to remove axes and white margins?

I have written a program to process and print a 81x81 2D surface plot, which needs to be used as an input. Saving the plot made by Matlab includes side axes as well as white margins on the sides.
How do I crop this image to get just the (81pixels)x(81pixels) output as an image?
Try placing this after your figure code, it will remove the margins around your figure.
set(gca,'units','pixels') % set the axes units to pixels
xx = get(gca,'position'); % get the position of the axes;
set(gcf,'units','pixels') % set the figure units to pixels
yy = get(gcf,'position'); % get the figure position;
set(gcf,'position',[yy(1) yy(2) xx(3) xx(4)]) % set the position of the figure to the length and width of the axes
set(gca,'units','normalized','position',[0 0 1 1]) % set the axes units to pixels
You can avoid using surf plot and just save your array as image. So, you have 81x81 array. I'll use this one for the example:
a = magic(81);
Now you need to normalize image in order to have values from 0 to 255:
a = (a - min(min(a)))/(max(max(a))-min(min(a)))*255;
Finally you use imwrite to save your array as image.
imwrite(a,jet(256),'SO_4.png')
The first argument is your 81x81 array. The second argument is colormap. You can specify any colormap you want, but I like the heatmap jet(256). You can skip this argument, in this case the image would be grayscale. From here you can find what colormaps are available and what they look like. The last argument is image name. The result is shown below:
Hope that helps, good luck.

How can I create a rectangle with an outlined border?

I want to draw a rectangle to outline an area of an image that I've plotted in one axes of a figure. I have multiple axes in this figure, so I am using the rectangle() function. What I want is to draw a white rectangle a thin black border just inside and just outside the rectangle. The part of the image inside the rectangle should be visible, so all 'facecolor' should be 'none'. I have tried drawing 3 rectangles, two black ones with thin linewidths and one thicker white one, but the problem is that 'Position' is defined in axes units and 'LineWidth' is defined in point units, so that the scaling doesn't work too well, especially when the figure is resized.
FYI, the outline is so that the white rectangle is more visible against a light background. The images plotted vary widely, so a single color won't be universally visible for my data.
Any suggestions on how I can do this?
How about just using different line widths for black and white rectangle?
imshow('cameraman.tif')
rectangle('position',[80 30 100 100],'edgecolor','k','LineWidth',4)
rectangle('position',[80 30 100 100],'edgecolor','w','LineWidth',1)
Hmm, the corners look much better on MATLAB figure than as PNG file.
Better with getframe:
I like #Yuks solution. But there is another possibility that you can consider:
You could also calculate the mean value of the pixels inside the rectangle, and set the box color to the inverse. In this way, you will always have a good contrast.
Here is the code:
function PlotRect(im,x,y,w,h)
m = double(im( round(y): round(y+h) , round(x): round(x+w),:));
if (mean(m(:)) < 255/2)
col = [1 1 1];
else
col = [0 0 0];
end
rectangle('Position',[x y w h],'EdgeColor', col);
end
And the test:
function Inverse()
im = imresize( uint8(0:5:255), [250, 400]) ;
figure;imshow(im); hold on;
PlotRect(im,5,8,50,75);
PlotRect(im,100,30,25,42);
PlotRect(im,200,10,40,40);
PlotRect(im,300,10,40,40);
end
Yuk's solution works quite well for adding a rectangle to a normal MATLAB plot, too. The 'position' values are not interpretet as pixels but are adjusted to the plot values (see code example below):
figure;
plot(0:10,0:10); grid on;
hold on;
rectangle('position',[1 1 8.5 8.5],'LineWidth',2);
hold off;
This code results in the following plot:

Creating black/white squares using matlab

I'have to create these three simple squares in Matlab:
Can anyone give me a hand? I know how to use images by openning them (imread) but I dont know how to create them on matlab from 0.
To create image number 3, you have to remember that an image is just a matrix, and that black and white can be represented by 0 and 1, respectively. So the question becomes: How can you create a 2D array in Matlab that is all zeros except for some specific region?
%# create an empty image (all zeros)
%# use a logical image, since all we want to show
%# are black and white
img = false(256,256);
%# to add the square, make the top left quarter white
%# by setting the pixel values to true (i.e. 1)
img(1:128,1:128) = true;
%# show the image
figure,imshow(img)
pcolor might be an option
pcolor(hadamard(20))
colormap(gray(2))
axis ij
axis square

Relative Markersize in Matlab plots

I am trying to plot a matrix where each element is in one out of two states. (ising model..)
Now, I would like to have one state colored and the other one white. That works using
[i,j] = find(S);
figure(gcf);
plothandle = scatter(i,j);
axis([0 nNodes+1 0 nNodes+1]);
when S holds the Spins and one state is equal to 0. (find returns a matrix of only non-zero elements)
To have a useful plot, the sizes of the markers should be 1x1 in RELATIVE coordinates. So if the whole matrix S would be in a state non-zero, everything would be colored.
However, it seems like Matlab only allows MarkerSizes in points or inches. How could I solve this?
One idea I had was, that I find out the point-size of the axes and then can easily calculate how big my markers should be. Then I would have to create a callback function if I want to zoom in and so on. Also, I have not yet found a way (without the image acq. toolbox) to find out the absolute size of my axes.
To clarify what I want: How could I plot a chessboard using a matrix with 1 for black and 0 for white fields?
For displaying data of this sort I generally prefer IMAGE or IMAGESC to PCOLOR since PCOLOR won't display the last row and column of the matrix when using faceted shading (the default). Also, IMAGE and IMAGESC flip the y axis so the image more intuitively matches what you think of when looking at a matrix (i.e. rows start from 1 at the top). You can visualize your matrix like this:
S = round(rand(20)); %# Sample 20-by-20 matrix of ones and zeroes
imagesc(S); %# Plot the image
colormap([1 1 1; 0 0 0]); %# Set the colormap to show white (zero elements) and
%# black (non-zero elements)
And here's a sample image:
Just as a suggestion, you can try using pcolor instead of `scatter' Example:
pcolor(hadamard(20))
colormap(gray(2))
axis ij
axis square